Skip to content

Instantly share code, notes, and snippets.

@codeinthehole
Created September 4, 2012 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeinthehole/3618479 to your computer and use it in GitHub Desktop.
Save codeinthehole/3618479 to your computer and use it in GitHub Desktop.
Models for store opening times
from django.db import models
from django.utils.translation import ugettext_lazy as _
class WeekdayOpeningPeriod(models.Model):
store = models.ForeignKey('stores.Store', related_name='opening_periods')
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY = range(7)
weekday_choices = (
(SUNDAY, _("Sunday")),
(MONDAY, _("Monday")),
(TUESDAY, _("Tuesday")),
(WEDNESDAY, _("Wednesday")),
(THURSDAY, _("Thursday")),
(FRIDAY, _("Friday")),
(SATURDAY, _("Saturday")),
)
weekday = models.PositiveIntegerField(choices=weekday_choices)
start = models.TimeField()
end = models.TimeField()
class OpeningHoursOverride(models.Model):
"""
Override the opening hours for a given date
"""
store = models.ForeignKey('stores.Store', related_name='opening_periods')
date = models.DateField()
start = models.TimeField()
end = models.TimeField()
# Explain why normal hours are being overridden
description = models.TextField()
# To determine normal opening hours:
# 1. fetch all WeekdayOpeningPeriods ordered by weekday
# 2. fetch any OpeningHoursOverrides that occurs in the next x weeks
# 3. render this information
# To determine is a store is open at a given datetime:
# 1. look for override for date in question that covers datetime
# 2. if nothing, look for weekday opening for today
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment