add iCal event calendar

This commit is contained in:
Janus Troelsen 2016-08-30 13:21:44 +02:00
parent 78d84b21f9
commit 70c61b0048
3 changed files with 32 additions and 1 deletions

View file

@ -10,6 +10,7 @@ from django.conf.urls import include, url
from django.contrib import admin from django.contrib import admin
from django.views.generic import TemplateView, RedirectView from django.views.generic import TemplateView, RedirectView
from django.core.urlresolvers import reverse_lazy from django.core.urlresolvers import reverse_lazy
from program.views import ICSView
urlpatterns = [ urlpatterns = [
url( url(
@ -84,4 +85,5 @@ urlpatterns = [
), ),
url(r'^accounts/', include('allauth.urls')), url(r'^accounts/', include('allauth.urls')),
url(r'^admin/', include(admin.site.urls)), url(r'^admin/', include(admin.site.urls)),
url(r'^calendar/', ICSView.as_view()),
] ]

View file

@ -2,10 +2,38 @@ from collections import OrderedDict
import datetime import datetime
from django.views.generic import ListView, TemplateView, DetailView 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 camps.models import Day
from . import models 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): class SpeakerDetailView(DetailView):
model = models.Speaker model = models.Speaker

View file

@ -9,4 +9,5 @@ Pillow==3.2.0
qrcode==5.3 qrcode==5.3
CommonMark==0.6.4 CommonMark==0.6.4
django-bleach==0.3.0 django-bleach==0.3.0
Unidecode==0.4.19 Unidecode==0.4.19
icalendar==3.10