1 from django.http import HttpResponse
2 from django.utils.feedgenerator import SyndicationFeed
6 # Map the keys in the ICalendarFeed internal dictionary to the names of iCalendar attributes.
8 'title': 'x-wr-calname',
9 'description': 'x-wr-caldesc',
20 'ttl': 'x-published-ttl'
26 'description': 'description',
28 # author_email, author_name, and author_link need special handling. Consider them the
29 # 'organizer' of the event <http://tools.ietf.org/html/rfc5545#section-3.8.4.3> and
30 # construct something based on that.
32 'last_modified': 'last-modified',
33 #'comments' require special handling as well <http://tools.ietf.org/html/rfc5545#section-3.8.1.4>
35 'enclosure': 'attach', # does this need special handling?
36 'categories': 'categories', # does this need special handling?
43 class ICalendarFeed(SyndicationFeed):
44 #def __init__(self, title, link, description, language=None, author_email=None,
45 # author_name=None, author_link=None, subtitle=None, categories=None,
46 # feed_url=None, feed_copyright=None, feed_guid=None, ttl=None, **kwargs):
47 # super(ICalendarFeed, self).__init__(title, link, description, language,
48 # author_email, author_name, author_link, subtitle, categories,
49 # feed_url, feed_copyright, feed_guid, ttl, **kwargs)
51 mime_type = 'text/calendar'
53 def add_item(self, *args, **kwargs):
54 for kwarg in ['start', 'end', 'last_modified', 'location']:
55 kwargs.setdefault(kwarg, None)
56 super(ICalendarFeed, self).add_item(*args, **kwargs)
58 def write(self, outfile, encoding):
59 # TODO: Use encoding... how? Just convert all values when setting them should work...
60 cal = vobject.iCalendar()
62 # IE/Outlook needs this. See
63 # <http://blog.thescoop.org/archives/2007/07/31/django-ical-and-vobject/>
64 cal.add('method').value = 'PUBLISH'
66 for key, val in self.feed.items():
67 if key in FEED_ICAL_MAP and val:
68 cal.add(FEED_ICAL_MAP[key]).value = val
70 for item in self.items:
71 # TODO: handle multiple types of events.
72 event = cal.add('vevent')
73 for key, val in item.items():
74 #TODO: handle the non-standard items like comments and author.
75 if key in ITEM_ICAL_MAP and val:
76 event.add(ITEM_ICAL_MAP[key]).value = val
78 cal.serialize(outfile)
80 # Some special handling for HttpResponses. See link above.
81 if isinstance(outfile, HttpResponse):
82 filename = self.feed.get('filename', 'filename.ics')
83 outfile['Filename'] = filename
84 outfile['Content-Disposition'] = 'attachment; filename=%s' % filename