-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathsession.py
More file actions
339 lines (290 loc) · 9.57 KB
/
session.py
File metadata and controls
339 lines (290 loc) · 9.57 KB
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
from datetime import timedelta
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.urls.base import reverse
from django.utils import formats, timezone
from django.utils.functional import cached_property
from .common import CommonInfo
class Session(CommonInfo):
from .course import Course
from .location import Location
from .mentor import Mentor
from .student import Student
MALE = "male"
FEMALE = "female"
GENDER_LIMITATION_CHOICES = (
(MALE, "Male"),
(FEMALE, "Female"),
)
course = models.ForeignKey(
Course,
on_delete=models.CASCADE,
limit_choices_to={"is_active": True},
)
start_date = models.DateTimeField()
location = models.ForeignKey(
Location,
on_delete=models.CASCADE,
limit_choices_to={"is_active": True},
)
capacity = models.IntegerField(
default=20,
)
mentor_capacity = models.IntegerField(
blank=True,
null=True,
)
instructor = models.ForeignKey(
Mentor,
on_delete=models.CASCADE,
related_name="session_instructor",
limit_choices_to={"user__groups__name": "Instructor"},
)
# Pricing
cost = models.DecimalField(
max_digits=6,
decimal_places=2,
blank=True,
null=True,
)
minimum_cost = models.DecimalField(
max_digits=6,
decimal_places=2,
blank=True,
null=True,
)
maximum_cost = models.DecimalField(
max_digits=6,
decimal_places=2,
blank=True,
null=True,
)
# Extra
additional_info = models.TextField(blank=True, null=True, help_text="Basic HTML allowed")
waitlist_mentors = models.ManyToManyField(
Mentor,
blank=True,
related_name="session_waitlist_mentors",
)
waitlist_students = models.ManyToManyField(
Student,
blank=True,
related_name="session_waitlist_students",
)
external_enrollment_url = models.CharField(
max_length=255,
blank=True,
null=True,
help_text="When provided, local enrollment is disabled.",
)
is_active = models.BooleanField(
default=False,
help_text="Session is active.",
)
is_public = models.BooleanField(
default=False,
help_text="Session is a public session.",
)
password = models.CharField(
blank=True,
max_length=255,
)
partner_message = models.TextField(
blank=True,
)
announced_date_mentors = models.DateTimeField(
blank=True,
null=True,
)
announced_date_guardians = models.DateTimeField(
blank=True,
null=True,
)
image_url = models.CharField(
max_length=255,
blank=True,
null=True,
)
bg_image = models.ImageField(
blank=True,
null=True,
)
mentors_week_reminder_sent = models.BooleanField(
default=False,
)
mentors_day_reminder_sent = models.BooleanField(
default=False,
)
gender_limitation = models.CharField(
help_text="Limits the class to be only one gender.",
max_length=255,
choices=GENDER_LIMITATION_CHOICES,
blank=True,
null=True,
)
override_minimum_age_limitation = models.IntegerField(
"Min Age",
help_text="Only update this if different from the default.",
blank=True,
null=True,
validators=[MinValueValidator(0), MaxValueValidator(100)],
)
override_maximum_age_limitation = models.IntegerField(
"Max Age",
help_text="Only update this if different from the default.",
blank=True,
null=True,
validators=[MinValueValidator(0), MaxValueValidator(100)],
)
online_video_link = models.URLField(
"Online Video Link",
help_text="Zoom link with password.",
blank=True,
null=True,
)
online_video_meeting_id = models.CharField(
"Online Video Meeting ID",
help_text="XXX XXXX XXXX",
max_length=255,
blank=True,
null=True,
)
online_video_meeting_password = models.CharField(
"Online Video Meeting Password",
help_text="Plain text password shared by Zoom",
max_length=255,
blank=True,
null=True,
)
online_video_description = models.TextField(
"Online Video Description",
help_text="Information on how to connect to the video call. Basic HTML allowed.",
blank=True,
null=True,
)
# kept for older records
old_end_date = models.DateTimeField(
blank=True,
null=True,
)
old_mentor_start_date = models.DateTimeField(
blank=True,
null=True,
)
old_mentor_end_date = models.DateTimeField(
blank=True,
null=True,
)
@property
def end_date(self):
# Some records have a defined record with the end date,
# rather than use the course's duration.
# We're keeping this for old records.
if self.old_end_date:
return self.old_end_date
return self.start_date + self.course.duration
@property
def mentor_start_date(self):
# Some records have a defined record with the mentor start date,
# rather than do the math.
# We're keeping this for old records.
if self.old_mentor_start_date:
return self.old_mentor_start_date
return self.start_date - timedelta(hours=1)
@property
def mentor_end_date(self):
# Some records have a defined record with the mentor start date,
# rather than do the math.
# We're keeping this for old records.
if self.old_mentor_end_date:
return self.old_mentor_end_date
return self.end_date + timedelta(hours=1)
@property
def minimum_age(self):
if self.override_minimum_age_limitation is not None:
return self.override_minimum_age_limitation
return self.course.minimum_age
@property
def maximum_age(self):
if self.override_maximum_age_limitation is not None:
return self.override_maximum_age_limitation
return self.course.maximum_age
def __str__(self):
date = formats.date_format(self.start_date, "SHORT_DATETIME_FORMAT")
return f"{self.course.title} | {date}"
def save(self, *args, **kwargs):
if self.mentor_capacity is None:
self.mentor_capacity = int(self.capacity / 2)
super(Session, self).save(*args, **kwargs)
def get_absolute_url(self) -> str:
return reverse("session-detail", args=[str(self.id)])
def get_sign_up_url(self):
return reverse("session-sign-up", args=[str(self.id)])
def get_calendar_url(self):
return reverse("session-calendar", args=[str(self.id)])
def is_guardian_announced(self):
return self.announced_date_guardians is not None
is_guardian_announced.boolean = True
is_guardian_announced.short_description = "Is Announced"
is_guardian_announced.admin_order_field = "announced_date_guardians"
def get_mentor_orders(self):
from .mentor_order import MentorOrder
return MentorOrder.objects.filter(
session=self,
is_active=True,
).order_by("mentor__user__last_name")
def get_checked_in_mentor_orders(self):
from .mentor_order import MentorOrder
return MentorOrder.objects.filter(session=self, is_active=True, check_in__isnull=False).order_by(
"mentor__user__last_name"
)
def get_current_orders(self, checked_in=None):
from .order import Order
if checked_in is not None:
if checked_in:
orders = (
Order.objects.filter(is_active=True, session=self)
.exclude(check_in=None)
.order_by("student__last_name")
)
else:
orders = Order.objects.filter(is_active=True, session=self, check_in=None).order_by(
"student__last_name"
)
else:
orders = Order.objects.filter(is_active=True, session=self).order_by("check_in", "student__last_name")
return orders
def get_active_student_count(self):
from .order import Order
return Order.objects.filter(is_active=True, session=self).values("student").count()
def get_checked_in_students(self):
from .order import Order
return Order.objects.filter(is_active=True, session=self).exclude(check_in=None).values("student")
def get_mentor_capacity(self):
if self.mentor_capacity:
return self.mentor_capacity
else:
return int(self.capacity / 2)
def get_course_prerequisites_needed(self, student):
from .order import Order
student_previous_orders = Order.objects.filter(
student=student,
session__start_date__lt=timezone.now(),
session__course__id__in=self.course.prerequisite.values_list("id"),
).exclude(check_in=None)
student_prerequisites_needed = self.course.prerequisite.exclude(
id__in=student_previous_orders.values("session__course__id")
).distinct("id")
return student_prerequisites_needed
class PartnerPasswordAccess(CommonInfo):
from .user import CDCUser
user = models.ForeignKey(
CDCUser,
on_delete=models.CASCADE,
)
session = models.ForeignKey(
Session,
on_delete=models.CASCADE,
)
class Meta:
db_table = "partner_password_access"