|
1 | 1 | from django.conf import settings |
2 | 2 | from django.contrib.postgres.fields import ArrayField |
| 3 | +from django.core.exceptions import ValidationError |
3 | 4 | from django.db import models, transaction |
| 5 | +from django.db.models import Q |
4 | 6 | from django.utils import timezone |
5 | 7 | from django.utils.translation import gettext_lazy as _ |
6 | 8 |
|
7 | | -from api.models import Admin2, Country, DisasterType, District |
| 9 | +from api.models import Admin2, Country, DisasterType, District, Region |
8 | 10 | from main.fields import SecureFileField |
9 | 11 |
|
10 | 12 |
|
@@ -248,6 +250,64 @@ def __str__(self): |
248 | 250 | return f"{self.name}" |
249 | 251 |
|
250 | 252 |
|
| 253 | +# NOTE: Managed through admin panel for now, used for email notification |
| 254 | +class EmailRecipient(models.Model): |
| 255 | + class EmailType(models.IntegerChoices): |
| 256 | + DREF_ANTICIPATORY = 10, "DREF Anticipatory Pillar" |
| 257 | + DREF_AA_GLOBAL_TEAM = 20, "DREF AA Global Team" |
| 258 | + REGIONAL_COORDINATOR = 30, "Regional Coordinator" |
| 259 | + |
| 260 | + title = models.CharField(max_length=255, verbose_name=_("Title")) |
| 261 | + type = models.IntegerField(choices=EmailType.choices, verbose_name=_("Email Type")) |
| 262 | + region = models.ForeignKey( |
| 263 | + Region, |
| 264 | + null=True, |
| 265 | + blank=True, |
| 266 | + on_delete=models.CASCADE, |
| 267 | + help_text=_("Only assign region for Regional Coordinator email type."), |
| 268 | + ) |
| 269 | + email = models.EmailField() |
| 270 | + |
| 271 | + # TYPING |
| 272 | + id: int |
| 273 | + region_id: int | None |
| 274 | + |
| 275 | + class Meta: |
| 276 | + verbose_name = _("Email Recipient") |
| 277 | + verbose_name_plural = _("Email Recipients") |
| 278 | + constraints = [ |
| 279 | + models.UniqueConstraint( |
| 280 | + fields=["type", "email"], |
| 281 | + condition=Q(region__isnull=True), |
| 282 | + name="unique_email_type_no_region", |
| 283 | + violation_error_message=_("Email must be unique for the given type when no region is assigned."), |
| 284 | + ), |
| 285 | + models.UniqueConstraint( |
| 286 | + fields=["type", "region", "email"], |
| 287 | + condition=Q(region__isnull=False), |
| 288 | + name="unique_email_type_region", |
| 289 | + violation_error_message=_("Email must be unique for the given type and region."), |
| 290 | + ), |
| 291 | + ] |
| 292 | + |
| 293 | + def __str__(self): |
| 294 | + return f"{self.get_type_display()} - {self.email}" |
| 295 | + |
| 296 | + def clean(self): |
| 297 | + if ( |
| 298 | + self.type |
| 299 | + in [ |
| 300 | + self.EmailType.DREF_ANTICIPATORY, |
| 301 | + self.EmailType.DREF_AA_GLOBAL_TEAM, |
| 302 | + ] |
| 303 | + and self.region is not None |
| 304 | + ): |
| 305 | + raise ValidationError(f"{self.get_type_display()} should not have a region assigned.") |
| 306 | + |
| 307 | + if self.type == self.EmailType.REGIONAL_COORDINATOR and self.region is None: |
| 308 | + raise ValidationError("Regional coordinator must have a region.") |
| 309 | + |
| 310 | + |
251 | 311 | class TimeFrame(models.IntegerChoices): |
252 | 312 | YEARS = 10, _("Years") |
253 | 313 | MONTHS = 20, _("Months") |
|
0 commit comments