Skip to content

Commit 8997f7e

Browse files
committed
LocalUnit: Create, Update, Revert, Latest changes Apis (#2336)
* Add Create,Update,Validate Apis logics * Add changes in apis and add new migration file * Add local units enums to global enums * Add test cases for local units apis * Add latest changes api for local units - validate the location - Revert API - add test cases * Update logics on latest-change-request api
1 parent 6e14afc commit 8997f7e

File tree

9 files changed

+417
-37
lines changed

9 files changed

+417
-37
lines changed

local_units/enums.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from . import models
2+
3+
enum_register = {
4+
"deprecate_reason": models.LocalUnit.DeprecateReason,
5+
}

local_units/filterset.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Meta:
1313
"type__code",
1414
"draft",
1515
"validated",
16+
"is_locked",
1617
)
1718

1819

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Generated by Django 4.2.16 on 2024-12-10 09:09
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("local_units", "0019_alter_localunit_is_deprecated_alter_localunit_status_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AlterModelOptions(
14+
name="localunitchangerequest",
15+
options={"ordering": ("id",)},
16+
),
17+
migrations.RemoveField(
18+
model_name="localunit",
19+
name="status",
20+
),
21+
migrations.AddField(
22+
model_name="localunit",
23+
name="is_locked",
24+
field=models.BooleanField(default=False, verbose_name="Is locked?"),
25+
),
26+
]

local_units/models.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,6 @@ 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")
283280

284281
class DeprecateReason(models.IntegerChoices):
285282
NON_EXISTENT = 1, _("Non-existent local unit")
@@ -350,7 +347,7 @@ class DeprecateReason(models.IntegerChoices):
350347
email = models.EmailField(max_length=255, blank=True, null=True, verbose_name=_("Email"))
351348
link = models.URLField(max_length=255, blank=True, null=True, verbose_name=_("Social link"))
352349
location = models.PointField(srid=4326, help_text="Local Unit Location")
353-
status = models.IntegerField(choices=Status.choices, verbose_name=_("status"), default=Status.UNVERIFIED)
350+
is_locked = models.BooleanField(default=False, verbose_name=_("Is locked?"))
354351
is_deprecated = models.BooleanField(default=False, verbose_name=_("Is deprecated?"))
355352
deprecated_reason = models.IntegerField(
356353
choices=DeprecateReason.choices, verbose_name=_("deprecated reason"), blank=True, null=True
@@ -407,8 +404,12 @@ class Validator(models.IntegerChoices):
407404
rejected_data = models.JSONField(verbose_name=_("Rejected data"), default=dict)
408405
rejected_reason = models.TextField(verbose_name=_("Rejected reason"), blank=True, null=True)
409406

407+
class Meta:
408+
ordering = ("id",)
409+
410410
def __str__(self):
411-
branch_name = self.local_branch_name or self.english_branch_name
411+
# NOTE: N+1, make sure to use select_related
412+
branch_name = self.local_unit.local_branch_name or self.local_unit.english_branch_name
412413
return f"{branch_name}-Change Request-{self.id}"
413414

414415

local_units/serializers.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
HealthData,
2323
HospitalType,
2424
LocalUnit,
25+
LocalUnitChangeRequest,
2526
LocalUnitLevel,
2627
LocalUnitType,
2728
PrimaryHCC,
@@ -242,6 +243,7 @@ class PrivateLocalUnitDetailSerializer(NestedCreateMixin, NestedUpdateMixin):
242243
modified_by_details = LocalUnitMiniUserSerializer(source="modified_by", read_only=True)
243244
created_by_details = LocalUnitMiniUserSerializer(source="created_by", read_only=True)
244245
version_id = serializers.SerializerMethodField()
246+
is_locked = serializers.BooleanField(read_only=True)
245247

246248
class Meta:
247249
model = LocalUnit
@@ -283,6 +285,7 @@ class Meta:
283285
"modified_by_details",
284286
"created_by_details",
285287
"version_id",
288+
"is_locked",
286289
)
287290

288291
def get_location_details(self, unit) -> dict:
@@ -338,6 +341,7 @@ def create(self, validated_data):
338341
)
339342
validated_data["location"] = GEOSGeometry("POINT(%f %f)" % (lng, lat))
340343
validated_data["created_by"] = self.context["request"].user
344+
validated_data["is_locked"] = True
341345
return super().create(validated_data)
342346

343347
def update(self, instance, validated_data):
@@ -408,6 +412,7 @@ class PrivateLocalUnitSerializer(serializers.ModelSerializer):
408412
health_details = MiniHealthDataSerializer(read_only=True, source="health")
409413
validated = serializers.BooleanField(read_only=True)
410414
modified_by_details = LocalUnitMiniUserSerializer(source="modified_by", read_only=True)
415+
is_locked = serializers.BooleanField(read_only=True)
411416

412417
class Meta:
413418
model = LocalUnit
@@ -431,6 +436,7 @@ class Meta:
431436
"phone",
432437
"modified_at",
433438
"modified_by_details",
439+
"is_locked",
434440
)
435441

436442
def get_location_details(self, unit) -> dict:
@@ -520,3 +526,27 @@ class Meta:
520526
"city",
521527
"address",
522528
)
529+
530+
531+
class RejectedReasonSerialzier(serializers.Serializer):
532+
reason = serializers.CharField(required=True)
533+
534+
535+
class LocalUnitChangeRequestSerializer(serializers.ModelSerializer):
536+
local_unit_details = PrivateLocalUnitDetailSerializer(source="local_unit", read_only=True)
537+
created_by_details = LocalUnitMiniUserSerializer(source="created_by", read_only=True)
538+
status_details = serializers.CharField(source="get_status_display", read_only=True)
539+
current_validator_details = serializers.CharField(source="get_current_validator_display", read_only=True)
540+
541+
class Meta:
542+
model = LocalUnitChangeRequest
543+
fields = (
544+
"id",
545+
"local_unit_details",
546+
"status",
547+
"status_details",
548+
"current_validator",
549+
"current_validator_details",
550+
"created_by_details",
551+
"previous_data",
552+
)

0 commit comments

Comments
 (0)