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

Feature/local unit api #2339

Closed
Closed
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,
}
13 changes: 13 additions & 0 deletions local_units/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,16 @@ class Meta:
"created_by_details",
"previous_data",
)


class LocalUnitDepricateSerializer(serializers.Serializer):
deprecated_reason = serializers.ChoiceField(choices=LocalUnit.DeprecateReason.choices)
deprecated_reason_overview = serializers.CharField(required=False)

def save(self, instance):
"""Depricate the local unit object"""
instance.is_deprecated = True
instance.deprecated_reason = self.validated_data.get("deprecated_reason", LocalUnit.DeprecateReason.OTHER)
instance.deprecated_reason_overview = self.validated_data.get("deprecated_reason_overview", "")
instance.save()
return instance
32 changes: 31 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,35 @@ 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_depriciate_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.put(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)

# test revert deprecate
data = {}
url = f"/api/v2/local-units/{local_unit_obj.id}/revert-deprecate/"
response = self.client.put(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
41 changes: 41 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,
LocalUnitDepricateSerializer,
LocalUnitDetailSerializer,
LocalUnitOptionsSerializer,
LocalUnitSerializer,
Expand Down Expand Up @@ -101,6 +102,20 @@ def update(self, request, *args, **kwargs):
return response.Response(serializer.data)

@extend_schema(request=None, responses=PrivateLocalUnitSerializer)
@action(detail=False, methods=["post"], url_path="deprecate")
def deprecate(self, request):
"""Deprecate an object by ID."""
instance = self.get_object()
serializer = LocalUnitDepricateSerializer(data=request.data)
if serializer.is_valid():
serializer.save(instance)
return response.Response(
{"message": f"Object {instance.id}-{instance.is_deprecated} deprecated successfully."},
status=status.HTTP_200_OK,
)
return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@extend_schema(responses=PrivateLocalUnitSerializer)
@action(
detail=True,
url_path="validate",
Expand Down Expand Up @@ -214,6 +229,32 @@ 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=LocalUnitDepricateSerializer)
@action(detail=True, methods=["put"], url_path="deprecate")
def deprecate(self, request, pk=None):
"""Deprecate local unit object object"""
instance = self.get_object()
serializer = LocalUnitDepricateSerializer(data=request.data)
if serializer.is_valid():
serializer.save(instance)
return response.Response(
{"message": f"Object {instance.id}-{instance.is_deprecated} deprecated successfully."},
status=status.HTTP_200_OK,
)
return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@extend_schema(responses=PrivateLocalUnitSerializer)
@action(detail=True, methods=["put"], url_path="revert-deprecate")
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