From 70c61b00484ecfb4db038d8746c0e5228873fad3 Mon Sep 17 00:00:00 2001 From: Janus Troelsen Date: Tue, 30 Aug 2016 13:21:44 +0200 Subject: [PATCH] add iCal event calendar --- bornhack/urls.py | 2 ++ program/views.py | 28 ++++++++++++++++++++++++++++ requirements/base.txt | 3 ++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/bornhack/urls.py b/bornhack/urls.py index be35409a..899dd966 100644 --- a/bornhack/urls.py +++ b/bornhack/urls.py @@ -10,6 +10,7 @@ from django.conf.urls import include, url from django.contrib import admin from django.views.generic import TemplateView, RedirectView from django.core.urlresolvers import reverse_lazy +from program.views import ICSView urlpatterns = [ url( @@ -84,4 +85,5 @@ urlpatterns = [ ), url(r'^accounts/', include('allauth.urls')), url(r'^admin/', include(admin.site.urls)), + url(r'^calendar/', ICSView.as_view()), ] diff --git a/program/views.py b/program/views.py index 01d71dc7..104fb8d8 100644 --- a/program/views.py +++ b/program/views.py @@ -2,10 +2,38 @@ from collections import OrderedDict import datetime from django.views.generic import ListView, TemplateView, DetailView +from django.views import View +from django.http.response import HttpResponse from camps.models import Day from . import models +import icalendar +from icalendar import vDatetime + +def gen_icalevents(event): + for i in event.days.all(): + ievent = icalendar.Event() + ievent['summary'] = event.title + + newdate = datetime.datetime.combine(i.date, datetime.time(event.start.hour, event.start.minute, event.start.second)) + ievent['dtstart'] = vDatetime(newdate).to_ical() + + newdate = datetime.datetime.combine(i.date, datetime.time(event.end.hour, event.end.minute, event.end.second)) + ievent['dtend'] = vDatetime(newdate).to_ical() + + yield ievent + +def gen_ics(events): + cal = icalendar.Calendar() + for event in events: + for ical_event in gen_icalevents(event): + cal.add_component(ical_event) + return cal.to_ical() + +class ICSView(View): + def get(self, request, *args, **kwargs): + return HttpResponse(gen_ics(models.Event.objects.all())) class SpeakerDetailView(DetailView): model = models.Speaker diff --git a/requirements/base.txt b/requirements/base.txt index 6efaf098..9c15ff5e 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -9,4 +9,5 @@ Pillow==3.2.0 qrcode==5.3 CommonMark==0.6.4 django-bleach==0.3.0 -Unidecode==0.4.19 \ No newline at end of file +Unidecode==0.4.19 +icalendar==3.10