Commit 971736fe authored by David Haynes's avatar David Haynes 🙆

Fix django timezone usage

Closes #88
parent acbee882
Pipeline #3727 passed with stage
in 1 minute and 5 seconds
...@@ -8,7 +8,7 @@ the API. ...@@ -8,7 +8,7 @@ the API.
https://docs.djangoproject.com/en/1.11/topics/db/models/ https://docs.djangoproject.com/en/1.11/topics/db/models/
""" """
# Python stdlib Imports # Python Imports
import datetime import datetime
# Django Imports # Django Imports
...@@ -57,7 +57,6 @@ class Location(TimeStampedModel): ...@@ -57,7 +57,6 @@ class Location(TimeStampedModel):
""" """
CAMPUS_LOCATIONS = ( CAMPUS_LOCATIONS = (
# (set in model, human readable version)
("front royal", "Front Royal"), ("front royal", "Front Royal"),
("prince william", "Prince William County Science and Technology"), ("prince william", "Prince William County Science and Technology"),
("fairfax", "Fairfax"), ("fairfax", "Fairfax"),
...@@ -198,30 +197,16 @@ class Facility(TimeStampedModel): ...@@ -198,30 +197,16 @@ class Facility(TimeStampedModel):
First checks any valid special schedules and then checks the main, First checks any valid special schedules and then checks the main,
default, schedule. default, schedule.
""" """
# Get the current date today = timezone.now()
today = datetime.datetime.today().date() # Check special schedules first
# Check special schedules first, loop through all of them
for schedule in self.special_schedules.all(): for schedule in self.special_schedules.all():
# Special schedules must have valid_start and valid_end set # Special schedules must have valid_start and valid_end set
if schedule.valid_start and schedule.valid_end: if schedule.valid_start and schedule.valid_end:
# If a special schedule in in effect
if schedule.valid_start <= today <= schedule.valid_end: if schedule.valid_start <= today <= schedule.valid_end:
# Check if the facility is open or not based on that return schedule.is_open_now()
# special schedule
if schedule.is_open_now(): # If no special schedule is in effect then check the main_schedule
# Open return self.main_schedule.is_open_now()
return True
else:
# Closed
return False
# If no special schedule is in effect then check if the facility is
# open using the main_schedule
if self.main_schedule.is_open_now():
# Open
return True
else:
# Closed
return False
def clean_schedules(self): def clean_schedules(self):
""" """
...@@ -229,9 +214,9 @@ class Facility(TimeStampedModel): ...@@ -229,9 +214,9 @@ class Facility(TimeStampedModel):
expired as well as promote special schedules to main if necessary. expired as well as promote special schedules to main if necessary.
""" """
for special_schedule in self.special_schedules.all(): for special_schedule in self.special_schedules.all():
# If it ends before today
if ( if (
special_schedule.valid_end < timezone.now() special_schedule.valid_end is not None
and special_schedule.valid_end < timezone.now()
and special_schedule.schedule_for_removal and special_schedule.schedule_for_removal
): ):
self.special_schedules.remove(special_schedule) self.special_schedules.remove(special_schedule)
...@@ -312,14 +297,10 @@ class Schedule(TimeStampedModel): ...@@ -312,14 +297,10 @@ class Schedule(TimeStampedModel):
# If the schedule is a 24 hour one, then it's open. # If the schedule is a 24 hour one, then it's open.
if self.twenty_four_hours: if self.twenty_four_hours:
return True return True
# Otherwise let's check if it's open.
else: else:
# Loop through all the open times that correspond to this schedule # Loop through all the open times that correspond to this schedule
for open_time in OpenTime.objects.filter(schedule=self): for open_time in OpenTime.objects.filter(schedule=self):
# If the current time we are looking at is open, then the schedule
# will say that the facility is open
if open_time.is_open_now(): if open_time.is_open_now():
# Open
return True return True
# Closed (all open times are not open) # Closed (all open times are not open)
return False return False
...@@ -383,6 +364,7 @@ class OpenTime(TimeStampedModel): ...@@ -383,6 +364,7 @@ class OpenTime(TimeStampedModel):
""" """
# Get the current datetime # Get the current datetime
today = datetime.datetime.today() today = datetime.datetime.today()
# Check that the start occurs before the end # Check that the start occurs before the end
if self.start_day <= self.end_day: if self.start_day <= self.end_day:
# If today is the start_day # If today is the start_day
...@@ -405,7 +387,7 @@ class OpenTime(TimeStampedModel): ...@@ -405,7 +387,7 @@ class OpenTime(TimeStampedModel):
elif self.end_day < today.weekday(): elif self.end_day < today.weekday():
# Closed # Closed
return False return False
# The end_day > start_day # The start_day > end_day
else: else:
# If today is the start_day # If today is the start_day
if self.start_day == today.weekday(): if self.start_day == today.weekday():
...@@ -424,7 +406,7 @@ class OpenTime(TimeStampedModel): ...@@ -424,7 +406,7 @@ class OpenTime(TimeStampedModel):
if self.end_day < today.weekday() < self.start_day: if self.end_day < today.weekday() < self.start_day:
# Closed # Closed
return False return False
# All checks passed, it's Open # All checks passed, it's open
return True return True
def __str__(self): def __str__(self):
...@@ -501,4 +483,3 @@ class Alert(TimeStampedModel): ...@@ -501,4 +483,3 @@ class Alert(TimeStampedModel):
String representation of an Alert object. String representation of an Alert object.
""" """
return "{0} \n {1} \n {3}".format(self.subject, self.body, self.url) return "{0} \n {1} \n {3}".format(self.subject, self.body, self.url)
# Returns the subject, body, and url fields
...@@ -109,7 +109,6 @@ class FacilitySerializer(serializers.HyperlinkedModelSerializer): ...@@ -109,7 +109,6 @@ class FacilitySerializer(serializers.HyperlinkedModelSerializer):
main_schedule = ScheduleSerializer(many=False, read_only=True) main_schedule = ScheduleSerializer(many=False, read_only=True)
special_schedules = ScheduleSerializer(many=True, read_only=True) special_schedules = ScheduleSerializer(many=True, read_only=True)
facility_product_tags = TagListSerializerField() facility_product_tags = TagListSerializerField()
facility_classifier = TagListSerializerField()
class Meta: class Meta:
# Choose the model to be serialized # Choose the model to be serialized
......
...@@ -430,23 +430,21 @@ class FacilityViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -430,23 +430,21 @@ class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
closed_now = self.request.query_params.get("closed_now", None) closed_now = self.request.query_params.get("closed_now", None)
# Clean the schedules in every Facility # Clean the schedules in every Facility
for facility in Facility.objects.all(): # for facility in Facility.objects.all():
facility.clean_schedules() # facility.clean_schedules()
if open_now is not None or closed_now is not None: if open_now is not None:
# List of all open facilities open_now = [
open_facilities = [
facility.pk for facility in Facility.objects.all() if facility.is_open() facility.pk for facility in Facility.objects.all() if facility.is_open()
] ]
# Return all Facility objects with the primary keys located in the return Facility.objects.filter(pk__in=open_now)
# open_facilities list elif closed_now is not None:
if open_now: closed_now = [
return Facility.objects.filter(pk__in=open_facilities) facility.pk
# Return all Facility objects with the primary keys not located in for facility in Facility.objects.all()
# the open_facilities list if not facility.is_open()
elif closed_now: ]
return Facility.objects.exclude(pk__in=open_facilities) return Facility.objects.filter(pk__in=closed_now)
# Default behavior
else: else:
return Facility.objects.all() return Facility.objects.all()
......
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