Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaking the examples #271

Merged
merged 2 commits into from
Feb 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions examples/basic_usage_examples.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from datetime import date
from datetime import datetime
from datetime import timedelta

## We'll try to use the local caldav library, not the system-installed
sys.path.insert(0, "..")
Expand Down Expand Up @@ -99,9 +100,13 @@ def calendar_by_url_demo(client, url):


def read_modify_event_demo(event):
"""
This demonstrates how to edit properties in the ical object and
save it back to the calendar
"""This demonstrates how to edit properties in the ical object
and save it back to the calendar. It takes an event -
caldav.Event - as input. This event is found through the
`search_calendar_demo`. The event needs some editing, which will
be done below. Keep in mind that the differences between an
Event, a Todo and a Journal is small, everything that is done to
he event here could as well be done towards a task.
"""
## The objects (events, journals and tasks) comes with some properties that
## can be used for inspecting the data and modifying it.
Expand Down Expand Up @@ -135,6 +140,21 @@ def read_modify_event_demo(event):
"celebratiuns", "celebrations"
)

## timestamps (DTSTAMP, DTSTART, DTEND for events, DUE for tasks,
## etc) can be fetched using the icalendar library like this:
dtstart = event.icalendar_component.get("dtstart")

## but, dtstart is not a python datetime - it's a vDatetime from
## the icalendar package. If you want it as a python datetime,
## use the .dt property. (In this case dtstart is set - and it's
## pretty much mandatory for events - but the code here is robust
## enough to handle cases where it's undefined):
dtstart_dt = dtstart and dtstart.dt

## We can modify it:
if dtstart:
event.icalendar_component["dtstart"].dt = dtstart.dt + timedelta(seconds=3600)

## And finally, get the casing correct
event.data = event.data.replace("norwegian", "Norwegian")

Expand All @@ -153,7 +173,7 @@ def read_modify_event_demo(event):

## NOTE: always use event.save() for updating events and
## calendar.save_event(data) for creating a new event.
## This may not break:
## This may break:
# event.save(event.data)
## ref https://github.com/python-caldav/caldav/issues/153

Expand Down