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

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

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

16
# Other Imports
17
from rest_framework import viewsets
18

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

David Haynes's avatar
David Haynes committed
25
26
27
    def get_queryset(self):
        return Alert.objects.all()

28
class CategoryViewSet(viewsets.ReadOnlyModelViewSet):
29
    """
David Haynes's avatar
David Haynes committed
30
    Return all Category objects.
31
    """
32
33
    serializer_class = CategorySerializer

David Haynes's avatar
David Haynes committed
34
35
36
    def get_queryset(self):
        return Category.objects.all()

David Haynes's avatar
David Haynes committed
37
38
class LocationViewSet(viewsets.ReadOnlyModelViewSet):
    """
David Haynes's avatar
David Haynes committed
39
    Return all Location objects.
David Haynes's avatar
David Haynes committed
40
41
42
    """
    serializer_class = LocationSerializer

David Haynes's avatar
David Haynes committed
43
44
45
    def get_queryset(self):
        return Location.objects.all()

46
class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
47
48
    """
    """
49
50
    serializer_class = FacilitySerializer

51
    def get_queryset(self):
52
53
        """
        """
David Haynes's avatar
David Haynes committed
54
        open_now = self.request.query_params.get('open_now', None)
55
56
        if open_now is not None:
            results = []
David Haynes's avatar
David Haynes committed
57
58
59
60
            for fac in Facility.objects.all():
                if fac.is_open():
                    results.append(fac.pk)
            return Facility.objects.filter(pk__in=results)
61
        else:
David Haynes's avatar
David Haynes committed
62
            return Facility.objects.all()
63

64
class ScheduleViewSet(viewsets.ModelViewSet):
65
    """
David Haynes's avatar
David Haynes committed
66
    Return all Schedule objects.
67
    """
68
69
    serializer_class = ScheduleSerializer

David Haynes's avatar
David Haynes committed
70
71
72
    def get_queryset(self):
        return Schedule.objects.all()

73
class OpenTimeViewSet(viewsets.ModelViewSet):
74
    """
David Haynes's avatar
David Haynes committed
75
    Return all OpenTime objects.
76
    """
77
    serializer_class = OpenTimeSerializer
David Haynes's avatar
David Haynes committed
78
79
80

    def get_queryset(self):
        return OpenTime.objects.all()