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

Add API to deprecate local unit #2340

Merged
merged 16 commits into from
Dec 12, 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
2 changes: 2 additions & 0 deletions local_units/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

enum_register = {
"deprecate_reason": models.LocalUnit.DeprecateReason,
"validation_status": models.LocalUnitChangeRequest.Status,
"validators": models.LocalUnitChangeRequest.Validator,
}
29 changes: 29 additions & 0 deletions local_units/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,32 @@ class Meta:
"created_by_details",
"previous_data",
)


class LocalUnitDeprecateSerializer(serializers.ModelSerializer):
deprecated_reason = serializers.ChoiceField(choices=LocalUnit.DeprecateReason, required=True)
deprecated_reason_overview = serializers.CharField(required=True, allow_blank=False)

class Meta:
model = LocalUnit
fields = ("deprecated_reason", "deprecated_reason_overview")

def create(self, _):
raise serializers.ValidationError({"non_field_errors": gettext("Create is not allowed")})

def validate(self, attrs):
instance = self.instance

if instance and instance.is_deprecated:
raise serializers.ValidationError({"non_field_errors": gettext("This object is already depricated")})

return attrs
susilnem marked this conversation as resolved.
Show resolved Hide resolved

def update(self, instance, validated_data):
instance.is_deprecated = True
instance.deprecated_reason = validated_data.get("deprecated_reason", instance.deprecated_reason)
instance.deprecated_reason_overview = validated_data.get(
"deprecated_reason_overview", instance.deprecated_reason_overview
)
instance.save(update_fields=["is_deprecated", "deprecated_reason", "deprecated_reason_overview"])
return instance
37 changes: 36 additions & 1 deletion local_units/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import factory
from django.contrib.gis.geos import Point
from factory import fuzzy

from api.models import Country, Region
from deployments.factories.user import UserFactory
Expand All @@ -25,7 +26,7 @@

class LocalUnitFactory(factory.django.DjangoModelFactory):
location = Point(12, 38)
date_of_data = factory.fuzzy.FuzzyDate(datetime.date(2024, 1, 2))
date_of_data = fuzzy.FuzzyDate(datetime.date(2024, 1, 2))

class Meta:
model = LocalUnit
Expand Down Expand Up @@ -64,6 +65,40 @@ def test_list(self):
# self.assertEqual(response.data['results'][0]['type_details']['name'], 'Code 0')
# self.assertEqual(response.data['results'][0]['type_details']['code'], 0)

def test_deprecate_local_unit(self):
country = Country.objects.all().first()
type = LocalUnitType.objects.all().first()
local_unit_obj = LocalUnitFactory.create(
country=country, type=type, draft=True, validated=False, date_of_data="2023-09-09"
)

self.authenticate()
url = f"/api/v2/local-units/{local_unit_obj.id}/deprecate/"
data = {
"deprecated_reason": LocalUnit.DeprecateReason.INCORRECTLY_ADDED,
"deprecated_reason_overview": "test reason",
}
response = self.client.post(url, data=data)
local_unit_obj = LocalUnit.objects.get(id=local_unit_obj.id)

self.assert_200(response)
self.assertEqual(local_unit_obj.is_deprecated, True)
self.assertEqual(local_unit_obj.deprecated_reason, LocalUnit.DeprecateReason.INCORRECTLY_ADDED)

# Test for validation
response = self.client.post(url, data=data)
self.assert_400(response)

# test revert deprecate
data = {}
url = f"/api/v2/local-units/{local_unit_obj.id}/revert-deprecate/"
response = self.client.post(url, data=data)
local_unit_obj = LocalUnit.objects.get(id=local_unit_obj.id)
self.assert_200(response)
self.assertEqual(local_unit_obj.is_deprecated, False)
self.assertEqual(local_unit_obj.deprecated_reason, None)
self.assertEqual(local_unit_obj.deprecated_reason_overview, "")

def test_filter(self):
self.authenticate()
response = self.client.get("/api/v2/local-units/?country__name=Nepal")
Expand Down
37 changes: 37 additions & 0 deletions local_units/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from local_units.serializers import (
DelegationOfficeSerializer,
LocalUnitChangeRequestSerializer,
LocalUnitDeprecateSerializer,
LocalUnitDetailSerializer,
LocalUnitOptionsSerializer,
LocalUnitSerializer,
Expand Down Expand Up @@ -214,6 +215,42 @@ def get_latest_changes(self, request, pk=None, version=None):
serializer = LocalUnitChangeRequestSerializer(change_request, context={"request": request})
return response.Response(serializer.data)

@extend_schema(request=LocalUnitDeprecateSerializer, responses=None)
@action(
detail=True,
methods=["post"],
url_path="deprecate",
serializer_class=LocalUnitDeprecateSerializer,
permission_classes=[permissions.IsAuthenticated, DenyGuestUserPermission],
)
def deprecate(self, request, pk=None):
"""Deprecate local unit object object"""
susilnem marked this conversation as resolved.
Show resolved Hide resolved
instance = self.get_object()
serializer = LocalUnitDeprecateSerializer(instance, data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return response.Response(
{"message": "Local unit object deprecated successfully."},
status=status.HTTP_200_OK,
)

@extend_schema(request=None, responses=PrivateLocalUnitSerializer)
@action(
detail=True,
methods=["post"],
url_path="revert-deprecate",
permission_classes=[permissions.IsAuthenticated, DenyGuestUserPermission],
)
def revert_deprecate(self, request, pk=None):
"""Revert the deprecate local unit object."""
local_unit = self.get_object()
local_unit.is_deprecated = False
local_unit.deprecated_reason = None
local_unit.deprecated_reason_overview = ""
local_unit.save(update_fields=["is_deprecated", "deprecated_reason", "deprecated_reason_overview"])
serializer = PrivateLocalUnitSerializer(local_unit, context={"request": request})
return response.Response(serializer.data)


class LocalUnitViewSet(viewsets.ModelViewSet):
queryset = LocalUnit.objects.select_related(
Expand Down
Loading