From 5e322472e25d243ec600e54c06e2ac0a8b531f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Thu, 13 Apr 2017 11:48:50 +0200 Subject: [PATCH] Refactored ics code and added filtering. --- src/bornhack/urls.py | 3 +++ src/program/ics.py | 13 ------------- src/program/models.py | 18 +++++++++++++++--- src/program/views.py | 36 +++++++++++++++++++++++++++++++++--- 4 files changed, 51 insertions(+), 19 deletions(-) delete mode 100644 src/program/ics.py diff --git a/src/bornhack/urls.py b/src/bornhack/urls.py index 7c7d6125..896392ad 100644 --- a/src/bornhack/urls.py +++ b/src/bornhack/urls.py @@ -135,6 +135,9 @@ urlpatterns = [ ScheduleView.as_view(), name='schedule_index' ), + url( + r'^ics/', ICSView.as_view(), name="ics_view" + ), url( r'^proposals/', include([ url( diff --git a/src/program/ics.py b/src/program/ics.py deleted file mode 100644 index a88b0044..00000000 --- a/src/program/ics.py +++ /dev/null @@ -1,13 +0,0 @@ -import icalendar - -def gen_ics(eventinstances): - cal = icalendar.Calendar() - for eventinstance in eventinstances: - ievent = icalendar.Event() - ievent['summary'] = eventinstance.event.title - ievent['dtstart'] = icalendar.vDatetime(eventinstance.when.lower).to_ical() - ievent['dtend'] = icalendar.vDatetime(eventinstance.when.upper).to_ical() - cal.add_component(ievent) - return cal.to_ical() - - diff --git a/src/program/models.py b/src/program/models.py index cf12f529..e8c1c98a 100644 --- a/src/program/models.py +++ b/src/program/models.py @@ -1,18 +1,22 @@ +import uuid, os +from datetime import timedelta + from django.contrib.postgres.fields import DateTimeRangeField from django.db import models from django.utils.text import slugify from django.conf import settings from django.utils.translation import ugettext_lazy as _ -from utils.models import CreatedUpdatedModel, CampRelatedModel from django.core.exceptions import ValidationError -from datetime import timedelta from django.core.urlresolvers import reverse_lazy -import uuid, os from django.core.files.storage import FileSystemStorage from django.urls import reverse from django.apps import apps from django.core.files.base import ContentFile +import icalendar + +from utils.models import CreatedUpdatedModel, CampRelatedModel + class CustomUrlStorage(FileSystemStorage): def __init__(self, location=None): @@ -436,6 +440,14 @@ class EventInstance(CampRelatedModel): minutes = seconds / 60 return minutes / settings.SCHEDULE_TIMESLOT_LENGTH_MINUTES + def get_ics_event(self): + ievent = icalendar.Event() + ievent['summary'] = self.event.title + ievent['dtstart'] = icalendar.vDatetime(self.when.lower).to_ical() + ievent['dtend'] = icalendar.vDatetime(self.when.upper).to_ical() + ievent['location'] = icalendar.vText(self.location.name) + return ievent + def get_speaker_picture_upload_path(instance, filename): """ We want speaker pictures are saved as MEDIA_ROOT/public/speakers/camp-slug/speaker-slug/filename """ diff --git a/src/program/views.py b/src/program/views.py index 97f257da..4d28f7e5 100644 --- a/src/program/views.py +++ b/src/program/views.py @@ -1,3 +1,5 @@ +import datetime, os + from django.views.generic import ListView, TemplateView, DetailView, View from django.views.generic.edit import CreateView, UpdateView from django.conf import settings @@ -8,11 +10,12 @@ from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib import messages from django.shortcuts import redirect from django.urls import reverse + +import icalendar + from camps.mixins import CampViewMixin from .mixins import CreateProposalMixin, EnsureUnapprovedProposalMixin, EnsureUserOwnsProposalMixin, EnsureWritableCampMixin, PictureViewMixin, EnsureCFSOpenMixin from . import models -import datetime, os -from .ics import gen_ics ############## ical calendar ######################################################## @@ -20,7 +23,34 @@ from .ics import gen_ics class ICSView(CampViewMixin, View): def get(self, request, *args, **kwargs): - return HttpResponse(gen_ics(models.EventInstance.objects.all())) + eventinstances = models.EventInstance.objects.all() + type_ = request.GET.get('type', None) + location = request.GET.get('location', None) + + if type_: + try: + eventtype = models.EventType.objects.get( + slug=type_ + ) + eventinstances = eventinstances.filter(event__event_type=eventtype) + except models.EventType.DoesNotExist: + raise Http404 + + if location: + try: + eventlocation = models.EventLocation.objects.get( + slug=location, + camp=self.camp, + ) + eventinstances = eventinstances.filter(location__slug=location) + except models.EventLocation.DoesNotExist: + raise Http404 + + cal = icalendar.Calendar() + for event_instance in eventinstances: + cal.add_component(event_instance.get_ics_event()) + + return HttpResponse(cal.to_ical()) ############## proposals ########################################################