Login

Generate ICalendar Files with django-events

Author:
ElfSternberg
Posted:
October 6, 2010
Language:
Python
Version:
1.2
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.