models.py 4.56 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
8
9
class Category(TimeStampedModel):
    name = models.CharField(max_length=100)

10
11
12
13
14
15
16
    class Meta:
        verbose_name = "Categories"
        ordering = ['name']

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

Ben Waters's avatar
Ben Waters committed
17
class Facility(TimeStampedModel):
Ben Waters's avatar
Ben Waters committed
18
    """Represents a facility location on campus."""
Tyler Hallada's avatar
Tyler Hallada committed
19
    name = models.CharField(max_length=100)
Ben Waters's avatar
Ben Waters committed
20
    owners = models.ManyToManyField(User)
Ben Waters's avatar
Ben Waters committed
21
    category = models.ForeignKey('Category')
22
    slug = AutoSlugField(populate_from='name',unique=True)
Ben Waters's avatar
Ben Waters committed
23
    on_campus = models.BooleanField(default=True)
24
    location = models.CharField(max_length=100, null=True, blank=True)
25
    main_schedule = models.ForeignKey('Schedule',
26
            related_name='facility_main')
27
    special_schedules = models.ManyToManyField('Schedule',
28
            related_name='facility_special', null=True, blank=True)
29
30
     
    class Meta:
31
32
        verbose_name = "facility"
        verbose_name_plural = "facilities"
33
34
        # Sort by name in admin view
        ordering = ['name']
35

36
37
    def isOpen(self):
        """
38
        Return true if this facility is currently open.
39
40
41
42
43

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

        """
44
45
46
47
48
49
50
51
        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
52
53
                    else:
                        return False
54
        if self.main_schedule.isOpenNow():
55
56
57
            return True
        return False

58
59
60
    def __unicode__(self):
        return self.name

Ben Waters's avatar
Ben Waters committed
61
class Schedule(TimeStampedModel):
62
63
64
65
66
67
68
    """
    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
69
    name = models.CharField(max_length=100)
70
    # inclusive:
71
72
73
74
    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')
75
76
77
78
    
    class Meta:
        ordering = ['name']
        
79
80
    def isOpenNow(self):
        """Return true if this schedule is open right now."""
81
82
83
84
85
86
87
88
89
        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
90
class OpenTime(TimeStampedModel):
91
    """Represents a period time when a Facility is open"""
92
    schedule = models.ForeignKey('Schedule', related_name='open_times')
Benjamin S Waters's avatar
Benjamin S Waters committed
93
94
95
96
97
    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()
98
99
100

    def isOpenNow(self):
        """Return true if the current time is this OpenTime's range"""
101
        today = datetime.datetime.today()
102
103
104
105
106
107
108
109
110
111
112
        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
113
        else:
114
115
116
117
118
119
120
121
122
            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
123
124

    def __unicode__(self):
125
126
127
128
129
        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"))