models.py 5.25 KB
Newer Older
1
2
3
4
5
6
7
8
# Future Imports
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

# Python stdlib Imports
import datetime

# Django Imports
9
from django.db import models
10
from django.contrib.auth.models import User
Ben Waters's avatar
Ben Waters committed
11
from model_utils.models import TimeStampedModel
12
from autoslug import AutoSlugField
Tyler Hallada's avatar
Tyler Hallada committed
13

Ben Waters's avatar
Ben Waters committed
14
class Category(TimeStampedModel):
15
16
17
18
19
20
21
22
    name = models.CharField(max_length=100)

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

23
    def __str__(self):
24
        return '%s' % self.name
25

Ben Waters's avatar
Ben Waters committed
26
class Facility(TimeStampedModel):
Ben Waters's avatar
Ben Waters committed
27
    """Represents a facility location on campus."""
Tyler Hallada's avatar
Tyler Hallada committed
28
    name = models.CharField(max_length=100)
29
30
    slug = AutoSlugField(populate_from='name',unique=True)  # instead of id

31
    facility_category = models.ForeignKey('Category', related_name="facilities", null=True, blank=True)
Ben Waters's avatar
Ben Waters committed
32
    on_campus = models.BooleanField(default=True)
33
    location = models.CharField(max_length=100, null=True, blank=True)
34
35

    owners = models.ManyToManyField(User)
36
    main_schedule = models.ForeignKey('Schedule',
37
            related_name='facility_main')
38
    special_schedules = models.ManyToManyField('Schedule',
39
40
            related_name='facility_special', blank=True,
            help_text='This schedule will come into effect only for its specified duration.')
41
42
     
    class Meta:
43
44
        verbose_name = "facility"
        verbose_name_plural = "facilities"
45
46
        # Sort by name in admin view
        ordering = ['name']
47

48
49
    def isOpen(self):
        """
50
        Return true if this facility is currently open.
51
52
53
54
55

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

        """
56
57
58
59
60
61
62
63
        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
64
65
                    else:
                        return False
66
        if self.main_schedule.isOpenNow():
67
68
69
            return True
        return False

70
    def __str__(self):
71
72
        return self.name

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

98
    def __str__(self):
99
100
101
        return self.name


Ben Waters's avatar
Ben Waters committed
102
class OpenTime(TimeStampedModel):
103
    """Represents a period time when a Facility is open"""
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

    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'),
    )

123
    schedule = models.ForeignKey('Schedule', related_name='open_times')
124
    start_day = models.IntegerField(default=0, choices=DAY_CHOICES)  # 0-6, Monday == 0
125
    start_time = models.TimeField()
126
    end_day = models.IntegerField(default=0, choices=DAY_CHOICES)
127
128
129
130
    end_time = models.TimeField()

    def isOpenNow(self):
        """Return true if the current time is this OpenTime's range"""
131
        today = datetime.datetime.today()
132
133
134
135
136
137
138
139
140
141
142
        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
143
        else:
144
145
146
147
148
149
150
151
152
            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
153

154
    def __str__(self):
155
156
157
158
159
        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"))