Exporting a Schedule from a Django Application to Google Calendar

Wed 16 June 2010

Disclaimer

This blog post is not very original, it is essentially a mix of two previous blog posts, one from Andrew Turner and one from Derek Willis.

And now, the real content

For my pet project, ZoFa, I wanted to allow my users to publish their schedule, computed on the site, to their favourite calendar application, particulary to Google Calendar, which is the calendar application I am using myself.

To do this, there are two steps to accomplish : First,create a view generating an ical file (with extension .ics) I used the vobject library to do this as showed here.

By the way, vobject is a part of the doomed Chandler project described in excellent book. Highly recommended.

Having battled quite some time with this, I think that the following cod snippet could be of interest to some people. So, here is the code of my view :

def ical(request, worker_id):
worker=Worker.objects.get(pk=int(worker_id))
cal = vobject.iCalendar()
cal.add('method').value = 'PUBLISH' # IE/Outlook needs this
for shift in worker.shift_set.filter(published=True):
vevent = cal.add('vevent')
vevent.add('dtstart').value=shift.begin #begin and end are python datetime objects
vevent.add('dtend').value=shift.end
vevent.add('summary').value=shift.name
vevent.add('uid').value=str(shift.id)
icalstream = cal.serialize()
response = HttpResponse(icalstream, mimetype='text/calendar')
response['Filename'] = 'shifts.ics' # IE needs this
response['Content-Disposition'] = 'attachment; filename=shifts.ics'
return response

Finally, just add the url to your urls.py :

url(r'^ical/(?P<worker_id>\d+)/shifts.ics$','ical',name="shifts_ical")

It seemed necessary for Google to pick it up to add a file name at the end of the url, but I am not sure about this.

The second step needs that your user import the url of your calendar into Google Calendar. To do this in the current interface :

  1. open the "other calendars" little frame
  2. click on "add" (at the bottom)
  3. click "add by url" and paste the url.

Some concluding remarks :

  • It can take some time before Google really requests your calendar and displays it
  • The .ics file generated can also be used in Outlook, ICal or Thunderbird.
  • Your url needs to be publicly available : there is no way to provide to Google Calendar a login and a password to your site.

The next step for me would be to automatize the addition to google calendar (with a nice "Add your schedule to Google Calendar" button) but I still have no idea about the feasability of this.

Feed - About me

comments powered by Disqus