Nextcloud Calendar: Create a calendar event

Create an event in a calendar in Nextcloud calendar.

Script nextcloud Verified

by marcel klehr12 ยท 7/23/2024

The script

Submitted by nextcloud Python3
Verified 77 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
    Nextcloud: nextcloud,
15
    calendarName: str,
16
    event_start: datetime,
17
    event_end: datetime,
18
):
19
    headers = {}
20

21
    with caldav.DAVClient(
22
        url=Nextcloud["baseUrl"] + "/remote.php/dav/calendars/" + Nextcloud["userId"] + "/",
23
        username=Nextcloud["userId"],
24
        password=Nextcloud["token"],
25
        headers=headers,
26
    ) as client:
27
        my_principal = client.principal()
28
        calendar = next(
29
            filter(
30
                lambda calendar: calendar.name == calendarName, my_principal.calendars()
31
            )
32
        )
33
        if calendar is None:
34
            raise ValueError("Could not find calendar by the name you provided")
35

36
        my_event = calendar.save_event(
37
            dtstart=event_start,
38
            dtend=event_end,
39
            summary="Do the needful",
40
        )
41
        return my_event
42

43

Other submissions
  • Submitted by marcel klehr12 Python3
    Created 568 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