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

get_queryset actually works now

- append ?open_now to return only open facilities
- yay querysets
parent 57d63012
Pipeline #1423 passed with stage
in 1 minute and 22 seconds
......@@ -24,11 +24,11 @@ from rest_framework.routers import DefaultRouter
ROUTER = DefaultRouter()
# Register views to the API router
ROUTER.register(r'categories', CategoryViewSet)
ROUTER.register(r'facilities', FacilityViewSet)
ROUTER.register(r'schedules', ScheduleViewSet)
ROUTER.register(r'locations', LocationViewSet)
ROUTER.register(r'alerts', AlertViewSet)
ROUTER.register(r'categories', CategoryViewSet, 'category')
ROUTER.register(r'facilities', FacilityViewSet, 'facility')
ROUTER.register(r'schedules', ScheduleViewSet, 'schedule')
ROUTER.register(r'locations', LocationViewSet, 'location')
ROUTER.register(r'alerts', AlertViewSet, 'alert')
urlpatterns = [
# / - Default route
......
......@@ -18,50 +18,63 @@ from rest_framework import viewsets
class AlertViewSet(viewsets.ReadOnlyModelViewSet):
"""
Return all Alert objects.
"""
queryset = Alert.objects.all()
serializer_class = AlertSerializer
def get_queryset(self):
return Alert.objects.all()
class CategoryViewSet(viewsets.ReadOnlyModelViewSet):
"""
Return all Category objects.
"""
queryset = Category.objects.all()
serializer_class = CategorySerializer
def get_queryset(self):
return Category.objects.all()
class LocationViewSet(viewsets.ReadOnlyModelViewSet):
"""
Return all Location objects.
"""
queryset = Location.objects.all()
serializer_class = LocationSerializer
def get_queryset(self):
return Location.objects.all()
class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
"""
"""
queryset = Facility.objects.all()
serializer_class = FacilitySerializer
def get_queryset(self):
"""
"""
queryset = Facility.objects.all()
open_now = self.request.query_params.get('open', None)
open_now = self.request.query_params.get('open_now', None)
if open_now is not None:
results = []
for fac in queryset:
if fac.is_open_now():
results.append(fac)
return results
for fac in Facility.objects.all():
if fac.is_open():
results.append(fac.pk)
return Facility.objects.filter(pk__in=results)
else:
return queryset
return Facility.objects.all()
class ScheduleViewSet(viewsets.ModelViewSet):
"""
Return all Schedule objects.
"""
queryset = Schedule.objects.all()
serializer_class = ScheduleSerializer
def get_queryset(self):
return Schedule.objects.all()
class OpenTimeViewSet(viewsets.ModelViewSet):
"""
Return all OpenTime objects.
"""
queryset = OpenTime.objects.all()
serializer_class = OpenTimeSerializer
def get_queryset(self):
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