Skip to content

Instantly share code, notes, and snippets.

@tboulogne
Created November 25, 2018 08:30
Show Gist options
  • Select an option

  • Save tboulogne/d69d386cd9bf4a319c5bfde18ad7723d to your computer and use it in GitHub Desktop.

Select an option

Save tboulogne/d69d386cd9bf4a319c5bfde18ad7723d to your computer and use it in GitHub Desktop.
Generate iCal in Django
from icalendar import Calendar, Event
def ical(request, course_id):
try:
# see if the course exists
c = course.objects.get(id=course_id)
except course.DoesNotExist:
# redirect to search page in future
raise Http404
postCalendarItems = c.post_set.all()
cal = Calendar()
cal.add('prodid', '-//%s Events Calendar//%s//' % ('name', 'domain'))
cal.add('version', '2.0')
cal.add('x-wr-calname', c.name)
for item in postCalendarItems:
ical_event = Event()
ical_event.add('summary', item.name)
ical_event.add('description', item.description)
ical_event.add('url', 'http://%s/class/%s/%s/' % (request.META['HTTP_HOST'], c.id, item.id))
ical_event.add('dtstart', item.cal_date)
ical_event.add('dtend', item.cal_date)
ical_event.add('dtstamp', item.cal_date)
ical_event['uid'] = '%d.event.events' % (item.id)
cal.add_component(ical_event)
response = HttpResponse(cal.to_ical(), mimetype="text/calendar")
response['Content-Disposition'] = 'attachment; filename=%s.ics' % c.name
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment