Skip to content

Commit 6e14afc

Browse files
Add new fields and Snapshot model for localunit (#2331)
* Add new fields in localunit and create a new model LocalUnitChangeRequest * Add migrations file for the new models and fields * Fix typo for DepricateReason * update models field for LocalUnit. * fix pre-commit --------- Co-authored-by: rup-narayan-rajbanshi <[email protected]>
1 parent d918172 commit 6e14afc

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Generated by Django 4.2.16 on 2024-11-29 11:02
2+
3+
import django.db.models.deletion
4+
from django.conf import settings
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
12+
("local_units", "0017_alter_healthdata_other_medical_heal"),
13+
]
14+
15+
operations = [
16+
migrations.AddField(
17+
model_name="localunit",
18+
name="deprecated_reason",
19+
field=models.IntegerField(
20+
blank=True,
21+
choices=[
22+
(1, "Non-existent local unit"),
23+
(2, "Incorrectly added local unit"),
24+
(3, "Security concerns"),
25+
(4, "Other"),
26+
],
27+
null=True,
28+
verbose_name="deprecated reason",
29+
),
30+
),
31+
migrations.AddField(
32+
model_name="localunit",
33+
name="deprecated_reason_overview",
34+
field=models.TextField(blank=True, null=True, verbose_name="Explain the reason why the local unit is being deleted"),
35+
),
36+
migrations.AddField(
37+
model_name="localunit",
38+
name="is_deprecated",
39+
field=models.BooleanField(blank=True, default=False, null=True, verbose_name="Is deprecated?"),
40+
),
41+
migrations.AddField(
42+
model_name="localunit",
43+
name="status",
44+
field=models.IntegerField(
45+
blank=True, choices=[(1, "Verified"), (2, "Unverified")], default=2, null=True, verbose_name="status"
46+
),
47+
),
48+
migrations.CreateModel(
49+
name="LocalUnitChangeRequest",
50+
fields=[
51+
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
52+
("previous_data", models.JSONField(default=dict, verbose_name="Previous data")),
53+
(
54+
"status",
55+
models.IntegerField(
56+
choices=[(1, "Pending"), (2, "Approved"), (3, "Revert")], default=1, verbose_name="status"
57+
),
58+
),
59+
(
60+
"current_validator",
61+
models.IntegerField(
62+
choices=[(1, "Local"), (2, "Regional"), (3, "Global")], default=1, verbose_name="Current validator"
63+
),
64+
),
65+
("triggered_at", models.DateTimeField(auto_now=True, verbose_name="Triggered at")),
66+
("updated_at", models.DateTimeField(auto_now=True, verbose_name="Updated at")),
67+
("rejected_data", models.JSONField(default=dict, verbose_name="Rejected data")),
68+
("rejected_reason", models.TextField(blank=True, null=True, verbose_name="Rejected reason")),
69+
(
70+
"local_unit",
71+
models.ForeignKey(
72+
on_delete=django.db.models.deletion.CASCADE,
73+
related_name="local_unit_change_request",
74+
to="local_units.localunit",
75+
verbose_name="Local Unit",
76+
),
77+
),
78+
(
79+
"triggered_by",
80+
models.ForeignKey(
81+
null=True,
82+
on_delete=django.db.models.deletion.SET_NULL,
83+
related_name="tiggered_by_local_unit",
84+
to=settings.AUTH_USER_MODEL,
85+
verbose_name="triggered by",
86+
),
87+
),
88+
(
89+
"updated_by",
90+
models.ForeignKey(
91+
null=True,
92+
on_delete=django.db.models.deletion.SET_NULL,
93+
related_name="updated_by_local_unit",
94+
to=settings.AUTH_USER_MODEL,
95+
verbose_name="updated by",
96+
),
97+
),
98+
],
99+
),
100+
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Generated by Django 4.2.16 on 2024-12-02 11:26
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("local_units", "0018_localunit_deprecated_reason_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="localunit",
15+
name="is_deprecated",
16+
field=models.BooleanField(default=False, verbose_name="Is deprecated?"),
17+
),
18+
migrations.AlterField(
19+
model_name="localunit",
20+
name="status",
21+
field=models.IntegerField(choices=[(1, "Verified"), (2, "Unverified")], default=2, verbose_name="status"),
22+
),
23+
migrations.AlterField(
24+
model_name="localunitchangerequest",
25+
name="triggered_at",
26+
field=models.DateTimeField(auto_now_add=True, verbose_name="Triggered at"),
27+
),
28+
]

