fix a few ics related things, move ics functions to seperate file

This commit is contained in:
Thomas Steen Rasmussen 2017-03-31 19:33:19 +02:00
parent 484c864417
commit 33689c5270
3 changed files with 33 additions and 28 deletions

View file

@ -226,16 +226,17 @@ urlpatterns = [
CallForSpeakersView.as_view(),
name='call_for_speakers'
),
url(
r'^(?P<slug>[-_\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<slug>[-_\w+]+)/$',
EventDetailView.as_view(),
name='event_detail'
),
])
),

25
src/program/ics.py Normal file
View file

@ -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()

View file

@ -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()))