Fixing ICSView.

This commit is contained in:
Vidir Valberg Gudmundsson 2017-08-14 00:50:54 +02:00
parent 92126cc98f
commit bd1d139d2d

View file

@ -11,6 +11,7 @@ from django.utils.decorators import method_decorator
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib import messages from django.contrib import messages
from django.urls import reverse from django.urls import reverse
from django.db.models import Q
import icalendar import icalendar
@ -37,27 +38,45 @@ logger = logging.getLogger("bornhack.%s" % __name__)
class ICSView(CampViewMixin, View): class ICSView(CampViewMixin, View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
eventinstances = models.EventInstance.objects.filter(event__camp=self.camp) eventinstances = models.EventInstance.objects.filter(event__camp=self.camp)
type_ = request.GET.get('type', None)
location = request.GET.get('location', None)
if type_: # Type query
try: type_query = request.GET.get('type', None)
eventtype = models.EventType.objects.get( if type_query:
slug=type_ type_slugs = type_query.split(',')
) types = models.EventType.objects.filter(
eventinstances = eventinstances.filter(event__event_type=eventtype) slug__in=type_slugs
except models.EventType.DoesNotExist: )
raise Http404 eventinstances = eventinstances.filter(event__event_type__in=types)
if location: # Location query
try: location_query = request.GET.get('location', None)
eventlocation = models.EventLocation.objects.get( if location_query:
slug=location, location_slugs = location_query.split(',')
camp=self.camp, locations = models.EventLocation.objects.filter(
) slug__in=location_slugs,
eventinstances = eventinstances.filter(location__slug=location) camp=self.camp,
except models.EventLocation.DoesNotExist: )
raise Http404 eventinstances = eventinstances.filter(location__in=locations)
# Video recording query
video_query = request.GET.get('video', None)
if video_query:
video_states = video_query.split(',')
query_kwargs = {}
if 'has-recording' in video_states:
query_kwargs['event__video_url__isnull'] = False
if 'to-be-recorded' in video_states:
query_kwargs['event__video_recording'] = True
if 'not-to-be-recorded' in video_states:
if 'event__video_recording' in query_kwargs:
del query_kwargs['event__video_recording']
else:
query_kwargs['event__video_recording'] = False
eventinstances = eventinstances.filter(**query_kwargs)
cal = icalendar.Calendar() cal = icalendar.Calendar()
for event_instance in eventinstances: for event_instance in eventinstances: