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