models.py 3.1 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
    name = models.CharField(max_length=100)
8
    main_schedule = models.ForeignKey('Schedule',
Tyler Hallada's avatar
Tyler Hallada committed
9
            related_name='restaurant_main')
10
    special_schedules = models.ManyToManyField('Schedule',
Tyler Hallada's avatar
Tyler Hallada committed
11
            related_name='restaurant_special', null=True, blank=True)
12

13 14 15 16 17 18 19 20
    def isOpen(self):
        """
        Return true if this restaurant is currently open.

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

        """
21 22 23 24 25 26 27 28 29
        today = datetime.datetime.today().date()
        # Check special schedules first
        for schedule in self.special_schedules.all():
            # Special schedules must have valid_start and valid_end set
            if schedule.valid_start and schedule.valid_end:
                if schedule.valid_start <= today <= schedule.valid_end:
                    if schedule.isOpenNow():
                        return True
        if self.main_schedule.isOpenNow():
30 31 32
            return True
        return False

33 34 35
    def __unicode__(self):
        return self.name

36

Tyler Hallada's avatar
Tyler Hallada committed
37
class Schedule(models.Model):
38 39 40 41 42 43 44
    """
    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
45
    name = models.CharField(max_length=100)
46
    # inclusive:
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    valid_start = models.DateField(null=True, blank=True)
    valid_end = models.DateField(null=True, blank=True)
    mon_open = models.TimeField(null=True, blank=True)
    mon_close = models.TimeField(null=True, blank=True)
    tue_open = models.TimeField(null=True, blank=True)
    tue_close = models.TimeField(null=True, blank=True)
    wed_open = models.TimeField(null=True, blank=True)
    wed_close = models.TimeField(null=True, blank=True)
    thu_open = models.TimeField(null=True, blank=True)
    thu_close = models.TimeField(null=True, blank=True)
    fri_open = models.TimeField(null=True, blank=True)
    fri_close = models.TimeField(null=True, blank=True)
    sat_open = models.TimeField(null=True, blank=True)
    sat_close = models.TimeField(null=True, blank=True)
    sun_open = models.TimeField(null=True, blank=True)
    sun_close = models.TimeField(null=True, blank=True)
63

64 65 66 67
    def isOpenNow(self):
        """Return true if this schedule is open right now."""
        today = datetime.datetime.today()
        weekday = today.weekday()
68 69 70 71 72 73 74 75 76
        days = [
                (self.mon_open, self.mon_close),
                (self.tue_open, self.tue_close),
                (self.wed_open, self.wed_close),
                (self.thu_open, self.thu_close),
                (self.fri_open, self.fri_close),
                (self.sat_open, self.sat_close),
                (self.sun_open, self.sun_close),
        ]
Tyler Hallada's avatar
Tyler Hallada committed
77
        start, end = days[weekday]  # Get opening/closing times for today
78 79 80 81 82
        if (start is not None and end is not None and
                start <= today.time() <= end):
            return True
        else:
            return False
83 84 85

    def __unicode__(self):
        return self.name