Verified Commit e322abc6 authored by David Haynes's avatar David Haynes 🙆
Browse files

Do not return old schedules in /facilites or /schedules

- comments for everything
- remove all special_schedules that have expired from a Facility
- exclude all Schedules that have been expired from /schedules

Closes #51
parent e3b2dafd
Pipeline #1424 passed with stage
in 1 minute and 24 seconds
......@@ -7,6 +7,9 @@ Rest Framework Class Views
from __future__ import (absolute_import, division, print_function,
unicode_literals)
# Python std. lib. imports
import datetime
# App Imports
from .models import Facility, OpenTime, Category, Schedule, Location, Alert
from .serializers import (CategorySerializer, FacilitySerializer,
......@@ -23,6 +26,10 @@ class AlertViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = AlertSerializer
def get_queryset(self):
"""
Handle incoming GET requests and enumerate objects that get returned by
the API.
"""
return Alert.objects.all()
class CategoryViewSet(viewsets.ReadOnlyModelViewSet):
......@@ -32,6 +39,10 @@ class CategoryViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = CategorySerializer
def get_queryset(self):
"""
Handle incoming GET requests and enumerate objects that get returned by
the API.
"""
return Category.objects.all()
class LocationViewSet(viewsets.ReadOnlyModelViewSet):
......@@ -41,34 +52,72 @@ class LocationViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = LocationSerializer
def get_queryset(self):
"""
Handle incoming GET requests and enumerate objects that get returned by
the API.
"""
return Location.objects.all()
class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
"""
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.
"""
serializer_class = FacilitySerializer
def get_queryset(self):
"""
Handle incoming GET requests and enumerate objects that get returned by
the API.
"""
# Define and handle ?open_now
open_now = self.request.query_params.get('open_now', None)
if open_now is not None:
results = []
for fac in Facility.objects.all():
if fac.is_open():
results.append(fac.pk)
return Facility.objects.filter(pk__in=results)
# 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
else:
for facility in Facility.objects.all():
# Remove all special_schedules that have expired
facility.clean_special_schedules()
return Facility.objects.all()
class ScheduleViewSet(viewsets.ModelViewSet):
"""
Return all Schedule objects.
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)
"""
serializer_class = ScheduleSerializer
def get_queryset(self):
return Schedule.objects.all()
"""
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)
class OpenTimeViewSet(viewsets.ModelViewSet):
"""
......@@ -77,4 +126,8 @@ class OpenTimeViewSet(viewsets.ModelViewSet):
serializer_class = OpenTimeSerializer
def get_queryset(self):
"""
Handle incoming GET requests and enumerate objects that get returned by
the API.
"""
return OpenTime.objects.all()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment