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

5
6

class Restaurant(models.Model):
7
    """Represents a dining location on campus."""
Tyler Hallada's avatar
Tyler Hallada committed
8
9
10
11
12
    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)
13

Tyler Hallada's avatar
Tyler Hallada committed
14
15
16
    def save(self, *args, **kwargs):
        cache.clear()  # Invalidate cache on restaurant change/creation
        super(Restaurant, self).save(*args, **kwargs)
17

Tyler Hallada's avatar
Tyler Hallada committed
18
19
20
    def delete(self, *args, **kwargs):
        cache.clear()  # Invalidate cache on restuarant deletion
        super(Restaurant, self).delete(*args, **kwargs)
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    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

39

Tyler Hallada's avatar
Tyler Hallada committed
40
class Schedule(models.Model):
41
42
43
44
45
46
47
    """
    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
48
    name = models.CharField(max_length=100)
49
    # inclusive:
Tyler Hallada's avatar
Tyler Hallada committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    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)
66

Tyler Hallada's avatar
Tyler Hallada committed
67
68
69
    def save(self, *args, **kwargs):
        cache.clear()  # Invalidate cache on schedule change/creation
        super(Schedule, self).save(*args, **kwargs)
70

Tyler Hallada's avatar
Tyler Hallada committed
71
72
73
    def delete(self, *args, **kwargs):
        cache.clear()  # Invalidate cache on schedule deletion
        super(Schedule, self).delete(*args, **kwargs)
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

    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