arnaud briche
arnaud briche

Reputation: 1509

Any existing solution to implement "opening hours" in Django

I'm making a website for a client who wants to be able to change then opening hours for each of his different stores. Is there an existing solution for this type of problem with Django ?

Upvotes: 9

Views: 4167

Answers (1)

benjaoming
benjaoming

Reputation: 2215

What do you mean? Seems pretty simple. Adjust according to your weekday order. And if you like, add validation. But people should be smart enough to not need validation for that sort of stuff.

HOUR_OF_DAY_24 = [(i,i) for i in range(1,25)]

WEEKDAYS = [
  (1, _("Monday")),
  (2, _("Tuesday")),
  (3, _("Wednesday")),
  (4, _("Thursday")),
  (5, _("Friday")),
  (6, _("Saturday")),
  (7, _("Sunday")),
]

class OpeningHours(models.Model):
    store = models.ForeignKey("StoreModel")
    weekday_from = models.PositiveSmallIntegerField(choices=WEEKDAYS, unique=True)
    weekday_to = models.PositiveSmallIntegerField(choices=WEEKDAYS)
    from_hour = models.PositiveSmallIntegerField(choices=HOUR_OF_DAY_24)
    to_hour = models.PositiveSmallIntegerField(choices=HOUR_OF_DAY_24)

    def get_weekday_from_display(self):
        return WEEKDAYS[self.weekday_from]

    def get_weekday_to_display(self):
        return WEEKDAYS[self.weekday_to]

class SpecialDays(models.Model):
    holiday_date = models.DateField()
    closed = models.BooleanField(default=True)
    from_hour = models.PositiveSmallIntegerField(choices=HOUR_OF_DAY_24, null=True, blank=True)
    to_hour = models.PositiveSmallIntegerField(choices=HOUR_OF_DAY_24, null=True, blank=True)

Upvotes: 16

Related Questions