Commit 50ed6039 authored by David Haynes's avatar David Haynes 🙆
Browse files

Merge branch '65-promote-special-to-main' into '2.1-dev'

Resolve "Allow a schedule to become a normal schedule on a certain date"

See merge request srct/whats-open!34
parents 9bb565bc 8416d22c
Pipeline #1801 passed with stage
in 1 minute and 52 seconds
...@@ -43,6 +43,16 @@ class OpenTimeInline(admin.TabularInline): ...@@ -43,6 +43,16 @@ class OpenTimeInline(admin.TabularInline):
model = OpenTime model = OpenTime
# 7 days of the week, so only have 7 rows # 7 days of the week, so only have 7 rows
max_num = 7 max_num = 7
extra = 7
# We are basically reordering things to look nicer to the user here
fieldsets = (
(None, {
'fields': (
('start_day', 'start_time'),
('end_day', 'end_time')
),
}),
)
class ScheduleAdmin(admin.ModelAdmin): class ScheduleAdmin(admin.ModelAdmin):
""" """
...@@ -64,7 +74,8 @@ class ScheduleAdmin(admin.ModelAdmin): ...@@ -64,7 +74,8 @@ class ScheduleAdmin(admin.ModelAdmin):
# line # line
('valid_start', 'valid_end'), ('valid_start', 'valid_end'),
'twenty_four_hours', 'twenty_four_hours',
'schedule_for_removal') 'schedule_for_removal',
'promote_to_main')
}), }),
) )
......
...@@ -169,15 +169,18 @@ class Facility(TimeStampedModel): ...@@ -169,15 +169,18 @@ class Facility(TimeStampedModel):
# Closed # Closed
return False return False
def clean_special_schedules(self): def clean_schedules(self):
""" """
Loop through every special_schedule and remove entries that have 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(): for special_schedule in self.special_schedules.all():
# If it ends before today # If it ends before today
if special_schedule.valid_end < datetime.date.today() and special_schedule.schedule_for_removal: if special_schedule.valid_end < datetime.date.today() and special_schedule.schedule_for_removal:
self.special_schedules.remove(special_schedule) self.special_schedules.remove(special_schedule)
elif special_schedule.promote_to_main:
if special_schedule.valid_start < datetime.date.today() and special_schedule.valid_end >= datetime.date.today():
self.main_schedule = special_schedule
class Meta: class Meta:
verbose_name = "facility" verbose_name = "facility"
...@@ -213,8 +216,14 @@ class Schedule(TimeStampedModel): ...@@ -213,8 +216,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.") 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. # Boolean for if this schedule should never be removed.
schedule_for_removal = models.BooleanField('Schedule for removal', blank=False, schedule_for_removal = models.BooleanField('Schedule for removal?',
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.") 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): def is_open_now(self):
""" """
......
...@@ -66,7 +66,7 @@ class ScheduleSerializer(serializers.ModelSerializer): ...@@ -66,7 +66,7 @@ class ScheduleSerializer(serializers.ModelSerializer):
model = Schedule model = Schedule
# List the fields that we are serializing # List the fields that we are serializing
fields = ('id', 'open_times', 'modified', 'name', 'valid_start', 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): class FacilitySerializer(serializers.HyperlinkedModelSerializer):
""" """
......
...@@ -408,6 +408,11 @@ class FacilityViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -408,6 +408,11 @@ class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
open_now = self.request.query_params.get('open_now', None) open_now = self.request.query_params.get('open_now', None)
# Define ?closed_now # Define ?closed_now
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
for facility in Facility.objects.all():
facility.clean_schedules()
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 = [
...@@ -425,9 +430,6 @@ class FacilityViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -425,9 +430,6 @@ class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
return Facility.objects.exclude(pk__in=open_facilities) return Facility.objects.exclude(pk__in=open_facilities)
# Default behavior # Default behavior
else: else:
for facility in Facility.objects.all():
# Remove all special_schedules that have expired
facility.clean_special_schedules()
return Facility.objects.all() return Facility.objects.all()
class ScheduleViewSet(viewsets.ModelViewSet): class ScheduleViewSet(viewsets.ModelViewSet):
...@@ -492,7 +494,8 @@ class ScheduleViewSet(viewsets.ModelViewSet): ...@@ -492,7 +494,8 @@ class ScheduleViewSet(viewsets.ModelViewSet):
'valid_start', 'valid_start',
'valid_end', 'valid_end',
'twenty_four_hours', 'twenty_four_hours',
'schedule_for_removal' 'schedule_for_removal',
'promote_to_main'
) )
# Associate a serializer with the ViewSet # 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