models.py 5.01 KB
Newer Older
1
from django.db import models
2
from django.contrib.auth.models import User
Ben Waters's avatar
Ben Waters committed
3
from model_utils.models import TimeStampedModel
4
from autoslug import AutoSlugField
5
import datetime
Tyler Hallada's avatar
Tyler Hallada committed
6

Ben Waters's avatar
Ben Waters committed
7
class Category(TimeStampedModel):
8
9
10
11
12
13
14
15
16
    name = models.CharField(max_length=100)

    class Meta:
        verbose_name = "category"
        verbose_name_plural = "categories"
        # Sort by name in admin view
        ordering = ['name']

    def __unicode__(self):
17
        return '%s' % self.name
18

Ben Waters's avatar
Ben Waters committed
19
class Facility(TimeStampedModel):
Ben Waters's avatar
Ben Waters committed
20
    """Represents a facility location on campus."""
Tyler Hallada's avatar
Tyler Hallada committed
21
    name = models.CharField(max_length=100)
22
23
    slug = AutoSlugField(populate_from='name',unique=True)  # instead of id

24
    facility_category = models.ForeignKey('Category', related_name="facilities", null=True, blank=True)
Ben Waters's avatar
Ben Waters committed
25
    on_campus = models.BooleanField(default=True)
26
    location = models.CharField(max_length=100, null=True, blank=True)
27
28

    owners = models.ManyToManyField(User)
29
    main_schedule = models.ForeignKey('Schedule',
30
            related_name='facility_main')
31
    special_schedules = models.ManyToManyField('Schedule',
32
            related_name='facility_special', blank=True)
33
34
     
    class Meta:
35
36
        verbose_name = "facility"
        verbose_name_plural = "facilities"
37
38
        # Sort by name in admin view
        ordering = ['name']
39

40
41
    def isOpen(self):
        """
42
        Return true if this facility is currently open.
43
44
45
46
47

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

        """
48
49
50
51
52
53
54
55
        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
56
57
                    else:
                        return False
58
        if self.main_schedule.isOpenNow():
59
60
61
            return True
        return False

62
63
64
    def __unicode__(self):
        return self.name

Ben Waters's avatar
Ben Waters committed
65
class Schedule(TimeStampedModel):
66
67
68
69
70
71
72
    """
    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
73
    name = models.CharField(max_length=100)
74
    # inclusive:
75
76
77
78
    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')
79
80
81
82
    
    class Meta:
        ordering = ['name']
        
83
84
    def isOpenNow(self):
        """Return true if this schedule is open right now."""
85
86
87
88
89
90
91
92
93
        for time in OpenTime.objects.filter(schedule=self):
            if time.isOpenNow():
                return True
        return False

    def __unicode__(self):
        return self.name


Ben Waters's avatar
Ben Waters committed
94
class OpenTime(TimeStampedModel):
95
    """Represents a period time when a Facility is open"""
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

    MONDAY = 0
    TUESDAY = 1
    WEDNESDAY = 2
    THURSDAY = 3
    FRIDAY = 4
    SATURDAY = 5
    SUNDAY = 6

    DAY_CHOICES = (
        (MONDAY, 'Monday'),
        (TUESDAY, 'Tuesday'),
        (WEDNESDAY, 'Wednesday'),
        (THURSDAY, 'Thursday'),
        (FRIDAY, 'Friday'),
        (SATURDAY, 'Saturday'),
        (SUNDAY, 'Sunday'),
    )

115
    schedule = models.ForeignKey('Schedule', related_name='open_times')
116
    start_day = models.IntegerField(default=0, choices=DAY_CHOICES)  # 0-6, Monday == 0
117
    start_time = models.TimeField()
118
    end_day = models.IntegerField(default=0, choices=DAY_CHOICES)
119
120
121
122
    end_time = models.TimeField()

    def isOpenNow(self):
        """Return true if the current time is this OpenTime's range"""
123
        today = datetime.datetime.today()
124
125
126
127
128
129
130
131
132
133
134
        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
135
        else:
136
137
138
139
140
141
142
143
144
            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
145
146

    def __unicode__(self):
147
148
149
150
151
        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"))