python-caldav / caldav

Apache License 2.0
321 stars 95 forks source link

Edit a single occurence of a recurrent event #379

Open julien4215 opened 8 months ago

julien4215 commented 8 months ago

Is it possible to edit a single occurrence of a recurrent event ? I couldn't find a solution.

I guess this is linked to the issue #35.

tobixen commented 8 months ago

No, editing a single instance is more trivial than deleting a single instance. As for now there is no explicit way of doing it in the caldav library (arguably it's outside the scope of the caldav library since this is more in the icalendar layer than the caldav layer - but usability beats purity, so I would be willing to accept a pull request on that if it's done in a good way).

I think the best way to do this is to use calendar.search(begin=..., end=..., expand=True) to find the recurrence instance you want to edit - then when you got it, you may edit it and save it back to the calendar. The server should take care that only that instance is affected.

julien4215 commented 8 months ago

I tried to do this with Nextcloud calendar but when I save the single occurence I edited, it deletes the recurrence event and keeps only the single ocurrence.

Here is the code I used

with caldav.DAVClient(url=url, username=username, password=password) as client:
    calendar = client.calendar(url=calendar_url)
    now = datetime.now()
    events_fetched = calendar.search(start=now, end=now+timedelta(days=5), event=True, expand=True)
    e = events_fetched[0]
    e.icalendar_component["description"] = "Some description"
    e.save()
tobixen commented 8 months ago

I should write up some test code for this and see how different servers behave. Unfortunately I can't promise that I will have time for it anytime soon.

julien4215 commented 8 months ago

Sure, I understand. Thank you for your quick answer.

tobixen commented 8 months ago

I came to think on some things ...

1) Please ensure that there exists a property e.icalendar_component['recurrence_id']. If that one is missing, then for sure it's a client side problem.

2) The correct and safe way to edit a recurrence instance on is probably like this:

I leave this open, because ... probably, this is the only correct way to edit recurring instances, and if so, then the library should do that work. I will have to write tests and run it towards different servers and investigate.

Lots of work - and fairly low priority from my side, but if nothing else I think I have a plan for how to deal with it.

julien4215 commented 8 months ago

Thanks a lot for the explanations ! I will try this solution.

julien4215 commented 8 months ago

It worked ! Just to clarify if someone faces the same issue.

  1. There is a typo in the property, it is recurrence-id.

  2. It shouldn't be missing on the recurrence instance (events obtained by expanding the recurrence) but there is no recurrence-id property for the recurrence event.

tobixen commented 8 months ago

Right, I made a mistake in the post above, of course there should be no underscore in RECURRENCE-ID (capital letters according to the standard, but the icalendar library is case insensitive)