models.py 4.65 KB
Newer Older
1
2
# standard library imports
from __future__ import absolute_import, print_function
3
# core django imports
4
from django.db import models
5
from django.core.urlresolvers import reverse
6
from django.utils.text import slugify
7
from django.core.validators import RegexValidator
8
# third party imports
Daniel W Bond's avatar
Daniel W Bond committed
9
from model_utils.models import TimeStampedModel
10
from randomslugfield import RandomSlugField
11
from autoslug import AutoSlugField
Daniel W Bond's avatar
Daniel W Bond committed
12

13

Daniel W Bond's avatar
Daniel W Bond committed
14
class Building(TimeStampedModel):
15
    name = models.CharField(max_length=100, unique=True)
16
17
18
19
20
21

    NONE = 'na'
    AQUIA = 'aq'
    RAPPAHANNOCK = 'ra'
    SHENANDOAH = 'sh'
    NEIGHBOURHOOD_CHOICES = (
22
        (NONE, '---'),
23
24
25
26
27
28
        (AQUIA, 'Aquia'),
        (RAPPAHANNOCK, 'Rappahannock'),
        (SHENANDOAH, 'Shenandoah'),
    )

    neighbourhood = models.CharField(max_length=100, choices=NEIGHBOURHOOD_CHOICES,
Daniel W Bond's avatar
Daniel W Bond committed
29
30
31
                                     default=NONE)

    # address = models.ForeignKey('Address')
32
33
34
35
36

    PRINCE_WILLIAM = 'pw'
    MASONVALE = 'mv'
    FAIRFAX = 'ff'
    CAMPUS_CHOICES = (
37
        (NONE, '---'),
38
        (PRINCE_WILLIAM, 'Prince William'),
Daniel W Bond's avatar
Daniel W Bond committed
39
        (MASONVALE, 'MasonVale'),
40
41
42
        (FAIRFAX, 'Fairfax'),
    )

43
    campus = models.CharField(max_length=100, choices=CAMPUS_CHOICES, default='ff')
44

45
46
    slug = RandomSlugField(length=6)
    building_name = AutoSlugField(populate_from='name')
Daniel W Bond's avatar
Daniel W Bond committed
47

Daniel W Bond's avatar
Daniel W Bond committed
48
    def get_absolute_url(self):
49
        return reverse('detail_building', kwargs={
50
            'building': slugify(self.building_name),
51
        })
Daniel W Bond's avatar
Daniel W Bond committed
52

Daniel W Bond's avatar
Daniel W Bond committed
53
    def __str__(self):  # __unicode__ on Python 2
54
        return self.name
Daniel W Bond's avatar
Daniel W Bond committed
55
56

    def __unicode__(self):  # __unicode__ on Python 2
57
        return unicode(self.name)
Daniel W Bond's avatar
Daniel W Bond committed
58

59
60
61
    def __contains__(self, obj):
        if isinstance(obj, Floor):
            if obj.building == self:  # floor.building == building
62
63
64
                return True
            else:
                return False
65
66
67
68
69
70
71
72
73
74
75
76
77
        elif isinstance(obj, Room):
            if obj.floor.building == self:  # room.floor.building == building
                return True
            else:
                return False
        else:
            try:
                if obj.room.floor.building == self:  # student.room.floor.building == building
                    return True
                else:
                    return False
            except:
                return False
78

Daniel W Bond's avatar
Daniel W Bond committed
79
80
    class Meta:
        ordering = ['name']
81

Daniel W Bond's avatar
Daniel W Bond committed
82

Daniel W Bond's avatar
Daniel W Bond committed
83
84
class Floor(TimeStampedModel):
    building = models.ForeignKey('Building')
85
86
    number = models.CharField(max_length=1,  # Mason doesn't like tall buildings
                              validators=[RegexValidator(regex='^[0-9]{1}',)])
Daniel W Bond's avatar
Daniel W Bond committed
87

88
89
    slug = RandomSlugField(length=6)

Daniel W Bond's avatar
Daniel W Bond committed
90
    floor_num = AutoSlugField(populate_from='number',)  # unique_with='building')
Daniel W Bond's avatar
Daniel W Bond committed
91

Daniel W Bond's avatar
Daniel W Bond committed
92
93
    def get_absolute_url(self):
        return reverse('detail_floor', kwargs={
94
            'building': slugify(self.building.building_name),
Daniel W Bond's avatar
Daniel W Bond committed
95
            'floor': self.floor_num,
Daniel W Bond's avatar
Daniel W Bond committed
96
97
        })

Daniel W Bond's avatar
Daniel W Bond committed
98
    def __str__(self):  # __unicode__ on Python 2
Daniel W Bond's avatar
Daniel W Bond committed
99
100
        return self.building.__str__()+" "+self.number.__str__()

101
102
103
    def __contains__(self, obj):  # circular imports with Student
        if isinstance(obj, Room):
            if obj.floor == self:  # room.floor == room
104
105
106
                return True
            else:
                return False
107
108
109
110
111
112
113
114
        else:
            try:
                if obj.room.floor == self:  # student.room.floor == floor
                    return True
                else:
                    return False
            except:
                return False
115

Daniel W Bond's avatar
Daniel W Bond committed
116
    class Meta:
117
        ordering = ['building', 'number']
118
        unique_together = ('number', 'building')
Daniel W Bond's avatar
Daniel W Bond committed
119

Daniel W Bond's avatar
Daniel W Bond committed
120

Daniel W Bond's avatar
Daniel W Bond committed
121
class Room(TimeStampedModel):
122
123
124
    number = models.CharField(max_length=5,
                              validators=[RegexValidator(regex='^[A-Z]?[0-9]{3,4}',)])

Daniel W Bond's avatar
Daniel W Bond committed
125
    floor = models.ForeignKey('Floor')
126

127
128
    slug = RandomSlugField(length=6)

Daniel W Bond's avatar
Daniel W Bond committed
129
    room_num = AutoSlugField(populate_from='number',)  # unique_with='floor')
Daniel W Bond's avatar
Daniel W Bond committed
130

131
132
133
134
135
136
    def get_wing(self):
        if self.number[0].isalpha():
            return self.number[0]
        else:
            return None

Daniel W Bond's avatar
Daniel W Bond committed
137
138
    def get_absolute_url(self):
        return reverse('detail_room', kwargs={
Daniel W Bond's avatar
Daniel W Bond committed
139
            'floor': self.floor.floor_num,
140
            'building': slugify(self.floor.building.building_name),
Daniel W Bond's avatar
Daniel W Bond committed
141
            'room': self.room_num,
Daniel W Bond's avatar
Daniel W Bond committed
142
143
        })

Daniel W Bond's avatar
Daniel W Bond committed
144
    def __str__(self):  # __unicode__ on Python 2
Daniel W Bond's avatar
Daniel W Bond committed
145
        return self.floor.building.__str__()+" "+self.number.__str__()
146

147
    def __contains__(self, obj):  # circular imports with Student
148
        try:
149
            if obj.room == self:  # student.room == room
150
151
152
153
154
155
                return True
            else:
                return False
        except:
            return False

Daniel W Bond's avatar
Daniel W Bond committed
156
157
    class Meta:
        ordering = ['number']
158
        unique_together = ('number', 'floor',)