-
Notifications
You must be signed in to change notification settings - Fork 56
/
models.py
542 lines (434 loc) · 17.4 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
from __future__ import unicode_literals
import json
import uuid as uuid
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator, MinValueValidator
from django.db import models
from django.db.models import Avg
from django.utils import timezone
from multiselectfield import MultiSelectField
from app import utils, hackathon_variables
from user.models import User, BlacklistUser
from user import models as userModels
from applications.validators import validate_file_extension
APP_PENDING = 'P'
APP_REJECTED = 'R'
APP_INVITED = 'I'
APP_LAST_REMIDER = 'LR'
APP_CONFIRMED = 'C'
APP_CANCELLED = 'X'
APP_ATTENDED = 'A'
APP_EXPIRED = 'E'
APP_DUBIOUS = 'D'
APP_INVALID = 'IV'
APP_BLACKLISTED = 'BL'
PENDING_TEXT = 'Under review'
DUBIOUS_TEXT = 'Dubious'
BLACKLIST_TEXT = 'Blacklisted'
STATUS = [
(APP_PENDING, PENDING_TEXT),
(APP_REJECTED, 'Wait listed'),
(APP_INVITED, 'Invited'),
(APP_LAST_REMIDER, 'Last reminder'),
(APP_CONFIRMED, 'Confirmed'),
(APP_CANCELLED, 'Cancelled'),
(APP_ATTENDED, 'Attended'),
(APP_EXPIRED, 'Expired'),
(APP_DUBIOUS, DUBIOUS_TEXT),
(APP_INVALID, 'Invalid'),
(APP_BLACKLISTED, BLACKLIST_TEXT)
]
NO_ANSWER = 'NA'
MALE = 'M'
FEMALE = 'F'
NON_BINARY = 'NB'
GENDER_OTHER = 'X'
GENDERS = [
(NO_ANSWER, 'Prefer not to answer'),
(MALE, 'Male'),
(FEMALE, 'Female'),
(NON_BINARY, 'Non-binary'),
(GENDER_OTHER, 'Prefer to self-describe'),
]
D_NONE = 'None'
D_VEGETERIAN = 'Vegeterian'
D_VEGAN = 'Vegan'
D_NO_PORK = 'No pork'
D_GLUTEN_FREE = 'Gluten-free'
D_OTHER = 'Others'
DIETS = [
(D_NONE, 'No requirements'),
(D_VEGETERIAN, 'Vegeterian'),
(D_VEGAN, 'Vegan'),
(D_NO_PORK, 'No pork'),
(D_GLUTEN_FREE, 'Gluten-free'),
(D_OTHER, 'Others')
]
W_XXS = 'W-XSS'
W_XS = 'W-XS'
W_S = 'W-S'
W_M = 'W-M'
W_L = 'W-L'
W_XL = 'W-XL'
W_XXL = 'W-XXL'
T_XXS = 'XXS'
T_XS = 'XS'
T_S = 'S'
T_M = 'M'
T_L = 'L'
T_XL = 'XL'
T_XXL = 'XXL'
TSHIRT_SIZES = [
(W_XXS, "Women's - XXS"),
(W_XS, "Women's - XS"),
(W_S, "Women's - S"),
(W_M, "Women's - M"),
(W_L, "Women's - L"),
(W_XL, "Women's - XL"),
(W_XXL, "Women's - XXL"),
(T_XXS, "Unisex - XXS"),
(T_XS, "Unisex - XS"),
(T_S, "Unisex - S"),
(T_M, "Unisex - M"),
(T_L, "Unisex - L"),
(T_XL, "Unisex - XL"),
(T_XXL, "Unisex - XXL"),
]
DEFAULT_TSHIRT_SIZE = T_M
ATTENDANCE = [
(0, "Friday"),
(1, "Saturday"),
(2, "Sunday")
]
HACK_NAME = getattr(hackathon_variables, 'HACKATHON_NAME', "HackAssistant")
EXTRA_NAME = [' 2016', ' 2017', ' 2018', ' 2019']
PREVIOUS_HACKS = [(i, HACK_NAME + EXTRA_NAME[i]) for i in range(0, len(EXTRA_NAME))]
YEARS = [(int(size), size) for size in ('2018 2019 2020 2021 2022 2023 2024'.split(' '))]
DEFAULT_YEAR = 2018
ENGLISH_LEVEL = [(i, str(i)) for i in range(1, 5 + 1)]
class BaseApplication(models.Model):
class Meta:
abstract = True
uuid = models.UUIDField(default=uuid.uuid4, editable=False)
user = models.OneToOneField(User, related_name='%(class)s_application', primary_key=True, on_delete=models.CASCADE)
invited_by = models.ForeignKey(User, related_name='%(class)s_invited_applications', blank=True, null=True,
on_delete=models.SET_NULL)
# When was the application submitted
submission_date = models.DateTimeField(default=timezone.now)
# When was the last status update
status_update_date = models.DateTimeField(blank=True, null=True)
# Application status
status = models.CharField(choices=STATUS,
default=APP_PENDING,
max_length=2)
# ABOUT YOU
# Population analysis, optional
gender = models.CharField(max_length=23, choices=GENDERS, default=NO_ANSWER)
other_gender = models.CharField(max_length=50, blank=True, null=True)
# Personal data (asking here because we don't want to ask birthday)
under_age = models.BooleanField()
phone_number = models.CharField(blank=True, null=True, max_length=16,
validators=[RegexValidator(regex=r'^\+?1?\d{9,15}$',
message="Phone number must be entered in the format: \
'+#########'. Up to 15 digits allowed.")])
# Info for swag and food
diet = models.CharField(max_length=300, choices=DIETS, default=D_NONE)
other_diet = models.CharField(max_length=600, blank=True, null=True)
tshirt_size = models.CharField(max_length=5, default=DEFAULT_TSHIRT_SIZE, choices=TSHIRT_SIZES)
def __init__(self, *args, **kwargs):
super(BaseApplication, self).__init__(*args, **kwargs)
try:
dict = args[0]['dict']
except Exception:
dict = None
if dict is not None:
for key in dict:
atr = getattr(self, key, 'NOT_EXIST')
if atr != 'NOT_EXIST':
setattr(self, key, dict[key])
def __str__(self):
return self.user.email
@property
def uuid_str(self):
return str(self.uuid)
def get_soft_status_display(self):
text = self.get_status_display()
if DUBIOUS_TEXT == text or BLACKLIST_TEXT == text:
return PENDING_TEXT
return text
def save(self, **kwargs):
self.status_update_date = timezone.now()
super(BaseApplication, self).save(**kwargs)
def is_confirmed(self):
return self.status == APP_CONFIRMED
def is_cancelled(self):
return self.status == APP_CANCELLED
def answered_invite(self):
return self.status in [APP_CONFIRMED, APP_CANCELLED, APP_ATTENDED]
def needs_action(self):
return self.status == APP_INVITED
def is_pending(self):
return self.status == APP_PENDING
def can_be_edit(self):
return self.status == APP_PENDING
def is_invited(self):
return self.status == APP_INVITED
def is_expired(self):
return self.status == APP_EXPIRED
def is_rejected(self):
return self.status == APP_REJECTED
def is_invalid(self):
return self.status == APP_INVALID
def is_attended(self):
return self.status == APP_ATTENDED
def is_last_reminder(self):
return self.status == APP_LAST_REMIDER
def is_dubious(self):
return self.status == APP_DUBIOUS
def can_be_cancelled(self):
return self.status == APP_CONFIRMED or self.status == APP_INVITED or self.status == APP_LAST_REMIDER
def can_confirm(self):
return self.status in [APP_INVITED, APP_LAST_REMIDER]
def can_be_invited(self):
return self.status in [APP_INVITED, APP_LAST_REMIDER, APP_CANCELLED, APP_PENDING, APP_EXPIRED, APP_REJECTED,
APP_INVALID]
def can_join_team(self):
return self.user.type == userModels.USR_HACKER and self.status in [APP_PENDING, APP_LAST_REMIDER, APP_DUBIOUS]
def check_in(self):
self.status = APP_ATTENDED
self.status_update_date = timezone.now()
self.save()
def move_to_pending(self):
self.status = APP_PENDING
self.status_update_date = timezone.now()
self.save()
def reject(self):
if self.status == APP_ATTENDED:
raise ValidationError('Application has already attended. '
'Current status: %s' % self.status)
self.status = APP_REJECTED
self.status_update_date = timezone.now()
self.save()
def invite(self, user):
# We can re-invite someone invited
if self.status in [APP_CONFIRMED, APP_ATTENDED]:
raise ValidationError('Application has already answered invite. '
'Current status: %s' % self.status)
self.status = APP_INVITED
if not self.invited_by:
self.invited_by = user
self.last_invite = timezone.now()
self.last_reminder = None
self.status_update_date = timezone.now()
self.save()
def confirm(self):
if self.status == APP_CANCELLED:
raise ValidationError('This invite has been cancelled.')
elif self.status == APP_EXPIRED:
raise ValidationError('Unfortunately your invite has expired.')
elif self.status in [APP_INVITED, APP_LAST_REMIDER]:
self.status = APP_CONFIRMED
self.status_update_date = timezone.now()
self.save()
elif self.status in [APP_CONFIRMED, APP_ATTENDED]:
return None
else:
raise ValidationError('Unfortunately his application hasn\'t been '
'invited [yet]')
def cancel(self):
if not self.can_be_cancelled():
raise ValidationError('Application can\'t be cancelled. Current '
'status: %s' % self.status)
if self.status != APP_CANCELLED:
self.status = APP_CANCELLED
self.status_update_date = timezone.now()
self.save()
reimb = getattr(self.user, 'reimbursement', None)
if reimb:
reimb.delete()
def last_reminder(self):
if self.status != APP_INVITED:
raise ValidationError('Reminder can\'t be sent to non-pending '
'applications')
self.status_update_date = timezone.now()
self.status = APP_LAST_REMIDER
self.save()
def expire(self):
self.status_update_date = timezone.now()
self.status = APP_EXPIRED
self.save()
class _HackerMentorVolunteerApplication(models.Model):
class Meta:
abstract = True
# Where is this person coming from?
origin = models.CharField(max_length=300)
# Is this your first hackathon?
first_timer = models.BooleanField(default=False)
# Random lenny face
lennyface = models.CharField(max_length=300, default='-.-')
# University
graduation_year = models.IntegerField(choices=YEARS, default=DEFAULT_YEAR)
university = models.CharField(max_length=300)
degree = models.CharField(max_length=300)
class _HackerMentorApplication(models.Model):
class Meta:
abstract = True
# URLs
github = models.URLField(blank=True, null=True)
devpost = models.URLField(blank=True, null=True)
linkedin = models.URLField(blank=True, null=True)
site = models.URLField(blank=True, null=True)
# Giv me a resume here!
resume = models.FileField(upload_to='resumes', null=True, blank=True, validators=[validate_file_extension])
class _VolunteerMentorApplication(models.Model):
class Meta:
abstract = True
english_level = models.IntegerField(default=0, null=False, choices=ENGLISH_LEVEL)
which_hack = MultiSelectField(choices=PREVIOUS_HACKS, null=True, blank=True)
class _VolunteerMentorSponsorApplication(models.Model):
class Meta:
abstract = True
attendance = MultiSelectField(choices=ATTENDANCE)
class HackerApplication(
BaseApplication,
_HackerMentorVolunteerApplication,
_HackerMentorApplication
):
# Explain a little bit what projects have you done lately
projects = models.TextField(max_length=500, blank=True, null=True)
# META
contacted = models.BooleanField(default=False) # If a dubious application has been contacted yet
contacted_by = models.ForeignKey(User, related_name='contacted_by', blank=True, null=True,
on_delete=models.SET_NULL)
reviewed = models.BooleanField(default=False) # If a blacklisted application has been reviewed yet
blacklisted_by = models.ForeignKey(User, related_name='blacklisted_by', blank=True, null=True,
on_delete=models.SET_NULL)
# Why do you want to come to X?
description = models.TextField(max_length=500)
# Reimbursement
reimb = models.BooleanField(default=False)
reimb_amount = models.FloatField(blank=True, null=True, validators=[
MinValueValidator(0, "Negative? Really? Please put a positive value")])
@classmethod
def annotate_vote(cls, qs):
return qs.annotate(vote_avg=Avg('vote__calculated_vote'))
def invalidate(self):
if self.status != APP_DUBIOUS:
raise ValidationError('Applications can only be marked as invalid if they are dubious first')
self.status = APP_INVALID
self.save()
def set_dubious(self):
self.status = APP_DUBIOUS
self.contacted = False
self.status_update_date = timezone.now()
self.save()
def unset_dubious(self):
self.status = APP_PENDING
self.status_update_date = timezone.now()
self.save()
def set_contacted(self, user):
if not self.contacted:
self.contacted = True
self.contacted_by = user
self.save()
def confirm_blacklist(self, user, motive_of_ban):
if self.status != APP_BLACKLISTED:
raise ValidationError('Applications can only be confirmed as blacklisted if they are blacklisted first')
self.status = APP_INVALID
self.set_blacklisted_by(user)
blacklist_user = BlacklistUser.objects.filter(email=self.user.email).first()
if not blacklist_user:
blacklist_user = BlacklistUser.objects.create_blacklist_user(
self.user, motive_of_ban)
blacklist_user.save()
self.save()
def set_blacklist(self):
self.status = APP_BLACKLISTED
self.status_update_date = timezone.now()
self.save()
def unset_blacklist(self):
self.status = APP_PENDING
self.status_update_date = timezone.now()
self.save()
def set_blacklisted_by(self, user):
if not self.blacklisted_by:
self.blacklisted_by = user
def is_blacklisted(self):
return self.status == APP_BLACKLISTED
def can_be_edit(self):
return self.status == APP_PENDING and not self.vote_set.exists() and not utils.is_app_closed()
class MentorApplication(
BaseApplication,
_HackerMentorApplication,
_HackerMentorVolunteerApplication,
_VolunteerMentorApplication,
_VolunteerMentorSponsorApplication
):
company = models.CharField(max_length=100, null=True, blank=True)
why_mentor = models.CharField(max_length=500, null=False)
first_time_mentor = models.BooleanField(null=False)
fluent = models.CharField(max_length=150, null=False)
experience = models.CharField(max_length=300, null=False)
study_work = models.BooleanField(max_length=300, null=False)
university = models.CharField(max_length=300, null=True, blank=True)
degree = models.CharField(max_length=300, null=True, blank=True)
participated = models.TextField(max_length=500, blank=True, null=True)
class VolunteerApplication(
BaseApplication,
_VolunteerMentorSponsorApplication,
_VolunteerMentorApplication,
_HackerMentorVolunteerApplication
):
cool_skill = models.CharField(max_length=100, null=False)
first_time_volunteer = models.BooleanField(null=False)
quality = models.CharField(max_length=150, null=False)
weakness = models.CharField(max_length=150, null=False)
fav_movie = models.CharField(max_length=60, null=True, blank=True)
friends = models.CharField(max_length=100, null=True, blank=True)
class SponsorApplication(
_VolunteerMentorSponsorApplication,
):
name = models.CharField(
verbose_name='Full name',
max_length=255,
)
# When was the application submitted
submission_date = models.DateTimeField(default=timezone.now)
# When was the last status update
status_update_date = models.DateTimeField(blank=True, null=True)
uuid = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True)
user = models.ForeignKey(User, related_name='%(class)s_application', null=True, on_delete=models.SET_NULL)
# Application status
status = models.CharField(choices=STATUS,
default=APP_CONFIRMED,
max_length=2)
phone_number = models.CharField(blank=True, null=True, max_length=16,
validators=[RegexValidator(regex=r'^\+?1?\d{9,15}$',
message="Phone number must be entered in the format: \
'+#########'. Up to 15 digits allowed.")])
# Info for swag and food
diet = models.CharField(max_length=300, choices=DIETS, default=D_NONE)
other_diet = models.CharField(max_length=600, blank=True, null=True)
tshirt_size = models.CharField(max_length=5, default=DEFAULT_TSHIRT_SIZE, choices=TSHIRT_SIZES)
position = models.CharField(max_length=50, null=False)
@property
def uuid_str(self):
return str(self.uuid)
def __str__(self):
return self.name + ' from ' + self.user.name
def save(self, **kwargs):
self.status_update_date = timezone.now()
super(SponsorApplication, self).save(**kwargs)
def check_in(self):
self.status = APP_ATTENDED
self.status_update_date = timezone.now()
self.save()
class META:
unique_together = [['name', 'user']]
class DraftApplication(models.Model):
content = models.CharField(max_length=7000)
user = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE)
def save_dict(self, d):
self.content = json.dumps(d)
def get_dict(self):
return json.loads(self.content)