views.py 4.34 KB
Newer Older
1
"""
2
3
api/views.py

4
5
Rest Framework Class Views
"""
6
7
8
9
# Future Imports
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

10
11
12
# Python std. lib. imports
import datetime

13
# App Imports
David Haynes's avatar
David Haynes committed
14
from .models import Facility, OpenTime, Category, Schedule, Location, Alert
15
from .serializers import (CategorySerializer, FacilitySerializer,
David Haynes's avatar
David Haynes committed
16
                          ScheduleSerializer, OpenTimeSerializer,
David Haynes's avatar
David Haynes committed
17
                          LocationSerializer, AlertSerializer)
18

19
# Other Imports
20
from rest_framework import viewsets
21

David Haynes's avatar
David Haynes committed
22
23
class AlertViewSet(viewsets.ReadOnlyModelViewSet):
    """
David Haynes's avatar
David Haynes committed
24
    Return all Alert objects.
David Haynes's avatar
David Haynes committed
25
26
27
    """
    serializer_class = AlertSerializer

David Haynes's avatar
David Haynes committed
28
    def get_queryset(self):
29
30
31
32
        """
        Handle incoming GET requests and enumerate objects that get returned by
        the API.
        """
David Haynes's avatar
David Haynes committed
33
34
        return Alert.objects.all()

35
class CategoryViewSet(viewsets.ReadOnlyModelViewSet):
36
    """
David Haynes's avatar
David Haynes committed
37
    Return all Category objects.
38
    """
39
40
    serializer_class = CategorySerializer

David Haynes's avatar
David Haynes committed
41
    def get_queryset(self):
42
43
44
45
        """
        Handle incoming GET requests and enumerate objects that get returned by
        the API.
        """
David Haynes's avatar
David Haynes committed
46
47
        return Category.objects.all()

David Haynes's avatar
David Haynes committed
48
49
class LocationViewSet(viewsets.ReadOnlyModelViewSet):
    """
David Haynes's avatar
David Haynes committed
50
    Return all Location objects.
David Haynes's avatar
David Haynes committed
51
52
53
    """
    serializer_class = LocationSerializer

David Haynes's avatar
David Haynes committed
54
    def get_queryset(self):
55
56
57
58
        """
        Handle incoming GET requests and enumerate objects that get returned by
        the API.
        """
David Haynes's avatar
David Haynes committed
59
60
        return Location.objects.all()

61
class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
62
    """
63
64
65
66
67
68
69
    A Facility is some type of establishment that has a schedule of open hours and a location that serves a specific purpose that can be categorized.

    GET /api/facilities/
    Return all Facility objects. We additionally filter out stale special_schedules to reduce client side calculations.

    GET /api/facilities/?open_now
    Query parameter that only returns open Facility objects.
70
    """
71
72
    serializer_class = FacilitySerializer

73
    def get_queryset(self):
74
        """
75
76
        Handle incoming GET requests and enumerate objects that get returned by
        the API.
77
        """
78
        # Define and handle ?open_now
David Haynes's avatar
David Haynes committed
79
        open_now = self.request.query_params.get('open_now', None)
80
        if open_now is not None:
81
82
83
84
85
86
87
88
89
90
            # List of all open facilities
            open_facilities = []
            for facility in Facility.objects.all():
                if facility.is_open():
                    # Append the primary key
                    open_facilities.append(facility.pk)
            # Return all Facility objects with the primary keys located in the
            # open_facilities list
            return Facility.objects.filter(pk__in=open_facilities)
        # Default behavior
91
        else:
92
93
94
            for facility in Facility.objects.all():
                # Remove all special_schedules that have expired
                facility.clean_special_schedules()
David Haynes's avatar
David Haynes committed
95
            return Facility.objects.all()
96

97
class ScheduleViewSet(viewsets.ModelViewSet):
98
    """
99
100
101
102
    A period of time between two dates that represents the beginning and end of a "schedule" or rather, a collection of open times for a facility.

    GET /api/schedules
    Return all Schedule objects that have not expired. (ie. end_date is before today)
103
    """
104
105
    serializer_class = ScheduleSerializer

David Haynes's avatar
David Haynes committed
106
    def get_queryset(self):
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        """
        Handle incoming GET requests and enumerate objects that get returned by
        the API.
        """
        # List of all schedules that are outdated
        filter_old_schedules = []
        for schedule in Schedule.objects.all():
            if schedule.valid_end and schedule.valid_start:
                # If the schedule ended before today
                if schedule.valid_end < datetime.date.today():
                    # Add it to the list of objects we are excluding
                    filter_old_schedules.append(schedule.pk)
        # Return all Schedule objects that have not expired
        return Schedule.objects.exclude(pk__in=filter_old_schedules)
David Haynes's avatar
David Haynes committed
121

122
class OpenTimeViewSet(viewsets.ModelViewSet):
123
    """
David Haynes's avatar
David Haynes committed
124
    Return all OpenTime objects.
125
    """
126
    serializer_class = OpenTimeSerializer
David Haynes's avatar
David Haynes committed
127
128

    def get_queryset(self):
129
130
131
132
        """
        Handle incoming GET requests and enumerate objects that get returned by
        the API.
        """
David Haynes's avatar
David Haynes committed
133
        return OpenTime.objects.all()