-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
369 lines (294 loc) · 11.3 KB
/
models.py
File metadata and controls
369 lines (294 loc) · 11.3 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import re
import typing
import uuid
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django_choices_field import IntegerChoicesField
from django_stubs_ext.db.models.manager import RelatedManager
from apps.common.models import UserResource
from apps.user.models import User
# NOTE: regex source https://gist.github.com/Mecanik/b339e629c1020fcddbf7df5fadf305b1
REGEX_PATTERN = re.compile(r"^((?:https?:)?\/\/)?((?:www|player|m)\.)?(vimeo\.com|youtube\.com|youtu\.be).*$")
def validate_url(url: str) -> None:
"""Validates that the URL is a YouTube or Vimeo video link.
Args:
url: The video URL to check.
Raises:
ValidationError: If the URL is any link other than YouTube or Vimeo.
"""
if not REGEX_PATTERN.match(url):
raise ValidationError(
"Only YouTube or Vimeo video links are allowed.",
)
class Catalog(UserResource):
"""Model representing catalog where a tool belongs to."""
name = models.CharField[str, str](max_length=200)
description = models.TextField[str, str]()
show_in_help_me_choose = models.BooleanField[bool | None, bool | None](default=False)
# type hints
questions: typing.ClassVar[RelatedManager["Question"]]
tool_catalogs: typing.ClassVar[RelatedManager["Tool"]]
class Meta(UserResource.Meta):
ordering = ["name"]
@typing.override
def __str__(self):
return self.name
class ToolFeature(models.Model):
"""Model representing a tool feature."""
class FeatureCategoryEnum(models.IntegerChoices):
"""Enum representing feature category."""
CONFIGURATION = 10, ("Configuration")
BENEFICIARY_REGISTRATION = 20, ("Beneficiary Registration")
DISTRIBUTION_MANAGEMENT = 30, ("Distribution Management")
FEEDBACK_AND_SURVEYS = 40, ("Feedback and Surveys")
DATA_MANAGEMENT = 50, ("Data Management")
REPORTING_AND_ANALYTICS = 60, ("Reporting and Analytics")
name = models.CharField(max_length=200)
feature_category: int = IntegerChoicesField(choices_enum=FeatureCategoryEnum, default="None") # type: ignore[reportAssignmentType]
description = models.TextField(null=True)
cash_RTM_code = models.CharField(null=True, max_length=40)
@typing.override
def __str__(self):
return f"{self.feature_category} - {self.name}"
class Sector(models.Model):
"""Model representing tool sector."""
name = models.CharField(max_length=100, unique=True)
description = models.TextField(blank=True)
sector_owners = models.ManyToManyField(User, related_name="sector_owners", blank=True)
@typing.override
def __str__(self):
return self.name
class QuestionTypeEnum(models.IntegerChoices):
"""Enum representing type of question."""
ORDINAL = 10, ("Ordinal")
CHECKBOX = 20, ("Checkbox")
class Question(UserResource):
"""Model representing question within a catalog."""
catalog = models.ForeignKey(
Catalog,
on_delete=models.CASCADE,
related_name="questions",
)
question_type: int = IntegerChoicesField( # type: ignore[reportAssignmentType]
choices_enum=QuestionTypeEnum,
)
title = models.CharField[str, str](max_length=500)
short_name = models.CharField[str, str](max_length=500)
description = models.TextField[str, str](blank=True)
order = models.PositiveIntegerField[int, int]()
# Ordinal-specific fields
label_na = models.TextField[str, str](default="N/A", blank=True)
label_1 = models.TextField[str, str](default="1", blank=True)
label_2 = models.TextField[str, str](default="2", blank=True)
label_3 = models.TextField[str, str](default="3", blank=True)
label_4 = models.TextField[str, str](default="4", blank=True)
# type hints
options: typing.ClassVar[RelatedManager["CheckboxOption"]]
get_question_type_display: typing.ClassVar[typing.Callable[[typing.Self], str]]
class Meta(UserResource.Meta):
ordering = ["catalog", "order"]
@typing.override
def __str__(self) -> str:
return f"{self.catalog.name} - [{self.get_question_type_display()}] {self.title}"
class CheckboxOption(UserResource):
"""Model representing option of checkbox question."""
question = models.ForeignKey(
Question,
on_delete=models.CASCADE,
related_name="options",
)
text = models.CharField[str, str](max_length=300)
order = models.PositiveIntegerField[int, int]()
class Meta(UserResource.Meta):
ordering = ["question", "order"]
constraints = [
models.UniqueConstraint(
fields=["question", "order"],
name="unique_question_order",
),
]
@typing.override
def __str__(self):
return f"{self.question.title} - {self.text}"
class Tool(UserResource):
"""Model representing tool."""
catalogs = models.ManyToManyField(
Catalog,
related_name="tool_catalogs",
help_text="After adding or updating a catalog, the related questions are automatically populated to this tool.",
)
name = models.CharField[str, str](max_length=200)
tagline = models.CharField[str, str](max_length=300, blank=True)
description = models.TextField[str, str]()
video_link = models.URLField[str, str](blank=True, null=True, validators=[validate_url])
tool_link = models.CharField[str, str](blank=True, null=True)
logo = models.ImageField(
upload_to="logos/",
verbose_name="Logo",
null=True,
blank=True,
)
tool_sectors = models.ManyToManyField(Sector, related_name="tool_sectors", blank=True)
tool_features = models.ManyToManyField(ToolFeature, related_name="tool_features", blank=True)
tool_owners = models.ManyToManyField(User, related_name="tool_owners", blank=True)
class Meta(UserResource.Meta):
ordering = ["name"]
@typing.override
def __str__(self):
return f"{self.name}"
class OrdinalTypeEnum(models.IntegerChoices):
"""Enum representing scale of ordinal type question."""
"""
Note: The enum labels are intentionally descriptive and user-friendly so they can be
used directly in the frontend UI and remain consistent with design requirements.
"""
NOT_AVAILABLE = (
100,
("We don't know enough to answer at this time or don't want to include this when considering options"),
)
ONE = 1, ("One")
TWO = 2, ("Two")
THREE = 3, ("Three")
FOUR = 4, ("Four")
class ToolAnswer(UserResource):
"""Model representing tool's selection of question and its answer."""
tool = models.ForeignKey(
Tool,
on_delete=models.CASCADE,
related_name="answers",
)
description = models.TextField[str, str]()
question = models.ForeignKey(
Question,
on_delete=models.CASCADE,
related_name="tool_answers",
)
# For ordinal questions
ordinal_value: int = IntegerChoicesField( # type: ignore[reportAssignmentType]
choices_enum=OrdinalTypeEnum,
blank=True,
null=True,
)
# For checkbox questions
selected_options = models.ManyToManyField(
CheckboxOption,
related_name="tool_answers",
blank=True,
)
# typing
question_id: typing.ClassVar[int]
ordinal_value: int | None
class Meta(UserResource.Meta):
verbose_name = "Tool Answer"
verbose_name_plural = "Tool Answers"
constraints = [
models.UniqueConstraint(
fields=["tool", "question"],
name="unique_tool_question",
),
]
@typing.override
def __str__(self):
if self.question.question_type == "ordinal":
return f"{self.tool.name} - {self.question.title}: {self.ordinal_value}"
options = ", ".join(
[opt.text for opt in self.selected_options.all()],
)
return f"{self.tool.name} - {self.question.title}: [{options}]"
class UserSubmission(models.Model):
"""Model representing a user's submission of answers for a catalog."""
id = models.UUIDField[uuid.UUID, uuid.UUID](
primary_key=True,
default=uuid.uuid4,
editable=False,
)
created_at = models.DateTimeField(auto_now_add=True)
catalog = models.ForeignKey(
Catalog,
on_delete=models.CASCADE,
related_name="submissions",
)
# type hints
answers: typing.ClassVar[RelatedManager["UserAnswer"]]
results: typing.ClassVar[RelatedManager["RecommendationResult"]]
class Meta(UserResource.Meta):
ordering = ["-created_at"]
verbose_name = "User Submission"
verbose_name_plural = "User Submissions"
@typing.override
def __str__(self):
return f"Submission {self.id} for {self.catalog.name} at {self.created_at}"
class UserAnswer(models.Model):
"""Model representing a user's answer to a specific question."""
submission = models.ForeignKey(
UserSubmission,
on_delete=models.CASCADE,
related_name="answers",
)
question = models.ForeignKey(
Question,
on_delete=models.CASCADE,
related_name="user_answers",
)
# For ordinal questions
ordinal_value: int = IntegerChoicesField( # type: ignore[reportAssignmentType]
choices_enum=OrdinalTypeEnum,
blank=True,
null=True,
)
# For checkbox questions
selected_options = models.ManyToManyField(
CheckboxOption,
related_name="user_answers",
blank=True,
)
# typing
question_id: typing.ClassVar[int]
class Meta(UserResource.Meta):
constraints = [
models.UniqueConstraint(
fields=["submission", "question"],
name="unique_submission_question",
),
]
ordering = ["question__order"]
verbose_name = "User Answer"
verbose_name_plural = "User Answers"
@typing.override
def __str__(self):
if self.question.question_type == QuestionTypeEnum.ORDINAL:
return f"Answer to '{self.question.title}': {self.ordinal_value}"
options = ", ".join([opt.text for opt in self.selected_options.all()])
return f"Answer to '{self.question.title}': [{options}]"
class RecommendationResult(models.Model):
"""Model representing recommended tools for a submission."""
submission = models.ForeignKey(
UserSubmission,
on_delete=models.CASCADE,
related_name="results",
)
tool = models.ForeignKey(
Tool,
on_delete=models.CASCADE,
related_name="recommendations",
)
rank = models.IntegerField[int, int](
validators=[MinValueValidator(1), MaxValueValidator(5)],
)
score = models.FloatField[float, float](
help_text="Proximity score - lower is better (closer match)",
)
class Meta(UserResource.Meta):
constraints = [
models.UniqueConstraint(
fields=["submission", "rank"],
name="unique_submission_rank",
),
]
ordering = ["submission", "rank"]
verbose_name = "Recommendation Result"
verbose_name_plural = "Recommendation Results"
@typing.override
def __str__(self):
return f"#{self.rank} {self.tool.name} (score: {self.score:.2f})"