concrete-datastore icon indicating copy to clipboard operation
concrete-datastore copied to clipboard

Provide endpoint to download calendar events

Open LeanaNeuber opened this issue 5 years ago • 0 comments

An endpoint should be created to be able to transform an instance in a calendar event. The calendar event should be in the iCalendar (.ics) format to be directly imported into different calendars, e.g. Outlook. The format looks as follows:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Example Corp.//CalDAV Client//EN
BEGIN:VEVENT
UID:[email protected]
DTSTAMP:20200516T060000Z
DTSTART:20200517T060000Z
DTEND:20200517T230000Z
RRULE:FREQ=YEARLY
SUMMARY:Do the needful
END:VEVENT
END:VCALENDAR

The endpoint can either be called for all active instances like that, where for each instance one event will be returned (all summarized in one .ical file): /<model>/caldav/?c_event_datetime_fields=start_dateend_date&c_event_description=description&c_event_subject=name

Or for one specific instance: /<model>/<instance_id>/caldav/?c_event_datetime_fields=start_date,end_date&c_event_description=description&c_event_subject=name

While this start_date should be provided for the event to be displayed in the calendar, the end_date is optional. As a deefault for the event subject, the instance name or id can be used.

To implement this feature, the iCalendar python library can be used. The following code snippet creates one event:

from icalendar import Calendar, Event
import uuid
from datetime import datetime

def format_timestamp(ts):
    return ts.strftime('%Y%m%dT%H%M%SZ')

def create_ical_event():
    event = Event()
    cal = Calendar()

    # Add event information
    event.add('summary', 'I am a summary')
    event.add('description', 'I am a description')
    event.add('uid', str(uuid.uuid1()))
    event['dtstart'] = format_timestamp(datetime.now())
    # event['dtend'] = format_timestamp(datetime.now() + timedelta(hours=9))
    # event['created'] = format_timestamp(current_time)
    # event['last-modified'] = format_timestamp(current_time)
    # event['dtstamp'] = format_timestamp(current_time)
    
    # Add one or more events to the calendar
    cal.add_component(event)

    # Write the file 
    f = open('event.ics', 'wb')
    f.write(cal.to_ical())
    f.close()

    return f

LeanaNeuber avatar Sep 25 '20 13:09 LeanaNeuber