Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocalUnit: Create, Update, Revert, Latest changes Apis #2336

Merged
merged 6 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions local_units/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import models

enum_register = {
"deprecate_reason": models.LocalUnit.DeprecateReason,
}
1 change: 1 addition & 0 deletions local_units/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Meta:
"type__code",
"draft",
"validated",
"is_locked",
)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.16 on 2024-12-10 09:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("local_units", "0019_alter_localunit_is_deprecated_alter_localunit_status_and_more"),
]

operations = [
migrations.AlterModelOptions(
name="localunitchangerequest",
options={"ordering": ("id",)},
),
migrations.RemoveField(
model_name="localunit",
name="status",
),
migrations.AddField(
model_name="localunit",
name="is_locked",
field=models.BooleanField(default=False, verbose_name="Is locked?"),
),
]
11 changes: 6 additions & 5 deletions local_units/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,6 @@ def __str__(self):

@reversion.register(follow=("health",))
class LocalUnit(models.Model):
class Status(models.IntegerChoices):
VERIFIED = 1, _("Verified")
UNVERIFIED = 2, _("Unverified")

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

class Meta:
ordering = ("id",)

def __str__(self):
branch_name = self.local_branch_name or self.english_branch_name
# NOTE: N+1, make sure to use select_related
branch_name = self.local_unit.local_branch_name or self.local_unit.english_branch_name
thenav56 marked this conversation as resolved.
Show resolved Hide resolved
return f"{branch_name}-Change Request-{self.id}"


Expand Down
30 changes: 30 additions & 0 deletions local_units/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
HealthData,
HospitalType,
LocalUnit,
LocalUnitChangeRequest,
LocalUnitLevel,
LocalUnitType,
PrimaryHCC,
Expand Down Expand Up @@ -242,6 +243,7 @@ class PrivateLocalUnitDetailSerializer(NestedCreateMixin, NestedUpdateMixin):
modified_by_details = LocalUnitMiniUserSerializer(source="modified_by", read_only=True)
created_by_details = LocalUnitMiniUserSerializer(source="created_by", read_only=True)
version_id = serializers.SerializerMethodField()
is_locked = serializers.BooleanField(read_only=True)

class Meta:
model = LocalUnit
Expand Down Expand Up @@ -283,6 +285,7 @@ class Meta:
"modified_by_details",
"created_by_details",
"version_id",
"is_locked",
)

def get_location_details(self, unit) -> dict:
Expand Down Expand Up @@ -338,6 +341,7 @@ def create(self, validated_data):
)
validated_data["location"] = GEOSGeometry("POINT(%f %f)" % (lng, lat))
validated_data["created_by"] = self.context["request"].user
validated_data["is_locked"] = True
return super().create(validated_data)

def update(self, instance, validated_data):
Expand Down Expand Up @@ -408,6 +412,7 @@ class PrivateLocalUnitSerializer(serializers.ModelSerializer):
health_details = MiniHealthDataSerializer(read_only=True, source="health")
validated = serializers.BooleanField(read_only=True)
modified_by_details = LocalUnitMiniUserSerializer(source="modified_by", read_only=True)
is_locked = serializers.BooleanField(read_only=True)

class Meta:
model = LocalUnit
Expand All @@ -431,6 +436,7 @@ class Meta:
"phone",
"modified_at",
"modified_by_details",
"is_locked",
)

def get_location_details(self, unit) -> dict:
Expand Down Expand Up @@ -520,3 +526,27 @@ class Meta:
"city",
"address",
)


class RejectedReasonSerialzier(serializers.Serializer):
reason = serializers.CharField(required=True)


class LocalUnitChangeRequestSerializer(serializers.ModelSerializer):
local_unit_details = PrivateLocalUnitDetailSerializer(source="local_unit", read_only=True)
created_by_details = LocalUnitMiniUserSerializer(source="created_by", read_only=True)
status_details = serializers.CharField(source="get_status_display", read_only=True)
current_validator_details = serializers.CharField(source="get_current_validator_display", read_only=True)

class Meta:
model = LocalUnitChangeRequest
fields = (
"id",
"local_unit_details",
"status",
"status_details",
"current_validator",
"current_validator_details",
"created_by_details",
"previous_data",
)
Loading
Loading