-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
443 lines (356 loc) · 15.2 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
from django.db import models, IntegrityError
from django.contrib.localflavor.us.models import *
from batch_select.models import BatchManager
from stdimage import StdImageField
# Base models
class Organization(models.Model):
name = models.CharField("Name", max_length=30, unique=True)
def __unicode__(self):
return self.name
def clean(self):
for field in self._meta.fields:
if isinstance(field, (models.CharField, models.TextField)):
setattr(self, field.name, getattr(self, field.name).strip())
class Meta:
ordering = ['name']
class Program(models.Model):
name = models.CharField("Name", max_length=20, unique=True)
longname = models.CharField("Long name", max_length=50)
org = models.ForeignKey(Organization)
grade_start = models.PositiveIntegerField("Starting grade (1-12)")
grade_end = models.PositiveIntegerField("Ending grade (1-12)")
def __unicode__(self):
return self.name
def clean(self):
for field in self._meta.fields:
if isinstance(field, (models.CharField, models.TextField)):
setattr(self, field.name, getattr(self, field.name).strip())
class Meta:
ordering = ['name']
class Team(models.Model):
name = models.CharField("Name", max_length=20, unique=True)
startdate = models.DateField("Start date")
program = models.ForeignKey(Program)
reg_show = models.BooleanField("Show on registration form", default=True)
reg_default = models.BooleanField("Default team for registering students", default=False)
def __unicode__(self):
return self.name
def clean(self):
for field in self._meta.fields:
if isinstance(field, (models.CharField, models.TextField)):
setattr(self, field.name, getattr(self, field.name).strip())
class Meta:
ordering = ['name']
class School(models.Model):
name = models.CharField("Name", max_length=20, unique=True)
longname = models.CharField("Long name", max_length=50)
type = models.IntegerField("Type", choices=(
(0, "Elementary School"),
(1, "Middle School"),
(2, "High School"),
(3, "Homeschool"),
))
def __unicode__(self):
return "%s" % self.longname
def clean(self):
for field in self._meta.fields:
if isinstance(field, (models.CharField, models.TextField)):
setattr(self, field.name, getattr(self, field.name).strip())
class Meta:
ordering = ['longname']
class Company(models.Model):
name = models.CharField("Name", max_length=50, unique=True)
def __unicode__(self):
return self.name
def clean(self):
for field in self._meta.fields:
if isinstance(field, (models.CharField, models.TextField)):
setattr(self, field.name, getattr(self, field.name).strip())
class Meta:
verbose_name_plural = "Companies"
ordering = ['name']
class Email(models.Model):
email = models.EmailField("Email", max_length=255, unique=True)
location = models.CharField("Location", max_length=10, choices=(
('Home', "Home"),
('Work', "Work"),
('Other', "Other"),
))
def __unicode__(self):
return self.email
def clean(self):
for field in self._meta.fields:
if isinstance(field, (models.CharField, models.TextField)):
setattr(self, field.name, getattr(self, field.name).strip())
class Meta:
ordering = ['email']
class Address(models.Model):
line1 = models.CharField("Line 1", max_length=50)
line2 = models.CharField("Line 2", max_length=50, blank=True)
city = models.CharField("City", max_length=50)
state = USStateField("State")
zipcode = models.CharField("Zip Code", max_length=10, blank=True)
def __unicode__(self):
return "%s, %s, %s" % (self.line1, self.city, self.state)
def clean(self):
for field in self._meta.fields:
if isinstance(field, (models.CharField, models.TextField)):
setattr(self, field.name, getattr(self, field.name).strip())
class Meta:
verbose_name_plural = "Addresses"
ordering = ['line1']
class Phone(models.Model):
phone = PhoneNumberField("Phone Number")
ext = models.CharField("Extension", max_length=5, blank=True)
LOCATION_CHOICES=(
('Home', "Home"),
('Mobile', "Cell"),
('Work', "Work"),
('Other', "Other"),
)
location = models.CharField("Type", max_length=10, choices=LOCATION_CHOICES)
def __unicode__(self):
if self.ext:
return "%s x%s (%s)" % (self.phone, self.ext,
self.get_location_display())
else:
return "%s (%s)" % (self.phone, self.get_location_display())
def render_normal(self):
if self.ext:
return "%s x%s" % (self.phone, self.ext)
else:
return self.phone
def clean(self):
for field in self._meta.fields:
if isinstance(field, (models.CharField, models.TextField)):
setattr(self, field.name, getattr(self, field.name).strip())
class Meta:
ordering = ['phone']
unique_together = ['phone', 'ext']
class RelationshipType(models.Model):
type = models.CharField(max_length=30, unique=True)
parent = models.BooleanField("Parent", default=False)
sort_order = models.IntegerField("Sort order")
inverse = models.ForeignKey('self', null=True, blank=True)
def __unicode__(self):
return self.type
def clean(self):
for field in self._meta.fields:
if isinstance(field, (models.CharField, models.TextField)):
setattr(self, field.name, getattr(self, field.name).strip())
class Meta:
ordering = ['sort_order']
class Person(models.Model):
firstname = models.CharField("First Name", max_length=100)
lastname = models.CharField("Last Name", max_length=100)
suffix = models.CharField("Suffix", max_length=10, blank=True)
nickname = models.CharField("Nickname", max_length=50, blank=True)
GENDER_CHOICES=(
('M', "Male"),
('F', "Female"),
)
gender = models.CharField("Gender", max_length=1, choices=GENDER_CHOICES)
company = models.ForeignKey(Company, null=True, blank=True)
school = models.ForeignKey(School, null=True, blank=True)
grad_year = models.IntegerField("Graduation year", null=True, blank=True)
legacy_badge = models.IntegerField("Legacy badge", null=True, blank=True)
photo = StdImageField(upload_to='badge', blank=True)
teams = models.ManyToManyField(Team, through='PersonTeam', blank=True)
SHIRT_SIZE_CHOICES=(
('YS', "Youth Small"),
('YM', "Youth Medium"),
('YL', "Youth Large"),
('S', "Small"),
('M', "Medium"),
('L', "Large"),
('XL', "XL"),
('XXL', "XXL"),
)
shirt_size = models.CharField("Shirt Size", max_length=3, blank=True,
choices=SHIRT_SIZE_CHOICES)
medical = models.TextField("Medical Concerns", blank=True)
medications = models.TextField("Medications", blank=True)
birth_year = models.IntegerField(null=True, blank=True)
birth_month = models.IntegerField(null=True, blank=True)
birth_day = models.IntegerField(null=True, blank=True)
prospective_source = models.CharField("Prospective Source",
max_length=255, blank=True)
comments = models.TextField("Comments", blank=True)
position = models.CharField(max_length=50, blank=True)
emails = models.ManyToManyField(Email, through='PersonEmail', blank=True)
addresses = models.ManyToManyField(Address, blank=True)
phones = models.ManyToManyField(Phone, through='PersonPhone', blank=True)
def __unicode__(self):
if self.suffix:
return "%s %s %s" % (self.get_firstname(), self.lastname,
self.suffix)
else:
return "%s %s" % (self.get_firstname(), self.lastname)
def render_normal(self):
return self.__unicode__()
def get_badge(self):
if self.legacy_badge:
return self.legacy_badge
else:
return self.id+2000
def get_firstname(self):
if self.nickname:
return self.nickname
else:
return self.firstname
get_firstname.short_description = 'First Name'
def is_student(self, parent_relationships=None):
# Classify person as a student or an adult: this is somewhat
# complicated as a person can be on multiple teams, be an alumnus,
# or even be a student on one team and a mentor on another.
# The algorithm used here uses the following priority:
# 1) non-alumnus student role on any team => student
# 2) graduation year in future => student
# 3) graduation year in past => adult
# 4) age > 20 => adult
# 5) any non-student role on any team => adult
# 6) any parent relationships => student
if parent_relationships is None:
parent_relationships = RelationshipType.objects.filter(parent=True)\
.values_list('id', flat=True)
student = None
from datetime import date
today = date.today()
# graduation year
if (student is None and self.grad_year and
self.grad_year > 100 and self.grad_year > today.year):
student = True
if (student is None and self.grad_year and
self.grad_year > 100 and self.grad_year < today.year):
student = False
# age
if (student is None and self.birth_year and
self.birth_year > 100 and
(today.year - self.birth_year) > 20):
student = False
# roles
for x in PersonTeam.objects.filter(person=self):
if student is None and x.role != 'Student':
student = False
if x.role == 'Student' and x.status != 'Alumnus':
student = True
# relationships
if student is None:
if Relationship.objects.filter(person_from=self,
relationship__in=parent_relationships):
student = True
return student
def active_roles(self):
results = PersonTeam.objects.filter(person=self,
status__in=('Active', 'Prospective'))
results = results.select_related('team.name')
return ", ".join("%s (%s%s)" %
(x.team.name,
x.status == 'Prospective' and "Prospective " or "",
x.role)
for x in results)
active_roles.short_description = 'Active Roles'
objects = BatchManager()
def clean(self):
for field in self._meta.fields:
if isinstance(field, (models.CharField, models.TextField)):
setattr(self, field.name, getattr(self, field.name).strip())
class Meta:
verbose_name_plural = "People"
ordering = ['lastname', 'firstname']
unique_together = ['firstname', 'lastname', 'suffix']
class PersonTeam(models.Model):
person = models.ForeignKey(Person)
team = models.ForeignKey(Team)
ROLE_CHOICES=(
('Mentor', "Mentor"),
('Student', "Student"),
('Fan', "Fan"),
)
role = models.CharField("Role", max_length=10, choices=ROLE_CHOICES)
STATUS_CHOICES=(
('Prospective', "Prospective"),
('Pending', "Pending"),
('Active', "Active"),
('Alumnus', "Alumnus"),
('Disinterested', "Disinterested"),
)
status = models.CharField("Status", max_length=20, choices=STATUS_CHOICES)
joined = models.DateField("Joined", null=True, blank=True)
left = models.DateField("Left", null=True, blank=True)
def __unicode__(self):
return "%s %s, %s" % (self.status, self.role, self.team.name)
class Meta:
unique_together = ['team', 'person']
class PersonEmail(models.Model):
person = models.ForeignKey(Person)
email = models.ForeignKey(Email)
primary = models.BooleanField("Primary", default=True)
def __unicode__(self):
return "%s (%s)" % (self.email, self.primary and "Primary" or "Alternate")
class PersonPhone(models.Model):
person = models.ForeignKey(Person)
phone = models.ForeignKey(Phone)
primary = models.BooleanField("Primary", default=True)
def __unicode__(self):
return "%s (%s)" % (self.phone, self.primary and "Primary" or "Alternate")
class Waiver(models.Model):
person = models.ForeignKey(Person)
org = models.ForeignKey(Organization)
year = models.IntegerField("Year")
def __unicode__(self):
return "%s (%s)" % (self.org, self.year)
class Relationship(models.Model):
person_from = models.ForeignKey(Person,
related_name="relationship_to_set")
person_to = models.ForeignKey(Person,
related_name="relationship_from_set")
relationship = models.ForeignKey(RelationshipType)
cc_on_email = models.BooleanField("CC on Emails", default=False)
legal_guardian = models.BooleanField("Legal Guardian", default=False)
emergency_contact = models.BooleanField("Emergency Contact", default=False)
def __unicode__(self):
return "%s (%s)" % (self.person_to, self.relationship)
class Meta:
unique_together = ['person_from', 'person_to']
class Event(models.Model):
name = models.CharField(max_length=50)
location = models.CharField(max_length=50)
date = models.DateField()
time = models.TimeField(null=True, blank=True)
end_date = models.DateField(null=True, blank=True)
end_time = models.TimeField(null=True, blank=True)
calendar_post = models.BooleanField(default=True)
fee = models.DecimalField(decimal_places=2, max_digits=5, default=0.0)
people = models.ManyToManyField(Person, through='EventPerson',
blank=True)
def __unicode__(self):
return "%s: %s" % (self.date.strftime('%a %b %d, %Y'), self.name)
def clean(self):
for field in self._meta.fields:
if isinstance(field, (models.CharField, models.TextField)):
setattr(self, field.name, getattr(self, field.name).strip())
class Meta:
ordering = ['date', 'time']
class EventPerson(models.Model):
event = models.ForeignKey(Event)
person = models.ForeignKey(Person)
role = models.CharField("Role", max_length=20, choices=(
('Participant', "Participant"),
('Volunteer', "Volunteer"),
))
fee_paid = models.DecimalField(decimal_places=2, max_digits=5,
default=0.0)
comments = models.TextField("Comments", blank=True)
def __unicode__(self):
return "%s (%s) - %s" % (self.person, self.role, self.fee_paid)
class Meta:
unique_together = ['event', 'person']
ordering = ['event', 'person']
class TimeRecord(models.Model):
person = models.ForeignKey(Person)
event = models.ForeignKey(Event, null=True, blank=True)
clock_in = models.DateTimeField("Clocked in")
clock_out = models.DateTimeField("Clocked out", null=True, blank=True)
hours = models.FloatField("Hours")
recorded = models.DateField("Recorded")