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

4

5 6 7 8 9
class BaseModel(models.Model):
    last_modified = models.DateTimeField('Last Modified', auto_now=True)


class Restaurant(BaseModel):
10
    """Represents a dining location on campus."""
Tyler Hallada's avatar
Tyler Hallada committed
11
    name = models.CharField(max_length=100)
12
    main_schedule = models.ForeignKey('Schedule',
Tyler Hallada's avatar
Tyler Hallada committed
13
            related_name='restaurant_main')
14
    special_schedules = models.ManyToManyField('Schedule',
Tyler Hallada's avatar
Tyler Hallada committed
15
            related_name='restaurant_special', null=True, blank=True)
16

17 18 19 20 21 22 23 24
    def isOpen(self):
        """
        Return true if this restaurant is currently open.

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

        """
25 26 27 28 29 30 31 32 33
        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():
34 35 36
            return True
        return False

37 38 39
    def __unicode__(self):
        return self.name

40

41
class Schedule(BaseModel):
42 43 44 45 46 47 48
    """
    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
49
    name = models.CharField(max_length=100)
50
    # inclusive:
51 52 53 54
    valid_start = models.DateField('Start Date', null=True, blank=True,
            help_text='Date that this schedule goes into effect')
    valid_end = models.DateField('End Date', null=True, blank=True,
            help_text='Last day that this schedule is in effect')
55

56 57
    def isOpenNow(self):
        """Return true if this schedule is open right now."""
58 59 60 61 62 63 64 65 66
        for time in OpenTime.objects.filter(schedule=self):
            if time.isOpenNow():
                return True
        return False

    def __unicode__(self):
        return self.name


67
class OpenTime(BaseModel):
68 69 70 71 72 73 74 75 76
    """Represents a period time when a Restaurant is open"""
    schedule = models.ForeignKey('Schedule', related_name='open_times')
    start_day = models.IntegerField()  # 0-6, Monday == 0
    start_time = models.TimeField()
    end_day = models.IntegerField()  # 0-6, Monday == 0
    end_time = models.TimeField()

    def isOpenNow(self):
        """Return true if the current time is this OpenTime's range"""
77
        today = datetime.datetime.today()
78 79 80 81 82 83 84 85 86 87 88
        if self.start_day <= self.end_day:
            if self.start_day == today.weekday():
                if self.start_time > today.time():
                    return False
            elif self.start_day > today.weekday():
                return False
            if self.end_day == today.weekday():
                if self.end_time < today.time():
                    return False
            elif self.end_day < today.weekday():
                return False
89
        else:
90 91 92 93 94 95 96 97 98
            if self.start_day == today.weekday():
                if self.start_time > today.time():
                    return False
            if self.end_day == today.weekday():
                if self.end_time < today.time():
                    return False
            if self.end_day < today.weekday() < self.start_day:
                return False
        return True
99 100

    def __unicode__(self):
101 102 103 104 105
        weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
                'Saturday', 'Sunday']
        return '%s %s to %s %s' % (weekdays[self.start_day],
                self.start_time.strftime("%H:%M:%S"), weekdays[self.end_day],
                self.end_time.strftime("%H:%M:%S"))
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144


def export_data():
    restaurants = list()
    for restaurant in Restaurant.objects.all():
        restaurant_data = {'name': restaurant.name}
        open_times = list()
        for time in restaurant.main_schedule.open_times.all():
            open_times.append({
                    'start_day': time.start_day,
                    'start_time': time.start_time.isoformat(),
                    'end_day': time.end_day,
                    'end_time': time.end_time.isoformat()
            })
        restaurant_data['main_schedule'] = {
                'name': restaurant.main_schedule.name,
                'id': restaurant.id,
                'open_times': open_times
        }
        special_schedules = list()
        for schedule in restaurant.special_schedules.all():
            open_times = list()
            for time in schedule.open_times.all():
                open_times.append({
                        'start_day': time.start_day,
                        'start_time': time.start_time.isoformat(),
                        'end_day': time.end_day,
                        'end_time': time.end_time.isoformat()
                })
            special_schedules.append({
                    'name': schedule.name,
                    'id': restaurant.id,
                    'start': schedule.valid_start.isoformat(),
                    'end': schedule.valid_end.isoformat(),
                    'open_times': open_times
            })
        restaurant_data['special_schedules'] = special_schedules
        restaurants.append(restaurant_data)
    return restaurants