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

List compss

- I figured out how to use list comps
- let's clean some of these for loops
parent 1adaebb6
...@@ -124,11 +124,12 @@ class AlertViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -124,11 +124,12 @@ class AlertViewSet(viewsets.ReadOnlyModelViewSet):
return Alert.objects.all() return Alert.objects.all()
# Default behavior # Default behavior
else: else:
alertable = []
# Enumerate all Alert objects that are active # Enumerate all Alert objects that are active
for alert in Alert.objects.all(): alertable = [
if alert.is_active(): alert.pk
alertable.append(alert.pk) for alert in Alert.objects.all()
if alert.is_active()
]
# Return active Alerts # Return active Alerts
return Alert.objects.filter(pk__in=alertable) return Alert.objects.filter(pk__in=alertable)
...@@ -403,11 +404,11 @@ class FacilityViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -403,11 +404,11 @@ class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
closed_now = self.request.query_params.get('closed_now', None) closed_now = self.request.query_params.get('closed_now', None)
if open_now is not None or closed_now is not None: if open_now is not None or closed_now is not None:
# List of all open facilities # List of all open facilities
open_facilities = [] open_facilities = [
for facility in Facility.objects.all(): facility.pk
if facility.is_open(): for facility in Facility.objects.all()
# Append the primary key if facility.is_open()
open_facilities.append(facility.pk) ]
# Return all Facility objects with the primary keys located in the # Return all Facility objects with the primary keys located in the
# open_facilities list # open_facilities list
if open_now: if open_now:
...@@ -503,13 +504,13 @@ class ScheduleViewSet(viewsets.ModelViewSet): ...@@ -503,13 +504,13 @@ class ScheduleViewSet(viewsets.ModelViewSet):
the API. the API.
""" """
# List of all schedules that are outdated # List of all schedules that are outdated
filter_old_schedules = [] filter_old_schedules = [
for schedule in Schedule.objects.all(): schedule.pk
if schedule.valid_end and schedule.valid_start: for schedule in Schedule.objects.all()
# If the schedule ended before today # If the schedule ended before today
if schedule.valid_end < datetime.date.today(): if schedule.valid_end and schedule.valid_start
# Add it to the list of objects we are excluding if schedule.valid_end < datetime.date.today()
filter_old_schedules.append(schedule.pk) ]
# Return all Schedule objects that have not expired # Return all Schedule objects that have not expired
return Schedule.objects.exclude(pk__in=filter_old_schedules) return Schedule.objects.exclude(pk__in=filter_old_schedules)
......
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