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

Allow schedules to promote themselves to main

- done via a toggle
- will happen automatically when the schedule comes into effect

Closes #65
parent 8197992e
......@@ -169,15 +169,17 @@ class Facility(TimeStampedModel):
# Closed
return False
def clean_special_schedules(self):
def clean_schedules(self):
"""
Loop through every special_schedule and remove entries that have
expired.
expired as well as promote special schedules to main if necessary.
"""
for special_schedule in self.special_schedules.all():
# If it ends before today
if special_schedule.valid_end < datetime.date.today() and special_schedule.schedule_for_removal:
self.special_schedules.remove(special_schedule)
elif special_schedule.promote_to_main:
self.main_schedule = special_schedule
class Meta:
verbose_name = "facility"
......@@ -213,8 +215,14 @@ class Schedule(TimeStampedModel):
default=False, help_text="Toggle to True if the Facility is open 24 hours. You do not need to specify any Open Times, it will always be displayed as open.")
# Boolean for if this schedule should never be removed.
schedule_for_removal = models.BooleanField('Schedule for removal', blank=False,
default=True, help_text="Toggle to False if the schedule should never be removed in the backend. By default, all schedules are automatically deleted after they have expired.")
schedule_for_removal = models.BooleanField('Schedule for removal?',
blank=False, default=True,
help_text="Toggle to False if the schedule should never be removed in the backend. By default, all schedules are automatically deleted after they have expired.")
# Boolean for if this schedule should become the main schedule at the point
# it goes live
promote_to_main = models.BooleanField('Schedule for promotion?',
blank=False, default=False,
help_text="Upon the start of the schedule, it will be promoted to become the main schedule of the Facility it is attached to rather than a special schedule.")
def is_open_now(self):
"""
......
......@@ -66,7 +66,7 @@ class ScheduleSerializer(serializers.ModelSerializer):
model = Schedule
# List the fields that we are serializing
fields = ('id', 'open_times', 'modified', 'name', 'valid_start',
'valid_end', 'twenty_four_hours', 'schedule_for_removal')
'valid_end', 'twenty_four_hours', 'schedule_for_removal', 'promote_to_main')
class FacilitySerializer(serializers.HyperlinkedModelSerializer):
"""
......
......@@ -408,6 +408,11 @@ class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
open_now = self.request.query_params.get('open_now', None)
# Define ?closed_now
closed_now = self.request.query_params.get('closed_now', None)
# Clean the schedules in every Facility
for facility in Facility.objects.all():
facility.clean_schedules()
if open_now is not None or closed_now is not None:
# List of all open facilities
open_facilities = [
......@@ -425,9 +430,6 @@ class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
return Facility.objects.exclude(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):
......@@ -492,7 +494,8 @@ class ScheduleViewSet(viewsets.ModelViewSet):
'valid_start',
'valid_end',
'twenty_four_hours',
'schedule_for_removal'
'schedule_for_removal',
'promote_to_main'
)
# Associate a serializer with the ViewSet
......
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