Refactored ics code and added filtering.

This commit is contained in:
Víðir Valberg Guðmundsson 2017-04-13 11:48:50 +02:00
parent ec53bae454
commit 5e322472e2
4 changed files with 51 additions and 19 deletions

View file

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

View file

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

View file

@ -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 """

View file

@ -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 ########################################################