1
Nextcloud Calendar: Create a calendar event
One script reply has been approved by the moderators Verified

Create an event in a calendar in Nextcloud calendar.

Created by marcel klehr12 179 days ago Viewed 368 times
0
Submitted by marcel klehr12 Python3
Verified 179 days ago
1
import caldav
2
import base64
3
import datetime
4

5
# You can import any PyPi package.
6
# See here for more info: https://www.windmill.dev/docs/advanced/dependencies_in_python
7

8
# you can use typed resources by doing a type alias to dict
9
nextcloud = dict
10
datetime = dict
11

12

13
def main(
14
    NextcloudResource: nextcloud,
15
    userId: str,
16
    calendarName: str,
17
    event_start: datetime,
18
    event_end: datetime,
19
    useAppApiAuth: bool = False,
20
):
21
    if useAppApiAuth:
22
        headers = {
23
            "AA-VERSION": "2.3.0",
24
            "EX-APP-ID": "flow",
25
            "EX-APP-VERSION": "1.0.0",
26
            "AUTHORIZATION-APP-API": base64.b64encode(
27
                f"{userId}:{NextcloudResource['password']}".encode("utf-8")
28
            ).decode("utf-8"),
29
        }
30
    else:
31
        headers = {}
32

33
    with caldav.DAVClient(
34
        url=NextcloudResource["baseUrl"] + "/remote.php/dav/calendars/" + userId + "/",
35
        username=userId,
36
        password=NextcloudResource["password"],
37
        headers=headers,
38
    ) as client:
39
        my_principal = client.principal()
40
        calendar = next(
41
            filter(
42
                lambda calendar: calendar.name == calendarName, my_principal.calendars()
43
            )
44
        )
45
        if calendar is None:
46
            raise ValueError("Could not find calendar by the name you provided")
47

48
        my_event = calendar.save_event(
49
            dtstart=event_start,
50
            dtend=event_end,
51
            summary="Do the needful",
52
        )
53
        return my_event
54