models.py 3 KB
Newer Older
1
from django.db import models
2
import datetime
Tyler Hallada's avatar
Tyler Hallada committed
3

4 5

class Restaurant(models.Model):
6
    """Represents a dining location on campus."""
Tyler Hallada's avatar
Tyler Hallada committed
7 8 9 10 11
    name = models.CharField(max_length=100)
    mainSchedule = models.ForeignKey('Schedule',
            related_name='restaurant_main')
    specialSchedules = models.ManyToManyField('Schedule',
            related_name='restaurant_special', null=True, blank=True)
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    def isOpen(self):
        """
        Return true if this restaurant is currently open.

        First checks any valid special schedules and then checks the
        main default schedule.

        """
        today = datetime.datetime.today()
        for schedule in self.specialSchedules.all():
            if schedule.dateValidStart <= today <= schedule.dateValidEnd:
                if schedule.isOpenNow():
                    return True
        if self.mainSchedule.isOpenNow():
            return True
        return False

30

Tyler Hallada's avatar
Tyler Hallada committed
31
class Schedule(models.Model):
32 33 34 35 36 37 38
    """
    Contains opening and closing times for each day in a week.

    For special (temporary) schedules, start and end dates for
    when this schedule will be valid can also be set.

    """
Tyler Hallada's avatar
Tyler Hallada committed
39
    name = models.CharField(max_length=100)
40
    # inclusive:
Tyler Hallada's avatar
Tyler Hallada committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    dateValidStart = models.DateField(null=True, blank=True)
    dateValidEnd = models.DateField(null=True, blank=True)
    monOpen = models.TimeField(null=True, blank=True)
    monClose = models.TimeField(null=True, blank=True)
    tueOpen = models.TimeField(null=True, blank=True)
    tueClose = models.TimeField(null=True, blank=True)
    wedOpen = models.TimeField(null=True, blank=True)
    wedClose = models.TimeField(null=True, blank=True)
    thuOpen = models.TimeField(null=True, blank=True)
    thuClose = models.TimeField(null=True, blank=True)
    friOpen = models.TimeField(null=True, blank=True)
    friClose = models.TimeField(null=True, blank=True)
    satOpen = models.TimeField(null=True, blank=True)
    satClose = models.TimeField(null=True, blank=True)
    sunOpen = models.TimeField(null=True, blank=True)
    sunClose = models.TimeField(null=True, blank=True)
57

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    def isOpenNow(self):
        """Return true if this schedule is open right now."""
        today = datetime.datetime.today()
        weekday = today.weekday()
        if weekday == 0:
            start = self.monOpen
            end = self.monClose
        elif weekday == 1:
            start = self.tueOpen
            end = self.tueClose
        elif weekday == 2:
            start = self.wedOpen
            end = self.wedClose
        elif weekday == 3:
            start = self.thuOpen
            end = self.thuClose
        elif weekday == 4:
            start = self.friOpen
            end = self.friClose
        elif weekday == 5:
            start = self.satOpen
            end = self.satClose
        elif weekday == 6:
            start = self.sunOpen
            end = self.sunClose
        if (start is not None and end is not None and
                start <= today.time() <= end):
            return True
        else:
            return False