Any existing solution to implement “opening hours” in Django

前端 未结 1 1866
情书的邮戳
情书的邮戳 2020-12-31 22:57

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 proble

相关标签:
1条回答
  • 2020-12-31 23:11

    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)
    
    0 讨论(0)
提交回复
热议问题