-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmodels.py
More file actions
349 lines (294 loc) · 11.6 KB
/
models.py
File metadata and controls
349 lines (294 loc) · 11.6 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
340
341
342
343
344
345
346
347
348
349
from decimal import Decimal
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from model_utils.models import TimeStampedModel
from conferences.querysets import ConferenceQuerySetMixin
from countries import countries
from helpers.constants import GENDERS
from users.models import User
class GrantQuerySet(ConferenceQuerySetMixin, models.QuerySet):
def of_user(self, user):
return self.filter(user=user)
class GrantReimbursementCategory(models.Model):
"""
Define types of reimbursements available for a grant (e.g., Travel, Ticket, Accommodation).
"""
class Category(models.TextChoices):
TRAVEL = "travel", _("Travel")
TICKET = "ticket", _("Ticket")
ACCOMMODATION = "accommodation", _("Accommodation")
OTHER = "other", _("Other")
conference = models.ForeignKey(
"conferences.Conference",
on_delete=models.CASCADE,
related_name="reimbursement_categories",
)
name = models.CharField(max_length=100)
description = models.TextField(blank=True, null=True)
max_amount = models.DecimalField(
max_digits=6,
decimal_places=0,
default=Decimal("0.00"),
help_text=_("Maximum amount for this category"),
)
category = models.CharField(max_length=20, choices=Category.choices)
included_by_default = models.BooleanField(
default=False,
help_text="Automatically include this category in grants by default",
)
objects = GrantQuerySet().as_manager()
def __str__(self):
return f"{self.name} ({self.conference.name})"
class Meta:
verbose_name = _("Grant Reimbursement Category")
verbose_name_plural = _("Grant Reimbursement Categories")
unique_together = [("conference", "category")]
ordering = ["conference", "category"]
class Grant(TimeStampedModel):
# TextChoices
class Status(models.TextChoices):
pending = "pending", _("Pending")
rejected = "rejected", _("Rejected")
approved = "approved", _("Approved")
waiting_list = "waiting_list", _("Waiting List")
waiting_list_maybe = "waiting_list_maybe", _("Waiting List, Maybe")
waiting_for_confirmation = (
"waiting_for_confirmation",
_("Waiting for confirmation"),
)
refused = "refused", _("Refused")
confirmed = "confirmed", _("Confirmed")
did_not_attend = "did_not_attend", _("Did Not Attend")
REVIEW_SESSION_STATUSES_OPTIONS = [
Status.rejected.value,
Status.approved.value,
Status.waiting_list.value,
Status.waiting_list_maybe.value,
]
class CountryType(models.TextChoices):
italy = "italy", _("Italy")
europe = "europe", _("Europe")
extra_eu = "extra_eu", _("Extra EU")
class AgeGroup(models.TextChoices):
range_under_18 = "range_under_18", _("Under 18 years old")
range_18_24 = "range_18_24", _("18 - 24 years old")
range_25_34 = "range_25_34", _("25 - 34 years old")
range_35_44 = "range_35_44", _("35 - 44 years old")
range_45_54 = "range_45_54", _("45 - 54 years old")
range_55_64 = "range_55_64", _("55 - 64 years old")
range_more_than_65 = "range_more_than_65", _("65 years or older")
class Occupation(models.TextChoices):
developer = "developer", _("Developer")
student = "student", _("Student")
researcher = "researcher", _("Researcher")
unemployed = "unemployed", _("Unemployed")
other = "other", _("Other")
class GrantType(models.TextChoices):
diversity = "diversity", _("Diversity")
unemployed = "unemployed", _("Unemployed")
speaker = "speaker", _("Speaker")
conference = models.ForeignKey(
"conferences.Conference",
on_delete=models.CASCADE,
verbose_name=_("conference"),
related_name="grants",
)
user = models.ForeignKey(
"users.User",
on_delete=models.PROTECT,
null=True,
blank=True,
verbose_name=_("user"),
related_name="+",
)
email = models.EmailField(_("email address")) # OLD FIELD
# About You Section
full_name = models.CharField(_("full name"), max_length=300)
name = models.CharField(_("name"), max_length=300)
age_group = models.CharField(
_("Age group"), max_length=20, choices=AgeGroup.choices, blank=True
)
occupation = models.CharField(
_("occupation"), choices=Occupation.choices, max_length=10
)
# Your Grant Section
grant_type = models.JSONField(_("grant type"), default=list)
departure_country = models.CharField(
_("Departure Country"),
max_length=100,
blank=True,
null=True,
choices=[(country.code, country.name) for country in countries],
)
nationality = models.CharField(
_("Nationality"),
max_length=100,
blank=True,
null=True,
)
departure_city = models.CharField(
_("Departure city"),
max_length=100,
blank=True,
null=True,
)
needs_funds_for_travel = models.BooleanField(_("Needs funds for travel"))
need_visa = models.BooleanField(_("Need visa/invitation letter?"), default=False)
need_accommodation = models.BooleanField(_("Need accommodation"), default=False)
why = models.TextField(_("Why are you asking for a grant?"))
# You and Python Section
python_usage = models.TextField(_("How do they use python"))
been_to_other_events = models.TextField(_("Have they been to other events?"))
community_contribution = models.TextField(_("Community contribution"), blank=True)
# Optional Information Section
gender = models.CharField(_("gender"), choices=GENDERS, max_length=10, blank=True)
notes = models.TextField(_("Notes"), blank=True)
website = models.URLField(_("Website"), max_length=2048, blank=True)
twitter_handle = models.CharField(_("Twitter handle"), max_length=15, blank=True)
github_handle = models.CharField(_("GitHub handle"), blank=True, max_length=39)
linkedin_url = models.URLField(_("LinkedIn url"), max_length=2048, blank=True)
mastodon_handle = models.CharField(
_("Mastodon handle"), max_length=2048, blank=True
)
# Grant Management and Processing
status = models.CharField(
_("status"), choices=Status.choices, max_length=30, default=Status.pending
)
pending_status = models.CharField(
_("pending status"),
choices=Status.choices,
max_length=30,
null=True,
blank=True,
)
country_type = models.CharField(
_("Country type"),
max_length=10,
choices=CountryType.choices,
null=True,
blank=True,
)
# Applicant Communication Tracking
applicant_reply_sent_at = models.DateTimeField(
_("applicant reply sent at"), null=True, blank=True
)
applicant_reply_deadline = models.DateTimeField(
_("applicant reply deadline"), null=True, blank=True
)
# Voucher Management
internal_notes = models.TextField(
_("Internal Notes"),
help_text=_("Internal notes only available to the Financial Aid Commettie"),
blank=True,
)
reimbursement_categories = models.ManyToManyField(
GrantReimbursementCategory, through="GrantReimbursement", related_name="grants"
)
objects = GrantQuerySet().as_manager()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._original_status = self.status
self._original_pending_status = self.pending_status
self._original_country_type = self.country_type
def __str__(self):
return f"{self.full_name}"
def save(self, *args, **kwargs):
self._update_country_type()
update_fields = kwargs.get("update_fields", None)
if update_fields:
update_fields.append("country_type")
update_fields.append("pending_status")
super().save(*args, **kwargs)
self._original_country_type = self.country_type
self._original_pending_status = self.pending_status
self._original_status = self.status
def _update_country_type(self):
if not self.departure_country:
return
country = countries.get(code=self.departure_country)
assert country
if country.code == "IT":
self.country_type = Grant.CountryType.italy
elif country.continent == "EU":
self.country_type = Grant.CountryType.europe
else:
self.country_type = Grant.CountryType.extra_eu
def can_edit(self, user: User):
return self.user_id == user.id
def get_admin_url(self):
return reverse(
"admin:%s_%s_change" % (self._meta.app_label, self._meta.model_name),
args=(self.pk,),
)
def has_approved(self, category: GrantReimbursementCategory.Category) -> bool:
"""Return True if grant has approved reimbursement for category."""
return self.reimbursements.filter(category__category=category).exists()
def has_approved_ticket(self) -> bool:
return self.has_approved(GrantReimbursementCategory.Category.TICKET)
def has_approved_travel(self) -> bool:
return self.has_approved(GrantReimbursementCategory.Category.TRAVEL)
def has_approved_accommodation(self) -> bool:
return self.has_approved(GrantReimbursementCategory.Category.ACCOMMODATION)
def has_ticket_only(self) -> bool:
"""Return True if grant has only ticket, no travel or accommodation."""
return (
self.has_approved_ticket()
and not self.has_approved_travel()
and not self.has_approved_accommodation()
)
@property
def total_allocated_amount(self) -> Decimal:
"""Return total of all reimbursements including ticket."""
return sum(
(r.granted_amount for r in self.reimbursements.all()),
start=Decimal(0),
)
@property
def total_grantee_reimbursement_amount(self) -> Decimal:
"""Return total reimbursement excluding ticket."""
return sum(
(
r.granted_amount
for r in self.reimbursements.all()
if r.category.category != GrantReimbursementCategory.Category.TICKET
),
start=Decimal(0),
)
@property
def current_or_pending_status(self):
return self.pending_status or self.status
class GrantReimbursement(models.Model):
"""Links a Grant to its reimbursement categories and stores the actual amount granted."""
grant = models.ForeignKey(
Grant,
on_delete=models.CASCADE,
related_name="reimbursements",
verbose_name=_("grant"),
)
category = models.ForeignKey(
GrantReimbursementCategory,
on_delete=models.CASCADE,
verbose_name=_("reimbursement category"),
)
granted_amount = models.DecimalField(
_("granted amount"),
max_digits=6,
decimal_places=0,
help_text=_("Actual amount granted for this category"),
)
def __str__(self):
return f"{self.grant.full_name} - {self.category.name} - {self.granted_amount}"
class Meta:
verbose_name = _("Grant Reimbursement")
verbose_name_plural = _("Grant Reimbursements")
unique_together = [("grant", "category")]
ordering = ["grant", "category"]
indexes = [
models.Index(fields=["grant", "category"]),
]
class GrantConfirmPendingStatusProxy(Grant):
class Meta:
proxy = True
verbose_name = _("Grant Confirm Pending Status")
verbose_name_plural = _("Grants Confirm Pending Status")