local_units/models.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,16 @@ def __str__(self):
277277

278278
@reversion.register(follow=("health",))
279279
class LocalUnit(models.Model):
280+
class Status(models.IntegerChoices):
281+
VERIFIED = 1, _("Verified")
282+
UNVERIFIED = 2, _("Unverified")
283+
284+
class DeprecateReason(models.IntegerChoices):
285+
NON_EXISTENT = 1, _("Non-existent local unit")
286+
INCORRECTLY_ADDED = 2, _("Incorrectly added local unit")
287+
SECURITY_CONCERNS = 3, _("Security concerns")
288+
OTHER = 4, _("Other")
289+
280290
# added to track health local unit data (Table B)
281291
health = models.ForeignKey(
282292
HealthData, on_delete=models.SET_NULL, verbose_name=_("Health Data"), related_name="health_data", null=True, blank=True
@@ -340,12 +350,68 @@ class LocalUnit(models.Model):
340350
email = models.EmailField(max_length=255, blank=True, null=True, verbose_name=_("Email"))
341351
link = models.URLField(max_length=255, blank=True, null=True, verbose_name=_("Social link"))
342352
location = models.PointField(srid=4326, help_text="Local Unit Location")
353+
status = models.IntegerField(choices=Status.choices, verbose_name=_("status"), default=Status.UNVERIFIED)
354+
is_deprecated = models.BooleanField(default=False, verbose_name=_("Is deprecated?"))
355+
deprecated_reason = models.IntegerField(
356+
choices=DeprecateReason.choices, verbose_name=_("deprecated reason"), blank=True, null=True
357+
)
358+
deprecated_reason_overview = models.TextField(
359+
verbose_name=_("Explain the reason why the local unit is being deleted"), blank=True, null=True
360+
)
343361

344362
def __str__(self):
345363
branch_name = self.local_branch_name or self.english_branch_name
346364
return f"{branch_name} ({self.country.name})"
347365

348366

367+
class LocalUnitChangeRequest(models.Model):
368+
369+
class Status(models.IntegerChoices):
370+
PENDING = 1, _("Pending")
371+
APPROVED = 2, _("Approved")
372+
REVERT = 3, _("Revert")
373+
374+
class Validator(models.IntegerChoices):
375+
LOCAL = 1, _("Local")
376+
REGIONAL = 2, _("Regional")
377+
GLOBAL = 3, _("Global")
378+
379+
local_unit = models.ForeignKey(
380+
LocalUnit, on_delete=models.CASCADE, verbose_name=_("Local Unit"), related_name="local_unit_change_request"
381+
)
382+
previous_data = models.JSONField(verbose_name=_("Previous data"), default=dict)
383+
status = models.IntegerField(choices=Status.choices, verbose_name=_("status"), default=Status.PENDING)
384+
current_validator = models.IntegerField(
385+
choices=Validator.choices, verbose_name=_("Current validator"), default=Validator.LOCAL
386+
)
387+
388+
# NOTE: triggered_by is the user who created the request
389+
triggered_by = models.ForeignKey(
390+
settings.AUTH_USER_MODEL,
391+
verbose_name=_("triggered by"),
392+
on_delete=models.SET_NULL,
393+
null=True,
394+
related_name="tiggered_by_local_unit",
395+
)
396+
triggered_at = models.DateTimeField(verbose_name=_("Triggered at"), auto_now_add=True)
397+
398+
# NOTE: updated_by is the user who approved/revert the request
399+
updated_by = models.ForeignKey(
400+
settings.AUTH_USER_MODEL,
401+
verbose_name=_("updated by"),
402+
on_delete=models.SET_NULL,
403+
null=True,
404+
related_name="updated_by_local_unit",
405+
)
406+
updated_at = models.DateTimeField(verbose_name=_("Updated at"), auto_now=True)
407+
rejected_data = models.JSONField(verbose_name=_("Rejected data"), default=dict)
408+
rejected_reason = models.TextField(verbose_name=_("Rejected reason"), blank=True, null=True)
409+
410+
def __str__(self):
411+
branch_name = self.local_branch_name or self.english_branch_name
412+
return f"{branch_name}-Change Request-{self.id}"
413+
414+
349415
class DelegationOfficeType(models.Model):
350416
code = models.IntegerField(verbose_name=_("Type Code"), validators=[MaxValueValidator(10), MinValueValidator(0)])
351417
name = models.CharField(max_length=100, verbose_name=_("Name"))

0 commit comments

Comments
 (0)