Login

Generate ICalendar Files with django-events

Author:
ElfSternberg
Posted:
October 6, 2010
Language:
Python
Version:
1.2
Tags:
calendar ical icalendar
Score:
0 (after 0 ratings)

This is a fairly straightforward view to generate iCalendar (.ics) files, with a unique UUID, a proper filename, and the basic fields needed to export an event from a public calendar (using the django-events-calendar app). While it can certainly be extended and adapted, it works very well as-is.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from datetime import datetime
from icalendar import Calendar, Event
from django.db.models import get_model
from django.http import HttpResponse
from django.contrib.sites.models import Site

def export(request, event_id):
    event = get_model('events', 'event').objects.get(id = event_id)

    cal = Calendar()
    site = Site.objects.get_current()

    cal.add('prodid', '-//%s Events Calendar//%s//' % (site.name, site.domain))
    cal.add('version', '2.0')

    site_token = site.domain.split('.')
    site_token.reverse()
    site_token = '.'.join(site_token)

    ical_event = Event()
    ical_event.add('summary', event.description)
    ical_event.add('dtstart', event.start)
    ical_event.add('dtend', event.end and event.end or event.start)
    ical_event.add('dtstamp', event.end and event.end or event.start)
    ical_event['uid'] = '%d.event.events.%s' % (event.id, site_token)
    cal.add_component(ical_event)

    response = HttpResponse(cal.as_string(), mimetype="text/calendar")
    response['Content-Disposition'] = 'attachment; filename=%s.ics' % event.slug
    return response


Add to URLS.py:
urlpatterns += patterns('',
    url(r'^events/(?P<event_id>\d+)/export/', 'app_events.ics_views.export', name="event_ics_export"),
)


And invoke via:
<a href="{% url event_ics_export event.id %}">Export Event</a>

More like this

  1. Generate Google Calendar links from django-event-calendar by ElfSternberg 4 years, 9 months ago
  2. Simple Django event calendar template tag by sandinmyjoints_ 4 years ago
  3. Calendar template-tag by masida 8 years, 3 months ago
  4. Link Models to Google Calendar by JosefK 7 years, 11 months ago
  5. Calendar table by fauxparse 7 years, 2 months ago

Comments

Please login first before commenting.