Last active
October 17, 2016 19:45
-
-
Save audiolion/93eacfb86033a67e4ca02fdc1282de8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# refactored code | |
# -*- coding: utf-8 -*- | |
"""API Endpoints for the Calendar | |
Endpoints designed to be called with AJAX requests and retrieve the events | |
that should populate the webpage's calendar. | |
""" | |
from django.utils.html import strip_tags | |
from django.http import HttpResponse | |
from django.shortcuts import get_object_or_404 | |
from accounts.models import ShoppingCart, User | |
from .dictionaries import EventDictionary | |
from .models import Event, Registration | |
from datetime import datetime | |
import json | |
class BaseOccurrence(object): | |
title = "" | |
description = "" | |
color = "" | |
strand = "" | |
start = "" | |
url = "" | |
occurrence_type = "" | |
def __init__(self, **kwargs): | |
self.strand = kwargs.get('strand', '') | |
self.start = kwargs.get('start', '') | |
self.url = kwargs.get('url', '') | |
def to_dict(self): | |
return {"title": self.title, | |
"description": self.description, | |
"color": self.color, | |
"strand": self.strand, | |
"start": self.start, | |
"url": self.url, | |
"occurrence_type": self.occurrence_type} | |
class EventOccurrence(BaseOccurrence): | |
occurrence_type = "event" | |
def __init__(self, **kwargs): | |
self.title = kwargs.get('title', '') | |
self.description = self.parse_description(**kwargs) | |
self.color = kwargs.get('color', '') | |
super(BaseOccurrence, self).__init__(**kwargs) | |
def parse_description(self, **kwargs): | |
strand = kwargs.get('strand_verbose', '') | |
description = kwargs.get('description', '') | |
description = strip_tags(description)[:140].replace(' ', ' ') + ".." | |
return strand + "\n" + description | |
class OpenOccurrence(BaseOccurrence): | |
occurrence_type = "reg_open" | |
color = "#34495e" | |
def __init__(self, **kwargs): | |
self.title = 'Registration Opens for ' + kwargs.get('title', '') | |
self.description = 'Registration Open for ' + kwargs.get('description', '') | |
super(BaseOccurrence, self).__init__(**kwargs) | |
class ClosedOccurrence(BaseOccurrence): | |
occurrence_type = "reg_close" | |
color = "#34495e" | |
def __init__(self, **kwargs): | |
self.title = 'Registration Closes for ' + kwargs.get('title', '') | |
self.description = 'Registration Closes for ' + kwargs.get('description', '') | |
super(BaseOccurrence, self).__init__(**kwargs) | |
class EventApiMixin(object): | |
"""Base class for the Event API holding common code | |
""" | |
def parse_request(self, request): | |
default_time = datetime.now() | |
start = parse(request.GET.get("start", ""), default=default_time) | |
end = parse(request.GET.get("end", ""), default=default_time) | |
return (start, end) | |
def get_color(self, event): | |
color = EventDictionary.STRAND_COLORS_DICT.get(event.strand_type) | |
if color is None: | |
color = EventDictionary.STRAND_COLORS_DICT.get(EventDictionary.GENERAL) | |
return color | |
def get_strand(self, event): | |
event_strand = event.strand_type | |
strand_verbose = EventDictionary.STRAND_TYPES_DICT.get(event_strand) | |
if strand_verbose is None: | |
strand = EventDictionary.STRAND_TYPES_DICT.get(EventDictionary.GENERAL) | |
event_strand = EventDictionary.GENERAL | |
return (event_strand, strand_verbose) | |
def create_event_dict(self, **kwargs): | |
if kwargs.get('occurrence_type', '') == OpenOccurrence.occurrence_type: | |
return OpenOccurrence(**kwargs).to_dict() | |
elif kwargs.get('occurrence_type', '') == ClosedOccurrence.occurrence_type: | |
return ClosedOccurrence(**kwargs).to_dict() | |
elif kwargs.get('occurrence_type', '') == EventOccurrence.occurrence_type: | |
return EventOccurrence(**kwargs).to_dict() | |
else: | |
return BaseOccurrence.to_dict() | |
def create_events_json(self, events): | |
events_json = [] | |
for event in events: | |
color = self.get_color(event) | |
strand, strand_verbose = self.get_strand(event) | |
for meeting in event.meetings_between(start, end): | |
occurrence, occurrence_type = meeting | |
kwargs = { "title": event.title, | |
"description": event.description, | |
"color": color, | |
"strand": strand, | |
"start": str(occurrence), | |
"url": str(event.get_absolute_url()), | |
"occurrence_type": occurrence_type} | |
events_json.append(self.create_event_dict(event, **kwargs)) | |
return events_json | |
class SingleEvent(EventApiMixin, View): | |
"""Provides JSON Endpoint for a Single Calendar Events Schedule | |
""" | |
def get(self, request, *args, **kwargs): | |
start, end = self.parse_request(request) | |
events = (get_object_or_404(Event, pk=int(request.GET.get("event"))), ) | |
events_json = self.create_events_json(events) | |
return HttpResponse(json.dumps(events_json), content_type="application/json") | |
class AllEvents(EventApiMixin, View): | |
"""Provides JSON Endpoint for All Events on Calendar | |
Events include Registration open and close dates, general events, and | |
events that have multiple occurrences over time. Events are colored based | |
on their Event Strand color. | |
""" | |
def get(self, request, *args, **kwargs): | |
start, end = self.parse_request(request) | |
events = Event.published.all() | |
events_json = self.create_events_json(events) | |
return HttpResponse(json.dumps(events_json), content_type="application/json") | |
class UserEvents(EventApiMixin, View): | |
"""Provides JSON Endpoint for User's Schedule | |
Events returned are only those that are part of the requested user's | |
schedule. Events are broken into three categories, events in the user's | |
shopping cart (potentially registering for), events that the user has | |
requested to register for, and events that the user has been approved | |
for. | |
""" | |
user_enrolled_events = [] | |
user_pending_events = [] | |
def get_color(self, idx, event): | |
if idx == 0: | |
color = "#F36E21" | |
else: | |
if event in self.user_enrolled_events: | |
color = "#5cb85c" | |
else: | |
color = "#5bc0de" | |
return color | |
def create_event_list(self, user_pk): | |
cart = ShoppingCart.objects.get(owner=user_pk) | |
cart_events = cart.events.all() | |
usr = User.objects.get(pk=user_pk) | |
user_regs = usr.registration_set.exclude(status=Registration.DENIED) | |
for reg in user_regs: | |
if reg.event.in_session(): | |
if reg.status == Registration.APPROVED: | |
self.user_enrolled_events.append(reg.event) | |
else: | |
self.user_pending_events.append(reg.event) | |
return [cart_events, self.user_pending_events, self.user_enrolled_events] | |
def create_events_json(self, events_list): | |
events_json = [] | |
for idx, lst in enumerate(events_list): | |
for event in lst: | |
color = self.get_color(idx, event) | |
strand, strand_verbose = self.get_strand(event) | |
for meeting in event.meetings_between(start, end): | |
occurrence, occurrence_type = meeting | |
kwargs = { "title": event.title, | |
"description": event.description, | |
"color": color, | |
"strand": strand, | |
"start": str(occurrence), | |
"url": str(event.get_absolute_url()), | |
"occurrence_type": occurrence_type} | |
events_json.append(self.create_event_dict(event, **kwargs)) | |
return events_json | |
def get(self, request, *args, **kwargs): | |
start, end = self.parse_request(request) | |
events_list = self.create_event_list(request.user.pk) | |
events_json = self.create_events_json(events_list) | |
return HttpResponse(json.dumps(events_json), content_type="application/json") | |
class UserSchedule(GroupRequiredMixin, UserEvents): | |
group_required = [u"admins", u"managers"] | |
def get(self, request, *args, **kwargs): | |
start, end = self.parse_request(request) | |
events_list = self.create_event_list(kwargs['pk']) | |
events_json = self.create_events_json(events_list) | |
return HttpResponse(json.dumps(events_json), content_type="application/json") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment