Skip to content

Commit

Permalink
Fix typing issue on previous_data (JsonField) (#2344)
Browse files Browse the repository at this point in the history
* Previous data typing issue

* Remove deprecate filter on queryset

* Fix failed deprecated test
  • Loading branch information
susilnem committed Jan 7, 2025
1 parent cc4449e commit da60bdd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
15 changes: 12 additions & 3 deletions local_units/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ def get_location_geojson(self, unit) -> dict:
return json.loads(unit.location.geojson)


"""
NOTE: This `PrivateLocalUnitDetailSerializer` is used to store the previous_data of local unit
changing the serializer might effect the data of previous_data
"""


class PrivateLocalUnitDetailSerializer(NestedCreateMixin, NestedUpdateMixin):
country_details = LocalUnitCountrySerializer(source="country", read_only=True)
type_details = LocalUnitTypeSerializer(source="type", read_only=True)
Expand Down Expand Up @@ -537,24 +543,27 @@ class RejectedReasonSerialzier(serializers.Serializer):


class LocalUnitChangeRequestSerializer(serializers.ModelSerializer):
local_unit_details = PrivateLocalUnitDetailSerializer(source="local_unit", read_only=True)
created_by_details = LocalUnitMiniUserSerializer(source="created_by", read_only=True)
status_details = serializers.CharField(source="get_status_display", read_only=True)
current_validator_details = serializers.CharField(source="get_current_validator_display", read_only=True)
# NOTE: Typing issue on JsonField, So returning as string
previous_data_details = serializers.SerializerMethodField(read_only=True)

class Meta:
model = LocalUnitChangeRequest
fields = (
"id",
"local_unit_details",
"status",
"status_details",
"current_validator",
"current_validator_details",
"created_by_details",
"previous_data",
"previous_data_details",
)

def get_previous_data_details(self, obj):
return obj.previous_data


class LocalUnitDeprecateSerializer(serializers.ModelSerializer):
deprecated_reason = serializers.ChoiceField(choices=LocalUnit.DeprecateReason, required=True)
Expand Down
6 changes: 3 additions & 3 deletions local_units/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_deprecate_local_unit(self):

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

self.client.force_authenticate(self.root_user)
# test revert deprecate
Expand Down Expand Up @@ -622,5 +622,5 @@ def test_latest_changes(self):
# Checking the latest changes
response = self.client.post(f"/api/v2/local-units/{local_unit_id}/latest-change-request/")
self.assert_200(response)
self.assertEqual(response.data["previous_data"]["local_branch_name"], previous_data["local_branch_name"])
self.assertEqual(response.data["previous_data"]["english_branch_name"], previous_data["english_branch_name"])
self.assertEqual(response.data["previous_data_details"]["local_branch_name"], previous_data["local_branch_name"])
self.assertEqual(response.data["previous_data_details"]["english_branch_name"], previous_data["english_branch_name"])
2 changes: 1 addition & 1 deletion local_units/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PrivateLocalUnitViewSet(viewsets.ModelViewSet):
"country",
"type",
"level",
).exclude(is_deprecated=True)
)
filterset_class = LocalUnitFilters
search_fields = (
"local_branch_name",
Expand Down

0 comments on commit da60bdd

Please sign in to comment.