Skip to content

Commit 63ce7c8

Browse files
Add modified_by and modified_at in local-unit api.
1 parent 492feeb commit 63ce7c8

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

local_units/serializers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,14 @@ class PrivateLocalUnitDetailSerializer(NestedCreateMixin, NestedUpdateMixin):
246246
class Meta:
247247
model = LocalUnit
248248
fields = (
249+
"id",
249250
"local_branch_name",
250251
"english_branch_name",
251252
"type",
252253
"country",
253254
"created_at",
254255
"modified_at",
256+
"modified_by",
255257
"draft",
256258
"validated",
257259
"postcode",
@@ -405,6 +407,7 @@ class PrivateLocalUnitSerializer(serializers.ModelSerializer):
405407
type_details = LocalUnitTypeSerializer(source="type", read_only=True)
406408
health_details = MiniHealthDataSerializer(read_only=True, source="health")
407409
validated = serializers.BooleanField(read_only=True)
410+
modified_by_details = LocalUnitMiniUserSerializer(source="modified_by", read_only=True)
408411

409412
class Meta:
410413
model = LocalUnit
@@ -426,6 +429,8 @@ class Meta:
426429
"focal_person_en",
427430
"email",
428431
"phone",
432+
"modified_at",
433+
"modified_by_details",
429434
)
430435

431436
def get_location_details(self, unit) -> dict:

local_units/test_views.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.contrib.gis.geos import Point
55

66
from api.models import Country, Region
7+
from deployments.factories.user import UserFactory
78
from main.test_case import APITestCase
89

910
from .models import (
@@ -117,10 +118,10 @@ def test_filter(self):
117118
class TestLocalUnitsDetailView(APITestCase):
118119
def setUp(self):
119120
super().setUp()
120-
region = Region.objects.create(name=2)
121-
country = Country.objects.create(name="Nepal", iso3="NLP", region=region)
122-
type = LocalUnitType.objects.create(code=0, name="Code 0")
123-
LocalUnitFactory.create_batch(2, country=country, type=type)
121+
self.region = Region.objects.create(name=2)
122+
self.country = Country.objects.create(name="Nepal", iso3="NLP", region=self.region)
123+
self.type = LocalUnitType.objects.create(code=0, name="Code 0")
124+
LocalUnitFactory.create_batch(2, country=self.country, type=self.type)
124125

125126
def test_detail(self):
126127
local_unit = LocalUnit.objects.all().first()
@@ -133,6 +134,26 @@ def test_detail(self):
133134
self.assertEqual(response.data["type_details"]["name"], "Code 0")
134135
self.assertEqual(response.data["type_details"]["code"], 0)
135136

137+
def test_get_updated_at_updated_by(self):
138+
self.authenticate()
139+
user_1 = UserFactory.create()
140+
user_2 = UserFactory.create()
141+
user_1_local_units = LocalUnitFactory.create_batch(
142+
2, country=self.country, type=self.type, created_by=user_1, modified_by=user_1
143+
)
144+
user_2_local_units = LocalUnitFactory.create_batch(
145+
2, country=self.country, type=self.type, created_by=user_1, modified_by=user_2
146+
)
147+
user_1_local_unit = LocalUnit.objects.filter(id__in=[unit.id for unit in user_1_local_units]).first()
148+
user_2_local_unit = LocalUnit.objects.filter(id__in=[unit.id for unit in user_2_local_units]).first()
149+
response = self.client.get(f"/api/v2/local-units/{user_1_local_unit.id}/")
150+
self.assertIsNotNone(response.data["modified_at"])
151+
self.assertEqual(response.data["modified_by_details"]["id"], user_1.id)
152+
153+
response = self.client.get(f"/api/v2/local-units/{user_2_local_unit.id}/")
154+
self.assertIsNotNone(response.data["modified_at"])
155+
self.assertEqual(response.data["modified_by_details"]["id"], user_2.id)
156+
136157
def test_validate_local_units(self):
137158
local_unit = LocalUnit.objects.all().first()
138159
self.authenticate()
@@ -285,7 +306,7 @@ def test_create_local_unit_administrative(self):
285306
response = self.client.post("/api/v2/local-units/", data=data, format="json")
286307
self.assertEqual(response.status_code, 201)
287308

288-
def test_create_local_unit_health(self):
309+
def test_create_update_local_unit_health(self):
289310
region = Region.objects.create(name=2)
290311
country = Country.objects.create(name="Philippines", iso3="PHL", iso="PH", region=region)
291312
type = LocalUnitType.objects.create(code=2, name="Code 0")
@@ -368,3 +389,21 @@ def test_create_local_unit_health(self):
368389
self.client.force_authenticate(self.root_user)
369390
response = self.client.post("/api/v2/local-units/", data=data, format="json")
370391
self.assertEqual(response.status_code, 201)
392+
393+
# test update
394+
response = response.json()
395+
local_unit_id = response["id"]
396+
response_updated_1 = self.client.put(f"/api/v2/local-units/{local_unit_id}/", data=data, format="json").json()
397+
local_unit_obj = LocalUnit.objects.get(id=local_unit_id)
398+
self.assertIsNotNone(local_unit_obj.created_by)
399+
self.assertIsNotNone(response_updated_1["modified_by"])
400+
self.assertIsNotNone(response_updated_1["modified_at"])
401+
self.assertEqual(response_updated_1["modified_by"], local_unit_obj.created_by.id)
402+
403+
# update existing local_unit with new user
404+
user_1 = UserFactory()
405+
self.client.force_authenticate(user_1)
406+
response_updated_2 = self.client.put(f"/api/v2/local-units/{local_unit_id}/", data=data, format="json").json()
407+
self.assertEqual(response_updated_2["modified_by_details"]["id"], user_1.id)
408+
self.assertEqual(response_updated_2["created_by_details"]["id"], self.root_user.id)
409+
assert response_updated_1["modified_at"] < response_updated_2["modified_at"]

0 commit comments

Comments
 (0)