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.
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 :
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 :
- open the "other calendars" little frame
- click on "add" (at the bottom)
- 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.