1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
|
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
A "DAV object" is anything we get from the caldav server or push into the
caldav server, notably principal, calendars and calendar events.
(This file has become huge and will be split up prior to the next
release. I think it makes sense moving the CalendarObjectResource
class hierarchy into a separate file)
"""
import re
import uuid
from collections import defaultdict
from datetime import date
from datetime import datetime
from datetime import timedelta
from datetime import timezone
import icalendar
import vobject
from caldav.lib.python_utilities import to_normal_str
from caldav.lib.python_utilities import to_unicode
from caldav.lib.python_utilities import to_wire
from dateutil.rrule import rrulestr
from lxml import etree
try:
# noinspection PyCompatibility
from urllib.parse import unquote, quote
except ImportError:
from urllib import unquote, quote
try:
from typing import ClassVar, Union, Optional
TimeStamp = Optional[Union[date, datetime]]
except:
pass
from caldav.lib import error, vcal
from caldav.lib.url import URL
from caldav.elements import dav, cdav, ical
import logging
log = logging.getLogger("caldav")
def errmsg(r):
"""Utility for formatting a response xml tree to an error string"""
return "%s %s\n\n%s" % (r.status, r.reason, r.raw)
class DAVObject(object):
"""
Base class for all DAV objects. Can be instantiated by a client
and an absolute or relative URL, or from the parent object.
"""
id = None
url = None
client = None
parent = None
name = None
def __init__(
self,
client=None,
url=None,
parent=None,
name=None,
id=None,
props=None,
**extra,
):
"""
Default constructor.
Parameters:
* client: A DAVClient instance
* url: The url for this object. May be a full URL or a relative URL.
* parent: The parent object - used when creating objects
* name: A displayname - to be removed in 1.0, see https://github.com/python-caldav/caldav/issues/128 for details
* props: a dict with known properties for this object (as of 2020-12, only used for etags, and only when fetching CalendarObjectResource using the .objects or .objects_by_sync_token methods).
* id: The resource id (UID for an Event)
"""
if client is None and parent is not None:
client = parent.client
self.client = client
self.parent = parent
self.name = name
self.id = id
if props is None:
self.props = {}
else:
self.props = props
self.extra_init_options = extra
# url may be a path relative to the caldav root
if client and url:
self.url = client.url.join(url)
else:
self.url = URL.objectify(url)
@property
def canonical_url(self):
return str(self.url.canonical())
def children(self, type=None):
"""List children, using a propfind (resourcetype) on the parent object,
at depth = 1.
TODO: This is old code, it's querying for DisplayName and
ResourceTypes prop and returning a tuple of those. Those two
are relatively arbitrary. I think it's mostly only calendars
having DisplayName, but it may make sense to ask for the
children of a calendar also as an alternative way to get all
events? It should be redone into a more generic method, and
it should probably return a dict rather than a tuple. We
should also look over to see if there is any code duplication.
"""
c = []
depth = 1
properties = {}
props = [dav.DisplayName()]
multiprops = [dav.ResourceType()]
response = self._query_properties(props + multiprops, depth)
properties = response.expand_simple_props(
props=props, multi_value_props=multiprops
)
for path in list(properties.keys()):
resource_types = properties[path][dav.ResourceType.tag]
resource_name = properties[path][dav.DisplayName.tag]
if type is None or type in resource_types:
url = URL(path)
if url.hostname is None:
# Quote when path is not a full URL
path = quote(path)
# TODO: investigate the RFCs thoroughly - why does a "get
# members of this collection"-request also return the
# collection URL itself?
# And why is the strip_trailing_slash-method needed?
# The collection URL should always end with a slash according
# to RFC 2518, section 5.2.
if (isinstance(self, CalendarSet) and type == cdav.Calendar.tag) or (
self.url.canonical().strip_trailing_slash()
!= self.url.join(path).canonical().strip_trailing_slash()
):
c.append((self.url.join(path), resource_types, resource_name))
## TODO: return objects rather than just URLs, and include
## the properties we've already fetched
return c
def _query_properties(self, props=None, depth=0):
"""
This is an internal method for doing a propfind query. It's a
result of code-refactoring work, attempting to consolidate
similar-looking code into a common method.
"""
root = None
# build the propfind request
if props is not None and len(props) > 0:
prop = dav.Prop() + props
root = dav.Propfind() + prop
return self._query(root, depth)
def _query(
self,
root=None,
depth=0,
query_method="propfind",
url=None,
expected_return_value=None,
):
"""
This is an internal method for doing a query. It's a
result of code-refactoring work, attempting to consolidate
similar-looking code into a common method.
"""
body = ""
if root:
if hasattr(root, "xmlelement"):
body = etree.tostring(
root.xmlelement(), encoding="utf-8", xml_declaration=True
)
else:
body = root
if url is None:
url = self.url
ret = getattr(self.client, query_method)(url, body, depth)
if ret.status == 404:
raise error.NotFoundError(errmsg(ret))
if (
expected_return_value is not None and ret.status != expected_return_value
) or ret.status >= 400:
## COMPATIBILITY HACK - see https://github.com/python-caldav/caldav/issues/309
body = to_wire(body)
if (
ret.status == 500
and not b"getetag" in body
and b"<C:calendar-data/>" in body
):
body = body.replace(
b"<C:calendar-data/>", b"<D:getetag/><C:calendar-data/>"
)
return self._query(
body, depth, query_method, url, expected_return_value
)
raise error.exception_by_method[query_method](errmsg(ret))
return ret
def get_property(self, prop, use_cached=False, **passthrough):
## TODO: use_cached should probably be true
if use_cached:
if prop.tag in self.props:
return self.props[prop.tag]
foo = self.get_properties([prop], **passthrough)
return foo.get(prop.tag, None)
def get_properties(
self, props=None, depth=0, parse_response_xml=True, parse_props=True
):
"""Get properties (PROPFIND) for this object.
With parse_response_xml and parse_props set to True a
best-attempt will be done on decoding the XML we get from the
server - but this works only for properties that don't have
complex types. With parse_response_xml set to False, a
DAVResponse object will be returned, and it's up to the caller
to decode. With parse_props set to false but
parse_response_xml set to true, xml elements will be returned
rather than values.
Parameters:
* props = [dav.ResourceType(), dav.DisplayName(), ...]
Returns:
* {proptag: value, ...}
"""
rc = None
response = self._query_properties(props, depth)
if not parse_response_xml:
return response
if not parse_props:
properties = response.find_objects_and_props()
else:
properties = response.expand_simple_props(props)
error.assert_(properties)
path = unquote(self.url.path)
if path.endswith("/"):
exchange_path = path[:-1]
else:
exchange_path = path + "/"
if path in properties:
rc = properties[path]
elif exchange_path in properties:
if not isinstance(self, Principal):
## Some caldav servers reports the URL for the current
## principal to end with / when doing a propfind for
## current-user-principal - I believe that's a bug,
## the principal is not a collection and should not
## end with /. (example in rfc5397 does not end with /).
## ... but it gets worse ... when doing a propfind on the
## principal, the href returned may be without the slash.
## Such inconsistency is clearly a bug.
log.error(
"potential path handling problem with ending slashes. Path given: %s, path found: %s. %s"
% (path, exchange_path, error.ERR_FRAGMENT)
)
error._assert(False)
rc = properties[exchange_path]
elif self.url in properties:
rc = properties[self.url]
elif "/principal/" in properties and path.endswith("/principal/"):
## Workaround for a known iCloud bug.
## The properties key is expected to be the same as the path.
## path is on the format /123456/principal/ but properties key is /principal/
## tests apparently passed post bc589093a34f0ed0ef489ad5e9cba048750c9837 and 3ee4e42e2fa8f78b71e5ffd1ef322e4007df7a60, even without this workaround
## TODO: should probably be investigated more.
## (observed also by others, ref https://github.com/python-caldav/caldav/issues/168)
rc = properties["/principal/"]
elif "//" in path and path.replace("//", "/") in properties:
## ref https://github.com/python-caldav/caldav/issues/302
## though, it would be nice to find the root cause,
## self.url should not contain double slashes in the first place
rc = properties[path.replace("//", "/")]
elif len(properties) == 1:
## Ref https://github.com/python-caldav/caldav/issues/191 ...
## let's be pragmatic and just accept whatever the server is
## throwing at us. But we'll log an error anyway.
log.error(
"Possibly the server has a path handling problem, possibly the URL configured is wrong.\n"
"Path expected: %s, path found: %s %s.\n"
"Continuing, probably everything will be fine"
% (path, str(list(properties.keys())), error.ERR_FRAGMENT)
)
rc = list(properties.values())[0]
else:
log.error(
"Possibly the server has a path handling problem. Path expected: %s, paths found: %s %s"
% (path, str(list(properties.keys())), error.ERR_FRAGMENT)
)
error.assert_(False)
if parse_props:
self.props.update(rc)
return rc
def set_properties(self, props=None):
"""
Set properties (PROPPATCH) for this object.
* props = [dav.DisplayName('name'), ...]
Returns:
* self
"""
props = [] if props is None else props
prop = dav.Prop() + props
set = dav.Set() + prop
root = dav.PropertyUpdate() + set
r = self._query(root, query_method="proppatch")
statuses = r.tree.findall(".//" + dav.Status.tag)
for s in statuses:
if " 200 " not in s.text:
raise error.PropsetError(s.text)
return self
def save(self):
"""
Save the object. This is an abstract method, that all classes
derived from DAVObject implement.
Returns:
* self
"""
raise NotImplementedError()
def delete(self):
"""
Delete the object.
"""
if self.url is not None:
r = self.client.delete(self.url)
# TODO: find out why we get 404
if r.status not in (200, 204, 404):
raise error.DeleteError(errmsg(r))
def get_display_name(self):
"""
Get calendar display name
"""
return self.get_property(dav.DisplayName())
def __str__(self):
try:
return (
str(self.get_property(dav.DisplayName(), use_cached=True)) or self.url
)
except:
return str(self.url)
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, self.url)
class CalendarSet(DAVObject):
"""
A CalendarSet is a set of calendars.
"""
def calendars(self):
"""
List all calendar collections in this set.
Returns:
* [Calendar(), ...]
"""
cals = []
data = self.children(cdav.Calendar.tag)
for c_url, c_type, c_name in data:
try:
cal_id = c_url.split("/")[-2]
except:
log.error(f"Calendar {c_name} has unexpected url {c_url}")
cal_id = None
cals.append(
Calendar(self.client, id=cal_id, url=c_url, parent=self, name=c_name)
)
return cals
def make_calendar(
self, name=None, cal_id=None, supported_calendar_component_set=None
):
"""
Utility method for creating a new calendar.
Parameters:
* name: the display name of the new calendar
* cal_id: the uuid of the new calendar
* supported_calendar_component_set: what kind of objects
(EVENT, VTODO, VFREEBUSY, VJOURNAL) the calendar should handle.
Should be set to ['VTODO'] when creating a task list in Zimbra -
in most other cases the default will be OK.
Returns:
* Calendar(...)-object
"""
return Calendar(
self.client,
name=name,
parent=self,
id=cal_id,
supported_calendar_component_set=supported_calendar_component_set,
).save()
def calendar(self, name=None, cal_id=None):
"""
The calendar method will return a calendar object. If it gets a cal_id
but no name, it will not initiate any communication with the server
Parameters:
* name: return the calendar with this display name
* cal_id: return the calendar with this calendar id or URL
Returns:
* Calendar(...)-object
"""
if name and not cal_id:
for calendar in self.calendars():
display_name = calendar.get_display_name()
if display_name == name:
return calendar
if name and not cal_id:
raise error.NotFoundError(
"No calendar with name %s found under %s" % (name, self.url)
)
if not cal_id and not name:
return self.calendars()[0]
if str(URL.objectify(cal_id).canonical()).startswith(
str(self.client.url.canonical())
):
url = self.client.url.join(cal_id)
elif (
isinstance(cal_id, URL)
or cal_id.startswith("https://")
or cal_id.startswith("http://")
):
url = self.url.join(cal_id)
else:
url = self.url.join(quote(cal_id) + "/")
return Calendar(self.client, name=name, parent=self, url=url, id=cal_id)
class Principal(DAVObject):
"""
This class represents a DAV Principal. It doesn't do much, except
keep track of the URLs for the calendar-home-set, etc.
A principal MUST have a non-empty DAV:displayname property
(defined in Section 13.2 of [RFC2518]),
and a DAV:resourcetype property (defined in Section 13.9 of [RFC2518]).
Additionally, a principal MUST report the DAV:principal XML element
in the value of the DAV:resourcetype property.
(TODO: the resourcetype is actually never checked, and the DisplayName
is not stored anywhere)
"""
def __init__(self, client=None, url=None):
"""
Returns a Principal.
Parameters:
* client: a DAVClient() object
* url: Deprecated - for backwards compatibility purposes only.
If url is not given, deduct principal path as well as calendar home set
path from doing propfinds.
"""
super(Principal, self).__init__(client=client, url=url)
self._calendar_home_set = None
if url is None:
self.url = self.client.url
cup = self.get_property(dav.CurrentUserPrincipal())
self.url = self.client.url.join(URL.objectify(cup))
def make_calendar(
self, name=None, cal_id=None, supported_calendar_component_set=None
):
"""
Convenience method, bypasses the self.calendar_home_set object.
See CalendarSet.make_calendar for details.
"""
return self.calendar_home_set.make_calendar(
name,
cal_id,
supported_calendar_component_set=supported_calendar_component_set,
)
def calendar(self, name=None, cal_id=None, cal_url=None):
"""
The calendar method will return a calendar object.
It will not initiate any communication with the server.
"""
if not cal_url:
return self.calendar_home_set.calendar(name, cal_id)
else:
return Calendar(self.client, url=self.client.url.join(cal_url))
def get_vcal_address(self):
"""
Returns the principal, as an icalendar.vCalAddress object
"""
from icalendar import vCalAddress, vText
cn = self.get_display_name()
ids = self.calendar_user_address_set()
cutype = self.get_property(cdav.CalendarUserType())
ret = vCalAddress(ids[0])
ret.params["cn"] = vText(cn)
ret.params["cutype"] = vText(cutype)
return ret
@property
def calendar_home_set(self):
if not self._calendar_home_set:
calendar_home_set_url = self.get_property(cdav.CalendarHomeSet())
## owncloud returns /remote.php/dav/calendars/tobixen@e.email/
## in that case the @ should be quoted. Perhaps other
## implementations return already quoted URLs. Hacky workaround:
if (
calendar_home_set_url is not None
and "@" in calendar_home_set_url
and not "://" in calendar_home_set_url
):
calendar_home_set_url = quote(calendar_home_set_url)
self.calendar_home_set = calendar_home_set_url
return self._calendar_home_set
@calendar_home_set.setter
def calendar_home_set(self, url):
if isinstance(url, CalendarSet):
self._calendar_home_set = url
return
sanitized_url = URL.objectify(url)
## TODO: sanitized_url should never be None, this needs more
## research. added here as it solves real-world issues, ref
## https://github.com/python-caldav/caldav/pull/56
if sanitized_url is not None:
if (
sanitized_url.hostname
and sanitized_url.hostname != self.client.url.hostname
):
# icloud (and others?) having a load balanced system,
# where each principal resides on one named host
## TODO:
## Here be dragons. sanitized_url will be the root
## of all future objects derived from client. Changing
## the client.url root by doing a principal.calendars()
## is an unacceptable side effect and may be a cause of
## incompatibilities with icloud. Do more research!
self.client.url = sanitized_url
self._calendar_home_set = CalendarSet(
self.client, self.client.url.join(sanitized_url)
)
def calendars(self):
"""
Return the principials calendars
"""
return self.calendar_home_set.calendars()
def freebusy_request(self, dtstart, dtend, attendees):
freebusy_ical = icalendar.Calendar()
freebusy_ical.add("prodid", "-//tobixen/python-caldav//EN")
freebusy_ical.add("version", "2.0")
freebusy_ical.add("method", "REQUEST")
uid = uuid.uuid1()
freebusy_comp = icalendar.FreeBusy()
freebusy_comp.add("uid", uid)
freebusy_comp.add("dtstamp", datetime.now())
freebusy_comp.add("dtstart", dtstart)
freebusy_comp.add("dtend", dtend)
freebusy_ical.add_component(freebusy_comp)
outbox = self.schedule_outbox()
caldavobj = FreeBusy(data=freebusy_ical, parent=outbox)
caldavobj.add_organizer()
for attendee in attendees:
caldavobj.add_attendee(attendee, no_default_parameters=True)
response = self.client.post(
outbox.url,
caldavobj.data,
headers={"Content-Type": "text/calendar; charset=utf-8"},
)
return response.find_objects_and_props()
def calendar_user_address_set(self):
"""
defined in RFC6638
"""
addresses = self.get_property(cdav.CalendarUserAddressSet(), parse_props=False)
assert not [x for x in addresses if x.tag != dav.Href().tag]
addresses = list(addresses)
## possibly the preferred attribute is iCloud-specific.
## TODO: do more research on that
addresses.sort(key=lambda x: -int(x.get("preferred", 0)))
return [x.text for x in addresses]
def schedule_inbox(self):
return ScheduleInbox(principal=self)
def schedule_outbox(self):
return ScheduleOutbox(principal=self)
class Calendar(DAVObject):
"""
The `Calendar` object is used to represent a calendar collection.
Refer to the RFC for details:
https://tools.ietf.org/html/rfc4791#section-5.3.1
"""
def _create(self, name=None, id=None, supported_calendar_component_set=None):
"""
Create a new calendar with display name `name` in `parent`.
"""
if id is None:
id = str(uuid.uuid1())
self.id = id
path = self.parent.url.join(id + "/")
self.url = path
# TODO: mkcalendar seems to ignore the body on most servers?
# at least the name doesn't get set this way.
# zimbra gives 500 (!) if body is omitted ...
prop = dav.Prop()
if name:
display_name = dav.DisplayName(name)
prop += [
display_name,
]
if supported_calendar_component_set:
sccs = cdav.SupportedCalendarComponentSet()
for scc in supported_calendar_component_set:
sccs += cdav.Comp(scc)
prop += sccs
set = dav.Set() + prop
mkcol = cdav.Mkcalendar() + set
r = self._query(
root=mkcol, query_method="mkcalendar", url=path, expected_return_value=201
)
# COMPATIBILITY ISSUE
# name should already be set, but we've seen caldav servers failing
# on setting the DisplayName on calendar creation
# (DAViCal, Zimbra, ...). Doing an attempt on explicitly setting the
# display name using PROPPATCH.
if name:
try:
self.set_properties([display_name])
except:
## TODO: investigate. Those asserts break.
error.assert_(False)
try:
current_display_name = self.get_display_name()
error.assert_(current_display_name == name)
except:
log.warning(
"calendar server does not support display name on calendar? Ignoring",
exc_info=True,
)
error.assert_(False)
def get_supported_components(self):
"""
returns a list of component types supported by the calendar, in
string format (typically ['VJOURNAL', 'VTODO', 'VEVENT'])
"""
props = [cdav.SupportedCalendarComponentSet()]
response = self.get_properties(props, parse_response_xml=False)
response_list = response.find_objects_and_props()
prop = response_list[unquote(self.url.path)][
cdav.SupportedCalendarComponentSet().tag
]
return [supported.get("name") for supported in prop]
def save_with_invites(self, ical, attendees, **attendeeoptions):
"""
sends a schedule request to the server. Equivalent with save_event, save_todo, etc,
but the attendees will be added to the ical object before sending it to the server.
"""
## TODO: consolidate together with save_*
obj = self._calendar_comp_class_by_data(ical)(data=ical, client=self.client)
obj.parent = self
obj.add_organizer()
for attendee in attendees:
obj.add_attendee(attendee, **attendeeoptions)
obj.id = obj.icalendar_instance.walk("vevent")[0]["uid"]
obj.save()
return obj
def _use_or_create_ics(self, ical, objtype, **ical_data):
if ical_data or (
(isinstance(ical, str) or isinstance(ical, bytes))
and not b"BEGIN:VCALENDAR" in to_wire(ical)
):
## TODO: the ical_fragment code is not much tested
if ical and not "ical_fragment" in ical_data:
ical_data["ical_fragment"] = ical
return vcal.create_ical(objtype=objtype, **ical_data)
return ical
## TODO: consolidate save_* - too much code duplication here
def save_event(self, ical=None, no_overwrite=False, no_create=False, **ical_data):
"""
Add a new event to the calendar, with the given ical.
Parameters:
* ical - ical object (text)
* no_overwrite - existing calendar objects should not be overwritten
* no_create - don't create a new object, existing calendar objects should be updated
* ical_data - passed to lib.vcal.create_ical
"""
e = Event(
self.client,
data=self._use_or_create_ics(ical, objtype="VEVENT", **ical_data),
parent=self,
)
e.save(no_overwrite=no_overwrite, no_create=no_create, obj_type="event")
self._handle_relations(e.id, ical_data)
return e
def save_todo(self, ical=None, no_overwrite=False, no_create=False, **ical_data):
"""
Add a new task to the calendar, with the given ical.
Parameters:
* ical - ical object (text)
"""
t = Todo(
self.client,
data=self._use_or_create_ics(ical, objtype="VTODO", **ical_data),
parent=self,
)
t.save(no_overwrite=no_overwrite, no_create=no_create, obj_type="todo")
self._handle_relations(t.id, ical_data)
return t
def save_journal(self, ical=None, no_overwrite=False, no_create=False, **ical_data):
"""
Add a new journal entry to the calendar, with the given ical.
Parameters:
* ical - ical object (text)
"""
j = Journal(
self.client,
data=self._use_or_create_ics(ical, objtype="VJOURNAL", **ical_data),
parent=self,
)
j.save(no_overwrite=no_overwrite, no_create=no_create, obj_type="journal")
self._handle_relations(j.id, ical_data)
return j
def _handle_relations(self, uid, ical_data):
for reverse_reltype, other_uid in [
("parent", x) for x in ical_data.get("child", ())
] + [("child", x) for x in ical_data.get("parent", ())]:
other = self.object_by_uid(other_uid)
other.set_relation(other=uid, reltype=reverse_reltype, set_reverse=False)
## legacy aliases
## TODO: should be deprecated
## TODO: think more through this - is `save_foo` better than `add_foo`?
## `save_foo` should not be used for updating existing content on the
## calendar!
add_event = save_event
add_todo = save_todo
add_journal = save_journal
def save(self):
"""
The save method for a calendar is only used to create it, for now.
We know we have to create it when we don't have a url.
Returns:
* self
"""
if self.url is None:
self._create(id=self.id, name=self.name, **self.extra_init_options)
return self
def calendar_multiget(self, event_urls):
"""
get multiple events' data
@author mtorange@gmail.com
@type events list of Event
"""
rv = []
prop = dav.Prop() + cdav.CalendarData()
root = (
cdav.CalendarMultiGet()
+ prop
+ [dav.Href(value=u.path) for u in event_urls]
)
response = self._query(root, 1, "report")
results = response.expand_simple_props([cdav.CalendarData()])
for r in results:
rv.append(
Event(
self.client,
url=self.url.join(r),
data=results[r][cdav.CalendarData.tag],
parent=self,
)
)
return rv
## TODO: Upgrade the warning to an error (and perhaps critical) in future
## releases, and then finally remove this method completely.
def build_date_search_query(
self, start, end=None, compfilter="VEVENT", expand="maybe"
):
"""
WARNING: DEPRECATED
"""
## This is dead code. It has no tests. It was made for usage
## by the date_search method, but I've decided not to use it
## there anymore. Most likely nobody is using this, as it's
## sort of an internal method - but for the sake of backward
## compatibility I will keep it for a while. I regret naming
## it build_date_search_query rather than
## _build_date_search_query...
logging.warning(
"DEPRECATION WARNING: The calendar.build_date_search_query method will be removed in caldav library from version 1.0 or perhaps earlier. Use calendar.build_search_xml_query instead."
)
if expand == "maybe":
expand = end
if compfilter == "VEVENT":
comp_class = Event
elif compfilter == "VTODO":
comp_class = Todo
else:
comp_class = None
return self.build_search_xml_query(
comp_class=comp_class, expand=expand, start=start, end=end
)
def date_search(
self, start, end=None, compfilter="VEVENT", expand="maybe", verify_expand=False
):
# type (TimeStamp, TimeStamp, str, str) -> CalendarObjectResource
"""Deprecated. Use self.search() instead.
Search events by date in the calendar. Recurring events are
expanded if they are occurring during the specified time frame
and if an end timestamp is given.
Parameters:
* start = datetime.today().
* end = same as above.
* compfilter = defaults to events only. Set to None to fetch all
calendar components.
* expand - should recurrent events be expanded? (to preserve
backward-compatibility the default "maybe" will be changed into True
unless the date_search is open-ended)
* verify_expand - not in use anymore, but kept for backward compatibility
Returns:
* [CalendarObjectResource(), ...]
"""
## TODO: upgrade to warning and error before removing this method
logging.info(
"DEPRECATION NOTICE: The calendar.date_search method may be removed in release 2.0 of the caldav library. Use calendar.search instead"
)
if verify_expand:
logging.warning(
"verify_expand in date_search does not work anymore, as we're doing client side expansion instead"
)
## for backward compatibility - expand should be false
## in an open-ended date search, otherwise true
if expand == "maybe":
expand = end
if compfilter == "VEVENT":
comp_class = Event
elif compfilter == "VTODO":
comp_class = Todo
else:
comp_class = None
## xandikos now yields a 5xx-error when trying to pass
## expand=True, after I prodded the developer that it doesn't
## work. By now there is some workaround in the test code to
## avoid sending expand=True to xandikos, but perhaps we
## should run a try-except-retry here with expand=False in the
## retry, and warnings logged ... or perhaps not.
objects = self.search(
start=start,
end=end,
comp_class=comp_class,
expand=expand,
split_expanded=False,
)
return objects
def _request_report_build_resultlist(
self, xml, comp_class=None, props=None, no_calendardata=False
):
"""
Takes some input XML, does a report query on a calendar object
and returns the resource objects found.
TODO: similar code is duplicated many places, we ought to do even more code
refactoring
"""
matches = []
if props is None:
props_ = [cdav.CalendarData()]
else:
props_ = [cdav.CalendarData()] + props
response = self._query(xml, 1, "report")
results = response.expand_simple_props(props_)
for r in results:
pdata = results[r]
if cdav.CalendarData.tag in pdata:
cdata = pdata.pop(cdav.CalendarData.tag)
if comp_class is None:
comp_class = self._calendar_comp_class_by_data(cdata)
else:
cdata = None
if comp_class is None:
## no CalendarData fetched - which is normal i.e. when doing a sync-token report and only asking for the URLs
comp_class = CalendarObjectResource
url = URL(r)
if url.hostname is None:
# Quote when result is not a full URL
url = quote(r)
## icloud hack - icloud returns the calendar URL as well as the calendar item URLs
if self.url.join(url) == self.url:
continue
matches.append(
comp_class(
self.client,
url=self.url.join(url),
data=cdata,
parent=self,
props=pdata,
)
)
return (response, matches)
def search(
self,
xml=None,
comp_class=None,
todo=None,
include_completed=False,
sort_keys=(),
split_expanded=True,
props=None,
**kwargs,
):
"""Creates an XML query, does a REPORT request towards the
server and returns objects found, eventually sorting them
before delivery.
This method contains some special logics to ensure that it can
consistently return a list of pending tasks on any server
implementation. In the future it may also include workarounds
and client side filtering to make sure other search results
are consistent on different server implementations.
Parameters supported:
* xml - use this search query, and ignore other filter parameters
* comp_class - set to event, todo or journal to restrict search to this
resource type. Some server implementations require this to be set.
* todo - sets comp_class to Todo, and restricts search to pending tasks,
unless the next parameter is set ...
* include_completed - include completed tasks
* event - sets comp_class to event
* text attribute search parameters: category, uid, summary, omment,
description, location, status
* no-category, no-summary, etc ... search for objects that does not
have those attributes. TODO: WRITE TEST CODE!
* expand - do server side expanding of recurring events/tasks
* start, end: do a time range search
* filters - other kind of filters (in lxml tree format)
* sort_keys - list of attributes to use when sorting
not supported yet:
* negated text match
* attribute not set
"""
## special compatibility-case when searching for pending todos
if todo and not include_completed:
matches1 = self.search(
todo=True,
comp_class=comp_class,
ignore_completed1=True,
include_completed=True,
**kwargs,
)
matches2 = self.search(
todo=True,
comp_class=comp_class,
ignore_completed2=True,
include_completed=True,
**kwargs,
)
matches3 = self.search(
todo=True,
comp_class=comp_class,
ignore_completed3=True,
include_completed=True,
**kwargs,
)
objects = []
match_set = set()
for item in matches1 + matches2 + matches3:
if not item.url in match_set:
match_set.add(item.url)
## and still, Zimbra seems to deliver too many TODOs in the
## matches2 ... let's do some post-filtering in case the
## server fails in filtering things the right way
if "STATUS:NEEDS-ACTION" in item.data or (
not "\nCOMPLETED:" in item.data
and not "\nSTATUS:COMPLETED" in item.data
and not "\nSTATUS:CANCELLED" in item.data
):
objects.append(item)
else:
if not xml:
(xml, comp_class) = self.build_search_xml_query(
comp_class=comp_class, todo=todo, props=props, **kwargs
)
elif kwargs:
raise error.ConsistencyError(
"Inconsistent usage parameters: xml together with other search options"
)
(response, objects) = self._request_report_build_resultlist(
xml, comp_class, props=props
)
if kwargs.get("expand", False):
## expand can only be used together with start and end.
## Error checking is done in build_search_xml_query. If
## search is fed with an XML query together with expand,
## then it's considered a "search option", and an error is
## raised above.
start = kwargs["start"]
end = kwargs["end"]
for o in objects:
## This would not be needed if the servers would follow the standard ...
o.load(only_if_unloaded=True)
## Google sometimes returns empty objects
objects = [o for o in objects if o.icalendar_component]
for o in objects:
component = o.icalendar_component
if component is None:
continue
recurrence_properties = ["exdate", "exrule", "rdate", "rrule"]
if any(key in component for key in recurrence_properties):
o.expand_rrule(start, end)
if split_expanded:
objects_ = objects
objects = []
for o in objects_:
objects.extend(o.split_expanded())
def sort_key_func(x):
ret = []
comp = x.icalendar_component
defaults = {
## TODO: all possible non-string sort attributes needs to be listed here, otherwise we will get type errors when comparing objects with the property defined vs undefined (or maybe we should make an "undefined" object that always will compare below any other type? Perhaps there exists such an object already?)
"due": "2050-01-01",
"dtstart": "1970-01-01",
"priority": 0,
"status": {
"VTODO": "NEEDS-ACTION",
"VJOURNAL": "FINAL",
"VEVENT": "TENTATIVE",
}[comp.name],
"category": "",
## Usage of strftime is a simple way to ensure there won't be
## problems if comparing dates with timestamps
"isnt_overdue": not (
"due" in comp
and comp["due"].dt.strftime("%F%H%M%S")
< datetime.now().strftime("%F%H%M%S")
),
"hasnt_started": (
"dtstart" in comp
and comp["dtstart"].dt.strftime("%F%H%M%S")
> datetime.now().strftime("%F%H%M%S")
),
}
for sort_key in sort_keys:
val = comp.get(sort_key, None)
if val is None:
ret.append(defaults.get(sort_key.lower(), ""))
continue
if hasattr(val, "dt"):
val = val.dt
elif hasattr(val, "cats"):
val = ",".join(val.cats)
if hasattr(val, "strftime"):
ret.append(val.strftime("%F%H%M%S"))
else:
ret.append(val)
return ret
if sort_keys:
objects.sort(key=sort_key_func)
## partial workaround for https://github.com/python-caldav/caldav/issues/201
for obj in objects:
try:
obj.load(only_if_unloaded=True)
except:
pass
return objects
def build_search_xml_query(
self,
comp_class=None,
todo=None,
ignore_completed1=None,
ignore_completed2=None,
ignore_completed3=None,
event=None,
filters=None,
expand=None,
start=None,
end=None,
props=None,
**kwargs,
):
"""This method will produce a caldav search query as an etree object.
It is primarily to be used from the search method. See the
documentation for the search method for more information.
"""
# those xml elements are weird. (a+b)+c != a+(b+c). First makes b and c as list members of a, second makes c an element in b which is an element of a.
# First objective is to let this take over all xml search query building and see that the current tests pass.
# ref https://www.ietf.org/rfc/rfc4791.txt, section 7.8.9 for how to build a todo-query
# We'll play with it and don't mind it's getting ugly and don't mind that the test coverage is lacking.
# we'll refactor and create some unit tests later, as well as ftests for complicated queries.
# build the request
data = cdav.CalendarData()
if expand:
if not start or not end:
raise error.ReportError("can't expand without a date range")
data += cdav.Expand(start, end)
if props is None:
props_ = [data]
else:
props_ = [data] + props
prop = dav.Prop() + props_
vcalendar = cdav.CompFilter("VCALENDAR")
comp_filter = None
if not filters:
filters = []
vNotCompleted = cdav.TextMatch("COMPLETED", negate=True)
vNotCancelled = cdav.TextMatch("CANCELLED", negate=True)
vNeedsAction = cdav.TextMatch("NEEDS-ACTION")
vStatusNotCompleted = cdav.PropFilter("STATUS") + vNotCompleted
vStatusNotCancelled = cdav.PropFilter("STATUS") + vNotCancelled
vStatusNeedsAction = cdav.PropFilter("STATUS") + vNeedsAction
vStatusNotDefined = cdav.PropFilter("STATUS") + cdav.NotDefined()
vNoCompleteDate = cdav.PropFilter("COMPLETED") + cdav.NotDefined()
if ignore_completed1:
## This query is quite much in line with https://tools.ietf.org/html/rfc4791#section-7.8.9
filters.extend([vNoCompleteDate, vStatusNotCompleted, vStatusNotCancelled])
elif ignore_completed2:
## some server implementations (i.e. NextCloud
## and Baikal) will yield "false" on a negated TextMatch
## if the field is not defined. Hence, for those
## implementations we need to turn back and ask again
## ... do you have any VTODOs for us where the STATUS
## field is not defined? (ref
## https://github.com/python-caldav/caldav/issues/14)
filters.extend([vNoCompleteDate, vStatusNotDefined])
elif ignore_completed3:
## ... and considering recurring tasks we really need to
## look a third time as well, this time for any task with
## the NEEDS-ACTION status set (do we need the first go?
## NEEDS-ACTION or no status set should cover them all?)
filters.extend([vStatusNeedsAction])
if start or end:
filters.append(cdav.TimeRange(start, end))
if todo is not None:
if not todo:
raise NotImplementedError()
if todo:
if comp_class is not None and comp_class is not Todo:
raise error.ConsistencyError(
"inconsistent search parameters - comp_class = %s, todo=%s"
% (comp_class, todo)
)
comp_filter = cdav.CompFilter("VTODO")
comp_class = Todo
if event is not None:
if not event:
raise NotImplementedError()
if event:
if comp_class is not None and comp_class is not Event:
raise error.ConsistencyError(
"inconsistent search parameters - comp_class = %s, event=%s"
% (comp_class, event)
)
comp_filter = cdav.CompFilter("VEVENT")
comp_class = Event
elif comp_class:
if comp_class is Todo:
comp_filter = cdav.CompFilter("VTODO")
elif comp_class is Event:
comp_filter = cdav.CompFilter("VEVENT")
elif comp_class is Journal:
comp_filter = cdav.CompFilter("VJOURNAL")
else:
raise error.ConsistencyError(
"unsupported comp class %s for search" % comp_class
)
for other in kwargs:
find_not_defined = other.startswith("no_")
find_defined = other.startswith("has_")
if find_not_defined:
other = other[3:]
if find_defined:
other = other[4:]
if other in (
"uid",
"summary",
"comment",
"class_",
"class",
"category",
"description",
"location",
"status",
"due",
"dtstamp",
"dtstart",
"dtend",
"duration",
"priority",
):
## category and class_ is special
if other.endswith("category"):
## TODO: we probably need to do client side filtering. I would
## expect --category='e' to fetch anything having the category e,
## but not including all other categories containing the letter e.
## As I read the caldav standard, the latter will be yielded.
target = other.replace("category", "categories")
elif other == "class_":
target = "class"
else:
target = other
if find_not_defined:
match = cdav.NotDefined()
elif find_defined:
raise NotImplemented(
"Seems not to be supported by the CalDAV protocol? or we can negate? not supported yet, in any case"
)
else:
match = cdav.TextMatch(kwargs[other])
filters.append(cdav.PropFilter(target.upper()) + match)
else:
raise NotImplementedError("searching for %s not supported yet" % other)
if comp_filter and filters:
comp_filter += filters
vcalendar += comp_filter
elif comp_filter:
vcalendar += comp_filter
elif filters:
vcalendar += filters
filter = cdav.Filter() + vcalendar
root = cdav.CalendarQuery() + [prop, filter]
return (root, comp_class)
def freebusy_request(self, start, end):
"""
Search the calendar, but return only the free/busy information.
Parameters:
* start = datetime.today().
* end = same as above.
Returns:
* [FreeBusy(), ...]
"""
root = cdav.FreeBusyQuery() + [cdav.TimeRange(start, end)]
response = self._query(root, 1, "report")
return FreeBusy(self, response.raw)
def todos(
self, sort_keys=("due", "priority"), include_completed=False, sort_key=None
):
"""
fetches a list of todo events (refactored to a wrapper around search)
Parameters:
* sort_keys: use this field in the VTODO for sorting (iterable of
lower case string, i.e. ('priority','due')).
* include_completed: boolean -
by default, only pending tasks are listed
* sort_key: DEPRECATED, for backwards compatibility with version 0.4.
"""
if sort_key:
sort_keys = (sort_key,)
return self.search(
todo=True, include_completed=include_completed, sort_keys=sort_keys
)
def _calendar_comp_class_by_data(self, data):
"""
takes some data, either as icalendar text or icalender object (TODO:
consider vobject) and returns the appropriate
CalendarResourceObject child class.
"""
if data is None:
## no data received - we'd need to load it before we can know what
## class it really is. Assign the base class as for now.
return CalendarObjectResource
if hasattr(data, "split"):
for line in data.split("\n"):
line = line.strip()
if line == "BEGIN:VEVENT":
return Event
if line == "BEGIN:VTODO":
return Todo
if line == "BEGIN:VJOURNAL":
return Journal
if line == "BEGIN:VFREEBUSY":
return FreeBusy
elif hasattr(data, "subcomponents"):
if not len(data.subcomponents):
return CalendarObjectResource
ical2caldav = {
icalendar.Event: Event,
icalendar.Todo: Todo,
icalendar.Journal: Journal,
icalendar.FreeBusy: FreeBusy,
}
for sc in data.subcomponents:
if sc.__class__ in ical2caldav:
return ical2caldav[sc.__class__]
return CalendarObjectResource
def event_by_url(self, href, data=None):
"""
Returns the event with the given URL
"""
return Event(url=href, data=data, parent=self).load()
def object_by_uid(self, uid, comp_filter=None, comp_class=None):
"""
Get one event from the calendar.
Parameters:
* uid: the event uid
* comp_class: filter by component type (Event, Todo, Journal)
* comp_filter: for backward compatibility
Returns:
* Event() or None
"""
if comp_filter:
assert not comp_class
if hasattr(comp_filter, "attributes"):
comp_filter = comp_filter.attributes["name"]
if comp_filter == "VTODO":
comp_class = Todo
elif comp_filter == "VJOURNAL":
comp_class = Journal
elif comp_filter == "VEVENT":
comp_class = Event
else:
raise error.ConsistencyError("Wrong compfilter")
query = cdav.TextMatch(uid)
query = cdav.PropFilter("UID") + query
root, comp_class = self.build_search_xml_query(
comp_class=comp_class, filters=[query]
)
try:
items_found = self.search(root)
if not items_found:
raise error.NotFoundError("%s not found on server" % uid)
except Exception as err:
if comp_filter is not None:
raise
logging.warning(
"Error %s from server when doing an object_by_uid(%s). search without compfilter set is not compatible with all server implementations, trying event_by_uid + todo_by_uid + journal_by_uid instead"
% (str(err), uid)
)
items_found = []
for compfilter in ("VTODO", "VEVENT", "VJOURNAL"):
try:
items_found.append(
self.object_by_uid(uid, cdav.CompFilter(compfilter))
)
except error.NotFoundError:
pass
if len(items_found) >= 1:
if len(items_found) > 1:
logging.error(
"multiple items found with same UID. Returning the first one"
)
return items_found[0]
# Ref Lucas Verney, we've actually done a substring search, if the
# uid given in the query is short (i.e. just "0") we're likely to
# get false positives back from the server, we need to do an extra
# check that the uid is correct
items_found2 = []
for item in items_found:
## In v0.10.0 we used regexps here - it's probably more optimized,
## but at one point it broke due to an extra CR in the data.
## Usage of the icalendar library increases readability and
## reliability
if item.icalendar_component:
item_uid = item.icalendar_component.get("UID", None)
if item_uid and item_uid == uid:
items_found2.append(item)
if not items_found2:
raise error.NotFoundError("%s not found on server" % uid)
error.assert_(len(items_found2) == 1)
return items_found2[0]
def todo_by_uid(self, uid):
return self.object_by_uid(uid, comp_filter=cdav.CompFilter("VTODO"))
def event_by_uid(self, uid):
return self.object_by_uid(uid, comp_filter=cdav.CompFilter("VEVENT"))
def journal_by_uid(self, uid):
return self.object_by_uid(uid, comp_filter=cdav.CompFilter("VJOURNAL"))
# alias for backward compatibility
event = event_by_uid
def events(self):
"""
List all events from the calendar.
Returns:
* [Event(), ...]
"""
return self.search(comp_class=Event)
def objects_by_sync_token(self, sync_token=None, load_objects=False):
"""objects_by_sync_token aka objects
Do a sync-collection report, ref RFC 6578 and
https://github.com/python-caldav/caldav/issues/87
This method will return all objects in the calendar if no
sync_token is passed (the method should then be referred to as
"objects"), or if the sync_token is unknown to the server. If
a sync-token known by the server is passed, it will return
objects that are added, deleted or modified since last time
the sync-token was set.
If load_objects is set to True, the objects will be loaded -
otherwise empty CalendarObjectResource objects will be returned.
This method will return a SynchronizableCalendarObjectCollection object, which is
an iterable.
"""
cmd = dav.SyncCollection()
token = dav.SyncToken(value=sync_token)
level = dav.SyncLevel(value="1")
props = dav.Prop() + dav.GetEtag()
root = cmd + [level, token, props]
(response, objects) = self._request_report_build_resultlist(
root, props=[dav.GetEtag()], no_calendardata=True
)
## TODO: look more into this, I think sync_token should be directly available through response object
try:
sync_token = response.sync_token
except:
sync_token = response.tree.findall(".//" + dav.SyncToken.tag)[0].text
## this is not quite right - the etag we've fetched can already be outdated
if load_objects:
for obj in objects:
try:
obj.load()
except error.NotFoundError:
## The object was deleted
pass
return SynchronizableCalendarObjectCollection(
calendar=self, objects=objects, sync_token=sync_token
)
objects = objects_by_sync_token
def journals(self):
"""
List all journals from the calendar.
Returns:
* [Journal(), ...]
"""
return self.search(comp_class=Journal)
class ScheduleMailbox(Calendar):
"""
RFC6638 defines an inbox and an outbox for handling event scheduling.
TODO: As ScheduleMailboxes works a bit like calendars, I've chosen
to inheritate the Calendar class, but this is a bit incorrect, a
ScheduleMailbox is a collection, but not really a calendar. We
should create a common base class for ScheduleMailbox and Calendar
eventually.
"""
def __init__(self, client=None, principal=None, url=None):
"""
Will locate the mbox if no url is given
"""
super(ScheduleMailbox, self).__init__(client=client, url=url)
self._items = None
if not client and principal:
self.client = principal.client
if not principal and client:
principal = self.client.principal
if url is not None:
self.url = client.url.join(URL.objectify(url))
else:
self.url = principal.url
try:
self.url = self.client.url.join(URL(self.get_property(self.findprop())))
except:
logging.error("something bad happened", exc_info=True)
error.assert_(self.client.check_scheduling_support())
self.url = None
raise error.NotFoundError(
"principal has no %s. %s"
% (str(self.findprop()), error.ERR_FRAGMENT)
)
def get_items(self):
"""
TODO: work in progress
TODO: perhaps this belongs to the super class?
"""
if not self._items:
try:
self._items = self.objects(load_objects=True)
except:
logging.debug(
"caldav server does not seem to support a sync-token REPORT query on a scheduling mailbox"
)
error.assert_("google" in str(self.url))
self._items = [
CalendarObjectResource(url=x[0], client=self.client)
for x in self.children()
]
for x in self._items:
x.load()
else:
try:
self._items.sync()
except:
self._items = [
CalendarObjectResource(url=x[0], client=self.client)
for x in self.children()
]
for x in self._items:
x.load()
return self._items
## TODO: work in progress
# def get_invites():
# for item in self.get_items():
# if item.vobject_instance.vevent.
class ScheduleInbox(ScheduleMailbox):
findprop = cdav.ScheduleInboxURL
class ScheduleOutbox(ScheduleMailbox):
findprop = cdav.ScheduleOutboxURL
class SynchronizableCalendarObjectCollection(object):
"""
This class may hold a cached snapshot of a calendar, and changes
in the calendar can easily be copied over through the sync method.
To create a SynchronizableCalendarObjectCollection object, use
calendar.objects(load_objects=True)
"""
def __init__(self, calendar, objects, sync_token):
self.calendar = calendar
self.sync_token = sync_token
self.objects = objects
self._objects_by_url = None
def __iter__(self):
return self.objects.__iter__()
def __len__(self):
return len(self.objects)
def objects_by_url(self):
"""
returns a dict of the contents of the SynchronizableCalendarObjectCollection, URLs -> objects.
"""
if self._objects_by_url is None:
self._objects_by_url = {}
for obj in self:
self._objects_by_url[obj.url.canonical()] = obj
return self._objects_by_url
def sync(self):
"""
This method will contact the caldav server,
request all changes from it, and sync up the collection
"""
updated_objs = []
deleted_objs = []
updates = self.calendar.objects_by_sync_token(
self.sync_token, load_objects=False
)
obu = self.objects_by_url()
for obj in updates:
obj.url = obj.url.canonical()
if (
obj.url in obu
and dav.GetEtag.tag in obu[obj.url].props
and dav.GetEtag.tag in obj.props
):
if obu[obj.url].props[dav.GetEtag.tag] == obj.props[dav.GetEtag.tag]:
continue
obu[obj.url] = obj
try:
obj.load()
updated_objs.append(obj)
except error.NotFoundError:
deleted_objs.append(obj)
obu.pop(obj.url)
self.objects = obu.values()
self.sync_token = updates.sync_token
return (updated_objs, deleted_objs)
class CalendarObjectResource(DAVObject):
"""
Ref RFC 4791, section 4.1, a "Calendar Object Resource" can be an
event, a todo-item, a journal entry, or a free/busy entry
"""
RELTYPE_REVERSER: ClassVar = {
"PARENT": "CHILD",
"CHILD": "PARENT",
"SIBLING": "SIBLING",
}
_ENDPARAM = None
_vobject_instance = None
_icalendar_instance = None
_data = None
def __init__(
self, client=None, url=None, data=None, parent=None, id=None, props=None
):
"""
CalendarObjectResource has an additional parameter for its constructor:
* data = "...", vCal data for the event
"""
super(CalendarObjectResource, self).__init__(
client=client, url=url, parent=parent, id=id, props=props
)
if data is not None:
self.data = data
if id:
old_id = self.icalendar_component.pop("UID", None)
self.icalendar_component.add("UID", id)
def add_organizer(self):
"""
goes via self.client, finds the principal, figures out the right attendee-format and adds an
organizer line to the event
"""
principal = self.client.principal()
## TODO: remove Organizer-field, if exists
## TODO: what if walk returns more than one vevent?
self.icalendar_component.add("organizer", principal.get_vcal_address())
def split_expanded(self):
i = self.icalendar_instance.subcomponents
tz_ = [x for x in i if isinstance(x, icalendar.Timezone)]
ntz = [x for x in i if not isinstance(x, icalendar.Timezone)]
if len(ntz) == 1:
return [self]
if tz_:
error.assert_(len(tz_) == 1)
ret = []
for ical_obj in ntz:
obj = self.copy(keep_uid=True)
obj.icalendar_instance.subcomponents = []
if tz_:
obj.icalendar_instance.subcomponents.append(tz_[0])
obj.icalendar_instance.subcomponents.append(ical_obj)
ret.append(obj)
return ret
def expand_rrule(self, start, end):
"""This method will transform the calendar content of the
event and expand the calendar data from a "master copy" with
RRULE set and into a "recurrence set" with RECURRENCE-ID set
and no RRULE set. The main usage is for client-side expansion
in case the calendar server does not support server-side
expansion. It should be safe to save back to the server, the
server should recognize it as recurrences and should not edit
the "master copy". If doing a `self.load`, the calendar
content will be replaced with the "master copy". However, as
of 2022-10 there is no test code verifying this.
:param event: Event
:param start: datetime.datetime
:param end: datetime.datetime
"""
import recurring_ical_events
recurrings = recurring_ical_events.of(
self.icalendar_instance, components=["VJOURNAL", "VTODO", "VEVENT"]
).between(start, end)
recurrence_properties = ["exdate", "exrule", "rdate", "rrule"]
# FIXME too much copying
stripped_event = self.copy(keep_uid=True)
# remove all recurrence properties
for component in stripped_event.vobject_instance.components():
if component.name in ("VEVENT", "VTODO"):
for key in recurrence_properties:
try:
del component.contents[key]
except KeyError:
pass
calendar = self.icalendar_instance
calendar.subcomponents = []
for occurrence in recurrings:
occurrence.add("RECURRENCE-ID", occurrence.get("DTSTART"))
calendar.add_component(occurrence)
# add other components (except for the VEVENT itself and VTIMEZONE which is not allowed on occurrence events)
for component in stripped_event.icalendar_instance.subcomponents:
if component.name not in ("VEVENT", "VTODO", "VTIMEZONE"):
calendar.add_component(component)
def set_relation(
self, other, reltype=None, set_reverse=True
): ## TODO: logic to find and set siblings?
"""
Sets a relation between this object and another object (given by uid or object).
"""
##TODO: test coverage
reltype = reltype.upper()
if isinstance(other, CalendarObjectResource):
if other.id:
uid = other.id
else:
uid = other.icalendar_component["uid"]
else:
uid = other
if set_reverse:
other = self.parent.object_by_uid(uid)
if set_reverse:
reltype_reverse = self.RELTYPE_REVERSER[reltype]
other.set_relation(other=self, reltype=reltype_reverse, set_reverse=False)
existing_relation = self.icalendar_component.get("related-to", None)
existing_relations = (
existing_relation
if isinstance(existing_relation, list)
else [existing_relation]
)
for rel in existing_relations:
if rel == uid:
return
# without str(…), icalendar ignores properties
# because if type(uid) == vText
# then Component._encode does miss adding properties
# see https://github.com/collective/icalendar/issues/557
# workaround should be safe to remove if issue gets fixed
uid = str(uid)
self.icalendar_component.add(
"related-to", uid, parameters={"RELTYPE": reltype}, encode=True
)
self.save()
## TODO: this method is undertested in the caldav library.
## However, as this consolidated and eliminated quite some duplicated code in the
## plann project, it is extensively tested in plann.
def get_relatives(
self, reltypes=None, relfilter=None, fetch_objects=True, ignore_missing=True
):
"""
By default, loads all objects pointed to by the RELATED-TO
property and loads the related objects.
It's possible to filter, either by passing a set or a list of
acceptable relation types in reltypes, or by passing a lambda
function in relfilter.
TODO: Make it possible to also check up reverse relationships
TODO: this is partially overlapped by plann.lib._relships_by_type
in the plann tool. Should consolidate the code.
"""
ret = defaultdict(set)
relations = self.icalendar_component.get("RELATED-TO", [])
if not isinstance(relations, list):
relations = [relations]
for rel in relations:
if relfilter and not relfilter(rel):
continue
reltype = rel.params.get("RELTYPE", "PARENT")
if reltypes and not reltype in reltypes:
continue
ret[reltype].add(str(rel))
if fetch_objects:
for reltype in ret:
uids = ret[reltype]
ret[reltype] = []
for obj in uids:
try:
ret[reltype].append(self.parent.object_by_uid(obj))
except error.NotFoundError:
if not ignore_missing:
raise
return ret
def _get_icalendar_component(self, assert_one=False):
"""Returns the icalendar subcomponent - which should be an
Event, Journal, Todo or FreeBusy from the icalendar class
See also https://github.com/python-caldav/caldav/issues/232
"""
self.load(only_if_unloaded=True)
ret = [
x
for x in self.icalendar_instance.subcomponents
if not isinstance(x, icalendar.Timezone)
]
error.assert_(len(ret) == 1 or not assert_one)
for x in ret:
for cl in (
icalendar.Event,
icalendar.Journal,
icalendar.Todo,
icalendar.FreeBusy,
):
if isinstance(x, cl):
return x
error.assert_(False)
def _set_icalendar_component(self, value):
s = self.icalendar_instance.subcomponents
i = [i for i in range(0, len(s)) if not isinstance(s[i], icalendar.Timezone)]
if len(i) == 1:
self.icalendar_instance.subcomponents[i[0]] = value
else:
my_instance = icalendar.Calendar()
my_instance.add("prodid", "-//python-caldav//caldav//" + language)
my_instance.add("version", "2.0")
my_instance.add_component(value)
self.icalendar_instance = my_instance
icalendar_component = property(
_get_icalendar_component,
_set_icalendar_component,
doc="icalendar component - should not be used with recurrence sets",
)
def get_due(self):
"""
A VTODO may have due or duration set. Return or calculate due.
WARNING: this method is likely to be deprecated and moved to
the icalendar library. If you decide to use it, please put
caldav<2.0 in the requirements.
"""
i = self.icalendar_component
if "DUE" in i:
return i["DUE"].dt
elif "DTEND" in i:
return i["DTEND"].dt
elif "DURATION" in i and "DTSTART" in i:
return i["DTSTART"].dt + i["DURATION"].dt
else:
return None
get_dtend = get_due
def add_attendee(self, attendee, no_default_parameters=False, **parameters):
"""
For the current (event/todo/journal), add an attendee.
The attendee can be any of the following:
* A principal
* An email address prepended with "mailto:"
* An email address without the "mailto:"-prefix
* A two-item tuple containing a common name and an email address
* (not supported, but planned: an ical text line starting with the word "ATTENDEE")
Any number of attendee parameters can be given, those will be used
as defaults unless no_default_parameters is set to True:
partstat=NEEDS-ACTION
cutype=UNKNOWN (unless a principal object is given)
rsvp=TRUE
role=REQ-PARTICIPANT
schedule-agent is not set
"""
from icalendar import vCalAddress, vText
if isinstance(attendee, Principal):
attendee_obj = attendee.get_vcal_address()
elif isinstance(attendee, vCalAddress):
attendee_obj = attendee
elif isinstance(attendee, tuple):
if attendee[1].startswith("mailto:"):
attendee_obj = vCalAddress(attendee[1])
else:
attendee_obj = vCalAddress("mailto:" + attendee[1])
attendee_obj.params["cn"] = vText(attendee[0])
elif isinstance(attendee, str):
if attendee.startswith("ATTENDEE"):
raise NotImplementedError(
"do we need to support this anyway? Should be trivial, but can't figure out how to do it with the icalendar.Event/vCalAddress objects right now"
)
elif attendee.startswith("mailto:"):
attendee_obj = vCalAddress(attendee)
elif "@" in attendee and not ":" in attendee and not ";" in attendee:
attendee_obj = vCalAddress("mailto:" + attendee)
else:
error.assert_(False)
attendee_obj = vCalAddress()
## TODO: if possible, check that the attendee exists
## TODO: check that the attendee will not be duplicated in the event.
if not no_default_parameters:
## Sensible defaults:
attendee_obj.params["partstat"] = "NEEDS-ACTION"
if not "cutype" in attendee_obj.params:
attendee_obj.params["cutype"] = "UNKNOWN"
attendee_obj.params["rsvp"] = "TRUE"
attendee_obj.params["role"] = "REQ-PARTICIPANT"
params = {}
for key in parameters:
new_key = key.replace("_", "-")
if parameters[key] == True:
params[new_key] = "TRUE"
else:
params[new_key] = parameters[key]
attendee_obj.params.update(params)
ievent = self.icalendar_component
ievent.add("attendee", attendee_obj)
def is_invite_request(self):
self.load(only_if_unloaded=True)
return self.icalendar_instance.get("method", None) == "REQUEST"
def accept_invite(self, calendar=None):
self._reply_to_invite_request("ACCEPTED", calendar)
def decline_invite(self, calendar=None):
self._reply_to_invite_request("DECLINED", calendar)
def tentatively_accept_invite(self, calendar=None):
self._reply_to_invite_request("TENTATIVE", calendar)
## TODO: DELEGATED is also a valid option, and for vtodos the
## partstat can also be set to COMPLETED and IN-PROGRESS.
def _reply_to_invite_request(self, partstat, calendar):
error.assert_(self.is_invite_request())
if not calendar:
calendar = self.client.principal().calendars()[0]
## we need to modify the icalendar code, update our own participant status
self.icalendar_instance.pop("METHOD")
self.change_attendee_status(partstat=partstat)
self.get_property(cdav.ScheduleTag(), use_cached=True)
try:
calendar.save_event(self.data)
except Exception as some_exception:
## TODO - TODO - TODO
## RFC6638 does not seem to be very clear (or
## perhaps I should read it more thoroughly) neither on
## how to handle conflicts, nor if the reply should be
## posted to the "outbox", saved back to the same url or
## sent to a calendar.
self.load()
self.get_property(cdav.ScheduleTag(), use_cached=False)
outbox = self.client.principal().schedule_outbox()
if calendar != outbox:
self._reply_to_invite_request(partstat, calendar=outbox)
else:
self.save()
def copy(self, keep_uid=False, new_parent=None):
"""
Events, todos etc can be copied within the same calendar, to another
calendar or even to another caldav server
"""
obj = self.__class__(
parent=new_parent or self.parent,
data=self.data,
id=self.id if keep_uid else str(uuid.uuid1()),
)
if new_parent or not keep_uid:
obj.url = obj.generate_url()
else:
obj.url = self.url
return obj
def load(self, only_if_unloaded=False):
"""
(Re)load the object from the caldav server.
"""
if only_if_unloaded and self.is_loaded():
return
r = self.client.request(self.url)
if r.status == 404:
raise error.NotFoundError(errmsg(r))
self.data = vcal.fix(r.raw)
if "Etag" in r.headers:
self.props[dav.GetEtag.tag] = r.headers["Etag"]
if "Schedule-Tag" in r.headers:
self.props[cdav.ScheduleTag.tag] = r.headers["Schedule-Tag"]
return self
## TODO: self.id should either always be available or never
def _find_id_path(self, id=None, path=None):
"""
With CalDAV, every object has a URL. With icalendar, every object
should have a UID. This UID may or may not be copied into self.id.
This method will:
0) if ID is given, assume that as the UID, and set it in the object
1) if UID is given in the object, assume that as the ID
2) if ID is not given, but the path is given, generate the ID from the
path
3) If neither ID nor path is given, use the uuid method to generate an
ID (TODO: recommendation is to concat some timestamp, serial or
random number and a domain)
4) if no path is given, generate the URL from the ID
"""
i = self._get_icalendar_component(assert_one=False)
if not id and getattr(self, "id", None):
id = self.id
if not id:
id = i.pop("UID", None)
if id:
id = str(id)
if not path and getattr(self, "path", None):
path = self.path
if id is None and path is not None and str(path).endswith(".ics"):
id = re.search("(/|^)([^/]*).ics", str(path)).group(2)
if id is None:
id = str(uuid.uuid1())
i.pop("UID", None)
i.add("UID", id)
self.id = id
for x in self.icalendar_instance.subcomponents:
if not isinstance(x, icalendar.Timezone):
error.assert_(x.get("UID", None) == self.id)
if path is None:
path = self.generate_url()
else:
path = self.parent.url.join(path)
self.url = URL.objectify(path)
def _put(self, retry_on_failure=True):
## SECURITY TODO: we should probably have a check here to verify that no such object exists already
r = self.client.put(
self.url, self.data, {"Content-Type": 'text/calendar; charset="utf-8"'}
)
if r.status == 302:
path = [x[1] for x in r.headers if x[0] == "location"][0]
elif not (r.status in (204, 201)):
if retry_on_failure:
## This looks like a noop, but the object may be "cleaned".
## See https://github.com/python-caldav/caldav/issues/43
self.vobject_instance
return self._put(False)
else:
raise error.PutError(errmsg(r))
def _create(self, id=None, path=None, retry_on_failure=True):
## We're efficiently running the icalendar code through the icalendar
## library. This may cause data modifications and may "unfix"
## https://github.com/python-caldav/caldav/issues/43
self._find_id_path(id=id, path=path)
self._put()
def generate_url(self):
## See https://github.com/python-caldav/caldav/issues/143 for the rationale behind double-quoting slashes
## TODO: should try to wrap my head around issues that arises when id contains weird characters. maybe it's
## better to generate a new uuid here, particularly if id is in some unexpected format.
if not self.id:
self.id = self._get_icalendar_component(assert_one=False)["UID"]
return self.parent.url.join(quote(self.id.replace("/", "%2F")) + ".ics")
def change_attendee_status(self, attendee=None, **kwargs):
if not attendee:
attendee = self.client.principal()
cnt = 0
if isinstance(attendee, Principal):
for addr in attendee.calendar_user_address_set():
try:
self.change_attendee_status(addr, **kwargs)
## TODO: can probably just return now
cnt += 1
except error.NotFoundError:
pass
if not cnt:
raise error.NotFoundError(
"Principal %s is not invited to event" % str(attendee)
)
error.assert_(cnt == 1)
return
ical_obj = self.icalendar_component
attendee_lines = ical_obj["attendee"]
if isinstance(attendee_lines, str):
attendee_lines = [attendee_lines]
strip_mailto = lambda x: str(x).replace("mailto:", "").lower()
for attendee_line in attendee_lines:
if strip_mailto(attendee_line) == strip_mailto(attendee):
attendee_line.params.update(kwargs)
cnt += 1
if not cnt:
raise error.NotFoundError("Participant %s not found in attendee list")
error.assert_(cnt == 1)
def save(
self,
no_overwrite=False,
no_create=False,
obj_type=None,
increase_seqno=True,
if_schedule_tag_match=False,
):
"""
Save the object, can be used for creation and update.
no_overwrite and no_create will check if the object exists.
Those two are mutually exclusive. Some servers don't support
searching for an object uid without explicitly specifying what
kind of object it should be, hence obj_type can be passed.
obj_type is only used in conjunction with no_overwrite and
no_create.
Returns:
* self
"""
if (
self._vobject_instance is None
and self._data is None
and self._icalendar_instance is None
):
return self
path = self.url.path if self.url else None
if no_overwrite or no_create:
## SECURITY TODO: path names on the server does not
## necessarily map cleanly to UUIDs. We need to do quite
## some refactoring here to ensure all corner cases are
## covered. Doing a GET first to check if the resource is
## found and then a PUT also gives a potential race
## condition. (Possibly the API gives no safe way to ensure
## a unique new calendar item is created to the server without
## overwriting old stuff or vice versa - it seems silly to me
## to do a PUT instead of POST when creating new data).
## TODO: the "find id"-logic is duplicated in _create,
## should be refactored
if not self.id:
for component in self.vobject_instance.getChildren():
if hasattr(component, "uid"):
self.id = component.uid.value
if not self.id and no_create:
raise error.ConsistencyError("no_create flag was set, but no ID given")
existing = None
## some servers require one to explicitly search for the right kind of object.
## todo: would arguably be nicer to verify the type of the object and take it from there
if not self.id:
methods = []
elif obj_type:
methods = (getattr(self.parent, "%s_by_uid" % obj_type),)
else:
methods = (
self.parent.object_by_uid,
self.parent.event_by_uid,
self.parent.todo_by_uid,
self.parent.journal_by_uid,
)
for method in methods:
try:
existing = method(self.id)
if no_overwrite:
raise error.ConsistencyError(
"no_overwrite flag was set, but object already exists"
)
break
except error.NotFoundError:
pass
if no_create and not existing:
raise error.ConsistencyError(
"no_create flag was set, but object does not exists"
)
if increase_seqno and b"SEQUENCE" in to_wire(self.data):
seqno = self.icalendar_component.pop("SEQUENCE", None)
if seqno is not None:
self.icalendar_component.add("SEQUENCE", seqno + 1)
self._create(id=self.id, path=path)
return self
def is_loaded(self):
return (
self._data or self._vobject_instance or self._icalendar_instance
) and self.data.count("BEGIN:") > 1
def __str__(self):
return "%s: %s" % (self.__class__.__name__, self.url)
## implementation of the properties self.data,
## self.vobject_instance and self.icalendar_instance follows. The
## rule is that only one of them can be set at any time, this
## since vobject_instance and icalendar_instance are mutable,
## and any modification to those instances should apply
def _set_data(self, data):
## The __init__ takes a data attribute, and it should be allowable to
## set it to a vobject object or an icalendar object, hence we should
## do type checking on the data (TODO: but should probably use
## isinstance rather than this kind of logic
if type(data).__module__.startswith("vobject"):
self._set_vobject_instance(data)
return self
if type(data).__module__.startswith("icalendar"):
self._set_icalendar_instance(data)
return self
self._data = vcal.fix(data)
self._vobject_instance = None
self._icalendar_instance = None
return self
def _get_data(self):
if self._data:
return to_normal_str(self._data)
elif self._vobject_instance:
return to_normal_str(self._vobject_instance.serialize())
elif self._icalendar_instance:
return to_normal_str(self._icalendar_instance.to_ical())
return None
def _get_wire_data(self):
if self._data:
return to_wire(self._data)
elif self._vobject_instance:
return to_wire(self._vobject_instance.serialize())
elif self._icalendar_instance:
return to_wire(self._icalendar_instance.to_ical())
return None
data = property(
_get_data, _set_data, doc="vCal representation of the object as normal string"
)
wire_data = property(
_get_wire_data,
_set_data,
doc="vCal representation of the object in wire format (UTF-8, CRLN)",
)
def _set_vobject_instance(self, inst):
self._vobject_instance = inst
self._data = None
self._icalendar_instance = None
return self
def _get_vobject_instance(self):
if not self._vobject_instance:
if self._get_data() is None:
return None
try:
self._set_vobject_instance(
vobject.readOne(to_unicode(self._get_data()))
)
except:
log.critical(
"Something went wrong while loading icalendar data into the vobject class. ical url: "
+ str(self.url)
)
raise
return self._vobject_instance
vobject_instance = property(
_get_vobject_instance,
_set_vobject_instance,
doc="vobject instance of the object",
)
def _set_icalendar_instance(self, inst):
self._icalendar_instance = inst
self._data = None
self._vobject_instance = None
return self
def _get_icalendar_instance(self):
if not self._icalendar_instance:
if not self.data:
return None
self.icalendar_instance = icalendar.Calendar.from_ical(
to_unicode(self.data)
)
return self._icalendar_instance
icalendar_instance = property(
_get_icalendar_instance,
_set_icalendar_instance,
doc="icalendar instance of the object",
)
def get_duration(self):
"""According to the RFC, either DURATION or DUE should be set
for a task, but never both - implicitly meaning that DURATION
is the difference between DTSTART and DUE (personally I
believe that's stupid. If a task takes five minutes to
complete - say, fill in some simple form that should be
delivered before midnight at new years eve, then it feels
natural for me to define "duration" as five minutes, DTSTART
to "some days before new years eve" and DUE to 20xx-01-01
00:00:00 - but I digress.
This method will return DURATION if set, otherwise the
difference between DUE and DTSTART (if both of them are set).
TODO: should be fixed for Event class as well (only difference
is that DTEND is used rather than DUE) and possibly also for
Journal (defaults to one day, probably?)
WARNING: this method is likely to be deprecated and moved to
the icalendar library. If you decide to use it, please put
caldav<2.0 in the requirements.
"""
i = self.icalendar_component
return self._get_duration(i)
def _get_duration(self, i):
if "DURATION" in i:
return i["DURATION"].dt
elif "DTSTART" in i and self._ENDPARAM in i:
return i[self._ENDPARAM].dt - i["DTSTART"].dt
elif "DTSTART" in i and not isinstance(i["DTSTART"], datetime):
return timedelta(days=1)
else:
return timedelta(0)
## for backward-compatibility - may be changed to
## icalendar_instance in version 1.0
instance = vobject_instance
class Event(CalendarObjectResource):
"""
The `Event` object is used to represent an event (VEVENT).
As of 2020-12 it adds nothing to the inheritated class. (I have
frequently asked myself if we need those subclasses ... perhaps
not)
"""
_ENDPARAM = "DTEND"
pass
class Journal(CalendarObjectResource):
"""
The `Journal` object is used to represent a journal entry (VJOURNAL).
As of 2020-12 it adds nothing to the inheritated class. (I have
frequently asked myself if we need those subclasses ... perhaps
not)
"""
pass
class FreeBusy(CalendarObjectResource):
"""
The `FreeBusy` object is used to represent a freebusy response from
the server. __init__ is overridden, as a FreeBusy response has no
URL or ID. The inheritated methods .save and .load is moot and
will probably throw errors (perhaps the class hierarchy should be
rethought, to prevent the FreeBusy from inheritating moot methods)
Update: With RFC6638 a freebusy object can have a URL and an ID.
"""
def __init__(self, parent, data, url=None, id=None):
CalendarObjectResource.__init__(
self, client=parent.client, url=url, data=data, parent=parent, id=id
)
class Todo(CalendarObjectResource):
"""The `Todo` object is used to represent a todo item (VTODO). A
Todo-object can be completed. Extra logic for different ways to
complete one recurrence of a recurrent todo. Extra logic to
handle due vs duration.
"""
_ENDPARAM = "DUE"
def _next(self, ts=None, i=None, dtstart=None, rrule=None, by=None, no_count=True):
"""Special logic to fint the next DTSTART of a recurring
just-completed task.
If any BY*-parameters are present, assume the task should have
fixed deadlines and preserve information from the previous
dtstart. If no BY*-parameters are present, assume the
frequency is meant to be the interval between the tasks.
Examples:
1) Garbage collection happens every week on a Tuesday, but
never earlier than 09 in the morning. Hence, it may be
important to take out the thrash Monday evenings or Tuesday
morning. DTSTART of the original task is set to Tuesday
2022-11-01T08:50, DUE to 09:00.
1A) Task is completed 07:50 on the 1st of November. Next
DTSTART should be Tuesday the 7th of November at 08:50.
1B) Task is completed 09:15 on the 1st of November (which is
probably OK, since they usually don't come before 09:30).
Next DTSTART should be Tuesday the 7th of November at 08:50.
1C) Task is completed at the 5th of November. We've lost the
DUE, but the calendar has no idea weather the DUE was a very
hard due or not - and anyway, probably we'd like to do it
again on Tuesday, so next DTSTART should be Tuesday the 7th of
November at 08:50.
1D) Task is completed at the 7th of November at 07:50. Next
DTSTART should be one hour later. Now, this is very silly,
but an algorithm cannot do guesswork on weather it's silly or
not. If DTSTART would be set to the earliest possible time
one could start thinking on this task (like, Monday evening),
then we would get Tue the 14th of November, which does make
sense. Unfortunately the icalendar standard does not specify
what should be used for DTSTART and DURATION/DUE.
1E) Task is completed on the 7th of November at 08:55. This
efficiently means we've lost the 1st of November recurrence
but have done the 7th of November recurrence instead, so next
timestamp will be the 14th of November.
2) Floors at home should be cleaned like once a week, but
there is no fixed deadline for it. For some people it may
make sense to have a routine doing it i.e. every Tuesday, but
this is not a strict requirement. If it wasn't done one
Tuesday, it's probably even more important to do it Wednesday.
If the floor was cleaned on a Saturday, it probably doesn't
make sense cleaning it again on Tuesday, but it probably
shouldn't wait until next Tuesday. Rrule is set to
FREQ=WEEKLY, but without any BYDAY. The original VTODO is set
up with DTSTART 16:00 on Tuesday the 1st of November and DUE
17:00. After 17:00 there will be dinner, so best to get it
done before that.
2A) Floor cleaning was finished 14:30. The next recurrence
has DTSTART set to 13:30 (and DUE set to 14:30). The idea
here is that since the floor starts accumulating dirt right
after 14:30, obviously it is overdue at 16:00 Tuesday the 7th.
2B) Floor cleaning was procrastinated with one day and
finished Wednesday at 14:30. Next instance will be Wednesday
in a week, at 14:30.
2C) Floor cleaning was procrastinated with two weeks and
finished Tuesday the 14th at 14:30. Next instance will be
Tuesday the 21st at 14:30.
While scenario 2 is the most trivial to implement, it may not
be the correct understanding of the RFC, and it may be tricky
to get the RECURRENCE-ID set correctly.
"""
if not i:
i = self.icalendar_component
if not rrule:
rrule = i["RRULE"]
if not dtstart:
if by is True or (
by is None and any((x for x in rrule if x.startswith("BY")))
):
if "DTSTART" in i:
dtstart = i["DTSTART"].dt
else:
dtstart = ts or datetime.now()
else:
dtstart = ts or datetime.now() - self._get_duration(i)
## dtstart should be compared to the completion timestamp, which
## is set in UTC in the complete() method. However, dtstart
## may be a naïve or a floating timestamp
## (TODO: what if it's a date?)
## (TODO: we need test code for those corner cases!)
if hasattr(dtstart, "astimezone"):
dtstart = dtstart.astimezone(timezone.utc)
if not ts:
ts = dtstart
## Counting is taken care of other places
if no_count and "COUNT" in rrule:
rrule = rrule.copy()
rrule.pop("COUNT")
rrule = rrulestr(rrule.to_ical().decode("utf-8"), dtstart=dtstart)
return rrule.after(ts)
def _reduce_count(self, i=None):
if not i:
i = self.icalendar_component
if "COUNT" in i["RRULE"]:
if i["RRULE"]["COUNT"][0] == 1:
return False
i["RRULE"]["COUNT"][0] -= 1
return True
def _complete_recurring_safe(self, completion_timestamp):
"""This mode will create a new independent task which is
marked as completed, and modify the existing recurring task.
It is probably the most safe way to handle the completion of a
recurrence of a recurring task, though the link between the
completed task and the original task is lost.
"""
## If count is one, then it is not really recurring
if not self._reduce_count():
return self.complete(handle_rrule=False)
next_dtstart = self._next(completion_timestamp)
if not next_dtstart:
return self.complete(handle_rrule=False)
completed = self.copy()
completed.url = self.parent.url.join(completed.id + ".ics")
completed.icalendar_component.pop("RRULE")
completed.save()
completed.complete()
duration = self.get_duration()
i = self.icalendar_component
i.pop("DTSTART", None)
i.add("DTSTART", next_dtstart)
self.set_duration(duration, movable_attr="DUE")
self.save()
def _complete_recurring_thisandfuture(self, completion_timestamp):
"""The RFC is not much helpful, a lot of guesswork is needed
to consider what the "right thing" to do wrg of a completion of
recurring tasks is ... but this is my shot at it.
1) The original, with rrule, will be kept as it is. The rrule
string is fetched from the first subcomponent of the
icalendar.
2) If there are multiple recurrence instances in subcomponents
and the last one is marked with RANGE=THISANDFUTURE, then
select this one. If it has the rrule property set, use this
rrule rather than the original one. Drop the RANGE parameter.
Calculate the next RECURRENCE-ID from the DTSTART of this
object. Mark task as completed. Increase SEQUENCE.
3) Create a new recurrence instance with RANGE=THISANDFUTURE,
without RRULE set (Ref
https://github.com/Kozea/Radicale/issues/1264). Set the
RECURRENCE-ID to the one calculated in #2. Calculate the
DTSTART based on rrule and completion timestamp/date.
"""
recurrences = self.icalendar_instance.subcomponents
orig = recurrences[0]
if not "STATUS" in orig:
orig["STATUS"] = "NEEDS-ACTION"
if len(recurrences) == 1:
## We copy the original one
just_completed = orig.copy()
just_completed.pop("RRULE")
just_completed.add(
"RECURRENCE-ID", orig.get("DTSTART", completion_timestamp)
)
seqno = just_completed.pop("SEQUENCE", 0)
just_completed.add("SEQUENCE", seqno + 1)
recurrences.append(just_completed)
prev = recurrences[-1]
rrule = prev.get("RRULE", orig["RRULE"])
thisandfuture = prev.copy()
seqno = thisandfuture.pop("SEQUENCE", 0)
thisandfuture.add("SEQUENCE", seqno + 1)
## If we have multiple recurrences, assume the last one is a THISANDFUTURE.
## (Otherwise, the data is coming from another client ...)
## The RANGE parameter needs to be removed
if len(recurrences) > 2:
if prev["RECURRENCE-ID"].params.get("RANGE", None) == "THISANDFUTURE":
prev["RECURRENCE-ID"].params.pop("RANGE")
else:
raise NotImplementedError(
"multiple instances found, but last one is not of type THISANDFUTURE, possibly this has been created by some incompatible client, but we should deal with it"
)
self._complete_ical(prev, completion_timestamp)
thisandfuture.pop("RECURRENCE-ID", None)
thisandfuture.add("RECURRENCE-ID", self._next(i=prev, rrule=rrule))
thisandfuture["RECURRENCE-ID"].params["RANGE"] = "THISANDFUTURE"
rrule2 = thisandfuture.pop("RRULE", None)
## Counting logic
if rrule2 is not None:
count = rrule2.get("COUNT", None)
if count is not None and count[0] in (0, 1):
for i in recurrences:
self._complete_ical(i, completion_timestamp=completion_timestamp)
thisandfuture.add("RRULE", rrule2)
else:
count = rrule.get("COUNT", None)
if count is not None and count[0] <= len(
[x for x in recurrences if not self._is_pending(x)]
):
self._complete_ical(
recurrences[0], completion_timestamp=completion_timestamp
)
self.save(increase_seqno=False)
return
rrule = rrule2 or rrule
duration = self._get_duration(i=prev)
thisandfuture.pop("DTSTART", None)
thisandfuture.pop("DUE", None)
next_dtstart = self._next(i=prev, rrule=rrule, ts=completion_timestamp)
thisandfuture.add("DTSTART", next_dtstart)
self._set_duration(i=thisandfuture, duration=duration, movable_attr="DUE")
self.icalendar_instance.subcomponents.append(thisandfuture)
self.save(increase_seqno=False)
def complete(
self, completion_timestamp=None, handle_rrule=False, rrule_mode="safe"
):
"""Marks the task as completed.
Parameters:
* completion_timestamp - datetime object. Defaults to
datetime.now().
* handle_rrule - if set to True, the library will try to be smart if
the task is recurring. The default is False, for backward
compatibility. I may consider making this one mandatory.
* rrule_mode - The RFC leaves a lot of room for interpretation on how
to handle recurring tasks, and what works on one server may break at
another. The following modes are accepted:
* this_and_future - see doc for _complete_recurring_thisandfuture for details
* safe - see doc for _complete_recurring_safe for details
"""
if not completion_timestamp:
completion_timestamp = datetime.utcnow().astimezone(timezone.utc)
if "RRULE" in self.icalendar_component and handle_rrule:
return getattr(self, "_complete_recurring_%s" % rrule_mode)(
completion_timestamp
)
self._complete_ical(completion_timestamp=completion_timestamp)
self.save()
def _complete_ical(self, i=None, completion_timestamp=None):
## my idea was to let self.complete call this one ... but self.complete
## should use vobject and not icalendar library due to backward compatibility.
if i is None:
i = self.icalendar_component
assert self._is_pending(i)
status = i.pop("STATUS", None)
i.add("STATUS", "COMPLETED")
i.add("COMPLETED", completion_timestamp)
def _is_pending(self, i=None):
if i is None:
i = self.icalendar_component
if i.get("COMPLETED", None) is not None:
return False
if i.get("STATUS", None) in ("NEEDS-ACTION", "IN-PROCESS"):
return True
if i.get("STATUS", None) in ("CANCELLED", "COMPLETED"):
return False
if not "STATUS" in i:
return True
## input data does not conform to the RFC
assert False
def uncomplete(self):
"""Undo completion - marks a completed task as not completed"""
### TODO: needs test code for code coverage!
## (it has been tested through the calendar-cli test code)
if not hasattr(self.vobject_instance.vtodo, "status"):
self.vobject_instance.vtodo.add("status")
self.vobject_instance.vtodo.status.value = "NEEDS-ACTION"
if hasattr(self.vobject_instance.vtodo, "completed"):
self.vobject_instance.vtodo.remove(self.vobject_instance.vtodo.completed)
self.save()
## TODO: should be moved up to the base class
def set_duration(self, duration, movable_attr="DTSTART"):
"""
If DTSTART and DUE/DTEND is already set, one of them should be moved. Which one? I believe that for EVENTS, the DTSTART should remain constant and DTEND should be moved, but for a task, I think the due date may be a hard deadline, hence by default we'll move DTSTART.
TODO: can this be written in a better/shorter way?
WARNING: this method is likely to be deprecated and moved to
the icalendar library. If you decide to use it, please put
caldav<2.0 in the requirements.
"""
i = self.icalendar_component
return self._set_duration(i, duration, movable_attr)
def _set_duration(self, i, duration, movable_attr="DTSTART"):
if ("DUE" in i or "DURATION" in i) and "DTSTART" in i:
i.pop(movable_attr, None)
if movable_attr == "DUE":
i.pop("DURATION", None)
if movable_attr == "DTSTART":
i.add("DTSTART", i["DUE"].dt - duration)
elif movable_attr == "DUE":
i.add("DUE", i["DTSTART"].dt + duration)
elif "DUE" in i:
i.add("DTSTART", i["DUE"].dt - duration)
elif "DTSTART" in i:
i.add("DUE", i["DTSTART"].dt + duration)
else:
if "DURATION" in i:
i.pop("DURATION")
i.add("DURATION", duration)
def set_due(self, due, move_dtstart=False, check_dependent=False):
"""The RFC specifies that a VTODO cannot have both due and
duration, so when setting due, the duration field must be
evicted
check_dependent=True will raise some error if there exists a
parent calendar component (through RELATED-TO), and the parents
due or dtend is before the new dtend).
WARNING: this method is likely to be deprecated and parts of
it moved to the icalendar library. If you decide to use it,
please put caldav<2.0 in the requirements.
WARNING: the check_dependent-logic may be rewritten to support
RFC9253 in 1.x already
"""
if hasattr(due, "tzinfo") and not due.tzinfo:
due = due.astimezone(timezone.utc)
i = self.icalendar_component
if check_dependent:
parents = self.get_relatives({"PARENT"})
for parent in parents["PARENT"]:
pend = parent.get_dtend()
if pend and pend.astimezone(timezone.utc) < due:
if check_dependent == "return":
return parent
raise error.ConsistencyError(
"parent object has due/end %s, cannot procrastinate child object without first procrastinating parent object"
)
duration = self.get_duration()
i.pop("DURATION", None)
i.pop("DUE", None)
if move_dtstart and duration and "DTSTART" in i:
i.pop("DTSTART")
i.add("DTSTART", due - duration)
i.add("DUE", due)
|