Skip to content

Commit

Permalink
Add latest changes api for local units
Browse files Browse the repository at this point in the history
- validate the location
- Revert API
- add test cases
  • Loading branch information
susilnem committed Dec 9, 2024
1 parent fd8a2ef commit 4061d6b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
9 changes: 0 additions & 9 deletions local_units/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
HealthData,
HospitalType,
LocalUnit,
LocalUnitChangeRequest,
LocalUnitLevel,
LocalUnitType,
PrimaryHCC,
Expand Down Expand Up @@ -528,13 +527,5 @@ class Meta:
)


# NOTE: Currently `FullLocalUnitSerializer` is used for storing previous version of LocalUnit
class FullLocalUnitSerializer(serializers.ModelSerializer):

class Meta:
model = LocalUnit
fields = "__all__"


class RejectedReasonSerialzier(serializers.Serializer):
reason = serializers.CharField(required=True)
13 changes: 13 additions & 0 deletions local_units/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def format_local_unit(data: dict) -> dict:
"""
Formatting the json local unit data
"""
location = data.get("location_details", None)
if location:
coordinates = location.get("coordinates")
data["location_json"] = {
"type": location.get("type"),
"lat": coordinates[1],
"lng": coordinates[0],
}
return data
41 changes: 21 additions & 20 deletions local_units/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
)
from local_units.serializers import (
DelegationOfficeSerializer,
FullLocalUnitSerializer,
LocalUnitDetailSerializer,
LocalUnitOptionsSerializer,
LocalUnitSerializer,
PrivateLocalUnitDetailSerializer,
PrivateLocalUnitSerializer,
RejectedReasonSerialzier,
)
from local_units.utils import format_local_unit
from main.permissions import DenyGuestUserPermission


Expand Down Expand Up @@ -67,8 +67,9 @@ def create(self, request, *args, **kwargs):
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)

clean_data = format_local_unit(serializer.data)

# Creating a new change request for the local unit
clean_data = FullLocalUnitSerializer(serializer.instance).data
LocalUnitChangeRequest.objects.create(
local_unit=serializer.instance,
previous_data=clean_data,
Expand All @@ -81,10 +82,17 @@ def update(self, request, *args, **kwargs):
local_unit = self.get_object()

# NOTE: Checking if the local unit is locked.
# TODO: This should be moved to a permission class and validators can update the local unit
if local_unit.is_locked:
return bad_request("Local unit is locked and cannot be updated")

clean_data = FullLocalUnitSerializer(local_unit).data
# NOTE: Locking the local unit after the change request is created
local_unit.is_locked = True
serializer = self.get_serializer(local_unit, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)

clean_data = format_local_unit(serializer.data)

# Creating a new change request for the local unit
LocalUnitChangeRequest.objects.create(
Expand All @@ -93,10 +101,7 @@ def update(self, request, *args, **kwargs):
status=LocalUnitChangeRequest.Status.PENDING,
triggered_by=request.user,
)
# NOTE: Locking the local unit after the change request is created
local_unit.is_locked = True
local_unit.save(update_fields=["is_locked"])
return super().update(request, *args, **kwargs)
return response.Response(serializer.data)

@extend_schema(request=None, responses=PrivateLocalUnitSerializer)
@action(
Expand All @@ -109,9 +114,9 @@ def update(self, request, *args, **kwargs):
def get_validate(self, request, pk=None, version=None):
local_unit = self.get_object()

clean_data = FullLocalUnitSerializer(local_unit).data
serializer = PrivateLocalUnitDetailSerializer(local_unit, context={"request": request})
clean_data = format_local_unit(serializer.data)

# Creating a new change request to revert the local unit
LocalUnitChangeRequest.objects.create(
local_unit=local_unit,
previous_data=clean_data,
Expand All @@ -128,20 +133,22 @@ def get_validate(self, request, pk=None, version=None):
serializer = PrivateLocalUnitSerializer(local_unit, context={"request": request})
return response.Response(serializer.data)

@extend_schema(request=RejectedReasonSerialzier, responses=PrivateLocalUnitSerializer)
@extend_schema(request=RejectedReasonSerialzier, responses=PrivateLocalUnitDetailSerializer)
@action(
detail=True,
url_path="revert",
methods=["post"],
serializer_class=RejectedReasonSerialzier,
permission_classes=[permissions.IsAuthenticated, ValidateLocalUnitPermission, DenyGuestUserPermission],
)
def get_revert(self, request, pk=None, version=None):
local_unit = self.get_object()

if local_unit.validated:
return bad_request("Local unit is already validated and cannot be reverted")

clean_data = FullLocalUnitSerializer(local_unit).data
full_serializer = PrivateLocalUnitDetailSerializer(local_unit, context={"request": request})
clean_data = format_local_unit(full_serializer.data)

serializer = RejectedReasonSerialzier(data=request.data)
serializer.is_valid(raise_exception=True)
Expand All @@ -168,7 +175,7 @@ def get_revert(self, request, pk=None, version=None):
local_unit.is_locked = False
local_unit.save(update_fields=["is_locked"])
# reverting the previous data of change request to local unit by passing through serializer
serializer = PrivateLocalUnitSerializer(
serializer = PrivateLocalUnitDetailSerializer(
local_unit,
data=last_change_request.previous_data,
context={"request": request},
Expand All @@ -184,6 +191,7 @@ def get_revert(self, request, pk=None, version=None):
url_path="latest-changes",
methods=["post"],
serializer_class=PrivateLocalUnitDetailSerializer,
permission_classes=[permissions.IsAuthenticated, IsAuthenticatedForLocalUnit, DenyGuestUserPermission],
)
def get_latest_changes(self, request, pk=None, version=None):
local_unit = self.get_object()
Expand All @@ -196,14 +204,7 @@ def get_latest_changes(self, request, pk=None, version=None):
if not change_request:
return bad_request("Last change request not found")

serializer = PrivateLocalUnitDetailSerializer(
local_unit,
data=change_request.previous_data,
context={"request": request},
partial=True,
)
serializer.is_valid(raise_exception=True)
return response.Response(serializer.data)
return response.Response(change_request.previous_data)


class LocalUnitViewSet(viewsets.ModelViewSet):
Expand Down

0 comments on commit 4061d6b

Please sign in to comment.