models.py 5.11 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
33
            related_name='facility_special', blank=True,
            help_text='This schedule will come into effect only for its specified duration.')
34
35
     
    class Meta:
36
37
        verbose_name = "facility"
        verbose_name_plural = "facilities"
38
39
        # Sort by name in admin view
        ordering = ['name']
40

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

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

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

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

Ben Waters's avatar
Ben Waters committed
66
class Schedule(TimeStampedModel):
67
68
69
70
71
72
73
    """
    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
74
    name = models.CharField(max_length=100)
75
    # inclusive:
76
77
78
79
    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')
80
81
82
83
    
    class Meta:
        ordering = ['name']
        
84
85
    def isOpenNow(self):
        """Return true if this schedule is open right now."""
86
87
88
89
90
91
92
93
94
        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
95
class OpenTime(TimeStampedModel):
96
    """Represents a period time when a Facility is open"""
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

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

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

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

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