Skip to content

Commit

Permalink
Add modified_by and modified_at in local-unit api.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rup-Narayan-Rajbanshi committed Jul 9, 2024
1 parent 492feeb commit 33d33cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions local_units/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ class PrivateLocalUnitSerializer(serializers.ModelSerializer):
type_details = LocalUnitTypeSerializer(source="type", read_only=True)
health_details = MiniHealthDataSerializer(read_only=True, source="health")
validated = serializers.BooleanField(read_only=True)
modified_by_details = LocalUnitMiniUserSerializer(source="modified_by", read_only=True)

class Meta:
model = LocalUnit
Expand All @@ -426,6 +427,8 @@ class Meta:
"focal_person_en",
"email",
"phone",
"modified_at",
"modified_by_details",
)

def get_location_details(self, unit) -> dict:
Expand Down
19 changes: 15 additions & 4 deletions local_units/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.contrib.gis.geos import Point

from api.models import Country, Region
from deployments.factories.user import UserFactory
from main.test_case import APITestCase

from .models import (
Expand Down Expand Up @@ -117,10 +118,10 @@ def test_filter(self):
class TestLocalUnitsDetailView(APITestCase):
def setUp(self):
super().setUp()
region = Region.objects.create(name=2)
country = Country.objects.create(name="Nepal", iso3="NLP", region=region)
type = LocalUnitType.objects.create(code=0, name="Code 0")
LocalUnitFactory.create_batch(2, country=country, type=type)
self.region = Region.objects.create(name=2)
self.country = Country.objects.create(name="Nepal", iso3="NLP", region=self.region)
self.type = LocalUnitType.objects.create(code=0, name="Code 0")
LocalUnitFactory.create_batch(2, country=self.country, type=self.type)

def test_detail(self):
local_unit = LocalUnit.objects.all().first()
Expand All @@ -133,6 +134,16 @@ def test_detail(self):
self.assertEqual(response.data["type_details"]["name"], "Code 0")
self.assertEqual(response.data["type_details"]["code"], 0)

def test_updated_at_updated_by(self):
self.authenticate()
user = UserFactory.create()
local_units = LocalUnitFactory.create_batch(2, country=self.country, type=self.type, created_by=user, modified_by=user)
local_unit = LocalUnit.objects.filter(id__in=[unit.id for unit in local_units]).first()
response = self.client.get(f"/api/v2/local-units/{local_unit.id}/")
self.assertIsNotNone(response.data["modified_by_details"]["id"])
self.assertIsNotNone(response.data["modified_at"])
self.assertEqual(response.data["modified_by_details"]["id"], user.id)

def test_validate_local_units(self):
local_unit = LocalUnit.objects.all().first()
self.authenticate()
Expand Down

0 comments on commit 33d33cc

Please sign in to comment.