diff --git a/src/bornhack/urls.py b/src/bornhack/urls.py index aef694ca..07757e43 100644 --- a/src/bornhack/urls.py +++ b/src/bornhack/urls.py @@ -226,16 +226,17 @@ urlpatterns = [ CallForSpeakersView.as_view(), name='call_for_speakers' ), - url( - r'^(?P[-_\w+]+)/$', - EventDetailView.as_view(), - name='event_detail' - ), url( r'^calendar/', ICSView.as_view(), name='ics_calendar' ), + # this has to be the last URL here + url( + r'^(?P[-_\w+]+)/$', + EventDetailView.as_view(), + name='event_detail' + ), ]) ), diff --git a/src/program/ics.py b/src/program/ics.py new file mode 100644 index 00000000..a96c50c2 --- /dev/null +++ b/src/program/ics.py @@ -0,0 +1,25 @@ +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() + + diff --git a/src/program/views.py b/src/program/views.py index 5d8edef9..20d2bcef 100644 --- a/src/program/views.py +++ b/src/program/views.py @@ -1,4 +1,4 @@ -from django.views.generic import ListView, TemplateView, DetailView +from django.views.generic import ListView, TemplateView, DetailView, View from django.views.generic.edit import CreateView, UpdateView from django.conf import settings from django.views.decorators.http import require_safe @@ -12,33 +12,12 @@ from camps.mixins import CampViewMixin from .mixins import CreateProposalMixin, EnsureUnapprovedProposalMixin, EnsureUserOwnsProposalMixin, EnsureWritableCampMixin, PictureViewMixin, EnsureCFSOpenMixin from . import models import datetime, os -import icalendar -from icalendar import vDatetime +from .ics import gen_ics ############## ical calendar ######################################################## -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()))