The Scoop

  • Home
  • Projects
  • About The Scoop
  • Fixing Journalism
  • Departments
    • Apple
    • Asides
    • Broadcast
    • Campaign Finance
    • Car Tools
    • Data
    • DIY
    • django
    • Fed Data
    • FOIA
    • General
    • IRE
    • Journalism
    • Local Data
    • Mapping
    • Miscellany
    • NonGov Data
    • Online
    • Paper Trail
    • Presentations
    • Public Records
    • Python
    • Rails
    • SLA
    • Social Network Analysis
    • Sports
    • State Data
    • Teaching
    • Work
    • XML
  • Subscribe via RSS

Django, iCal and vObject

July 31st, 2007  |  Published in Python, django  |  10 Comments

For one of our Django applications at work we received a request to add iCal feeds to accompany the RSS feeds available for each candidate’s page (example here). I first thought about doing this using a hard-coded template, the way described in March on boomby.com. So I did and it worked - the directions are great. Since I happened to be at OSCON, I showed it to Jacob, who immediately recommended that I check out vObject, a Python library for generating iCal files.

Why use vObject? Well, for one, it eliminates the need for a template file, and the iCal spec being a slightly cranky thing, that’s for the better. How cranky? Well, it doesn’t like carriage returns in template, for one thing. It also means that Django treats iCal feeds much the same as RSS feeds in terms of generation. So using vObject means you set up the file in the view, call the serialize() function to fill out the standard stuff and return an HttpResponse, setting the mimetype to text/calendar.

Seems pretty simple? It is, with one exception. Internet Explorer (of course). To ensure that Outlook correctly handles the iCal files, you need to add a few lines to the calendar object and to each event. Specifically, you need to do the following inside your view:


    cal = vobject.iCalendar()
    cal.add('method').value = 'PUBLISH'  # IE/Outlook needs this
    for event in event_list:
        vevent = cal.add('vevent')
        ... # add your event details
    icalstream = cal.serialize()
    response = HttpResponse(icalstream, mimetype='text/calendar')
    response['Filename'] = 'filename.ics'  # IE needs this
    response['Content-Disposition'] = 'attachment; filename=filename.ics'

Outlook seems to require three datetime fields for each event: DTSTART, DTEND and DTSTAMP (even if you don’t have values for all of them). Other consumers of .ics files do not, and what works on Windows with Firefox and Outlook may not with IE and Outlook. But it does work if you follow the steps, and vObject makes it much easier.

The only other thing you need is to set up the url in Django’s urlconf and have it call the view. No template needed! Here’s a sample iCal file listing Mike Huckabee’s upcoming visits.

Thanks to Jacob, Malcolm and Simon for their advice on this. Any mistakes are mine, however, and comments or suggestions are welcome.

Responses

Feed Trackback Address
  1. Lorenzo Bolognini says:

    August 1st, 2007 at 7:48 am (#)

    Please add it to DjangoSnippets! ;-)

    L

  2. Jeff Croft says:

    August 1st, 2007 at 10:51 am (#)

    Wonderful! This will definitely come in handy. Thanks! :)

  3. Doug Napoleone says:

    August 1st, 2007 at 11:30 am (#)

    Question:
    Do you know if the problems with the active feed and timezone’s have been fixed? I plan on checking myself but people might be interested in this.

    I ran into a problem where it does not support active iCal feeds (think iCal+RSS), which is supported by google, and Mac. This became a required feature for the PyCon schedule app. I also ran into problems with the timezone. Congress changes the daylight savings time in the middle of our development, and at one point vObject data worked, the next it stopped working because of a MS patch for dealing with daylight savings. (DTTimeZone stopped working).

    In the end I made my own ical django template which worked everywhere, not the most elegant solution.
    https://svn.python.org/conference/django/trunk/pycon/schedule/templates/schedule/sched.ics
    http://us.pycon.org/apps07/schedule/

  4. Derek says:

    August 1st, 2007 at 11:54 am (#)

    Doug,

    As far as I know that issue hasn’t been fixed. Due to a quirk of our app, which lists events in their local times around the country rather than standardize on a single timezone, we basically ignored that part of it (which obviously would not work for everyone).

    In general, vObject worked great with Macs and Google Calendar, and less so with Outlook and, especially, IE. So I’m pretty sure your experience still holds.

    Derek

  5. Jeffrey Harris says:

    August 1st, 2007 at 1:45 pm (#)

    I’m glad you’ve found vobject useful.

    I’ve considered serializing a default METHOD to make Outlook happy, but rfc2445 makes METHOD optional, and CalDAV specifically forbids it, so I decided not to.

    Doug, it looks like your template is using UTC times. If you want to use UTC times with vobject, that ought to work, your datetimes just need a UTC tzinfo class.

    It’s probably better to use non-UTC timezones in iCalendar though, because if Congress changes daylight savings time again consumers of your data have at least some hope of figuring out what local time was originally intended.

    vobject ought to handle floating datetimes (no timezone), datetimes in UTC, or datetimes in a non-UTC timezone. In this last case, iCalendar requires a VTIMEZONE, which vobject should create for you automatically.

    If you had trouble getting timezones to serialize, I’d be interested to hear more on the vobject mailing list. Timezones are a pain.

    Jeffrey

  6. Empty says:

    August 1st, 2007 at 6:59 pm (#)

    This is great stuff and pretty timely for me. Thanks.

  7. Doug Napoleone says:

    August 2nd, 2007 at 4:10 pm (#)

    Jeffrey,

    I switched to UTC at the very end due to problems caused by the MS patch. It was the only thing I could get to work everywhere. The change history on the version which worked one day and stopped the next is here:

    https://pycon.coderanger.net/changeset?new=django%2Ftrunk%2Fpycon%2Fschedule%2Ftemplates%2Fschedule%2Fsched.ics%40234&old=django%2Ftrunk%2Fpycon%2Fschedule%2Ftemplates%2Fschedule%2Fsched.ics%40193

  8. Geoff says:

    August 7th, 2007 at 7:36 am (#)

    Excellent Derek - you’ve done it properly. Much nicer than the hack that I came up with for mactactic.

  9. The Bitter Pill : » Stupidly simple microformat syncing with Django’s generic views. says:

    August 10th, 2007 at 5:10 pm (#)

    [...] Via Jeff Croft I saw Derek Willis’ solution for doing this with vobject. Looks like a great solution for icalendar links, but vobject struggles a bit with vcard, and I couldn’t get it to install on my shared server, so I kept looking. [...]

  10. Paul says:

    August 18th, 2007 at 5:00 pm (#)

    For what it’s worth, here’s my simple example of using vObject with Django to produce vCard output.

Leave a Response

Recent Comments

  • Scot Hacker on Six Reasons To Look Past Caspio
  • Dan D. Gutierrez on Six Reasons To Look Past Caspio
  • The AllYourtv.com Local News Blog » Six Reasons To Look Past Caspio on Six Reasons To Look Past Caspio
  • Justin Lilly on Six Reasons To Look Past Caspio
  • Derek on Six Reasons To Look Past Caspio

Recent Posts

  • The Hidden Appeal of GeoDjango
  • Six Reasons To Look Past Caspio
  • Fumblerooski
  • The Birth of Quadruplets, or Understanding the Process
  • DjangoCon


©2008 The Scoop
Powered by WordPress using the Gridline Lite theme by Graph Paper Press.