Skip to content

Commit

Permalink
Use DynamicFieldsModelSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
kemar committed Jan 15, 2025
1 parent 0d16789 commit 3682606
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
6 changes: 5 additions & 1 deletion iaso/api/data_source_versions_synchronization/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ class DataSourceVersionsSynchronizationFilter(django_filters.rest_framework.Filt

class Meta:
model = DataSourceVersionsSynchronization
fields = ["id", "name", "created_by"]
fields = {
"id": ["exact"],
"name": ["icontains"],
"created_by": ["exact"],
}
40 changes: 31 additions & 9 deletions iaso/api/data_source_versions_synchronization/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from rest_framework import serializers

from iaso.api.common import DynamicFieldsModelSerializer
from iaso.models import (
DataSourceVersionsSynchronization,
SourceVersion,
Expand Down Expand Up @@ -33,7 +34,12 @@ class Meta:
fields = ["id", "number", "description", "data_source", "data_source_name"]


class DataSourceVersionsSynchronizationSerializer(serializers.ModelSerializer):
class DataSourceVersionsSynchronizationSerializer(DynamicFieldsModelSerializer):
"""
Inheriting from DynamicFieldsModelSerializer allows to fetch the data with only a specific subset of fields.
This is useful e.g. to build a dropdown in the UI with only the name and ID of objects.
"""

class Meta:
model = DataSourceVersionsSynchronization
fields = [
Expand All @@ -48,6 +54,18 @@ class Meta:
"created_at",
"updated_at",
]
default_fields = [
"id",
"name",
"source_version_to_update",
"source_version_to_compare_with",
"count_create",
"count_update",
"account",
"created_by",
"created_at",
"updated_at",
]
extra_kwargs = {
"id": {"read_only": True},
"count_create": {"read_only": True},
Expand All @@ -60,14 +78,18 @@ class Meta:

def to_representation(self, instance):
representation = super().to_representation(instance)
representation["source_version_to_update"] = DataSourceVersionNestedSerializer(
instance.source_version_to_update, read_only=True
).data
representation["source_version_to_compare_with"] = DataSourceVersionNestedSerializer(
instance.source_version_to_compare_with, read_only=True
).data
representation["account"] = AccountNestedSerializer(instance.account, read_only=True).data
representation["created_by"] = UserNestedSerializer(instance.created_by, read_only=True).data
if "source_version_to_update" in self.fields:
representation["source_version_to_update"] = DataSourceVersionNestedSerializer(
instance.source_version_to_update, read_only=True
).data
if "source_version_to_compare_with" in self.fields:
representation["source_version_to_compare_with"] = DataSourceVersionNestedSerializer(
instance.source_version_to_compare_with, read_only=True
).data
if "account" in self.fields:
representation["account"] = AccountNestedSerializer(instance.account, read_only=True).data
if "created_by" in self.fields:
representation["created_by"] = UserNestedSerializer(instance.created_by, read_only=True).data
return representation

def validate(self, validated_data):
Expand Down
16 changes: 14 additions & 2 deletions iaso/tests/api/data_source_versions_synchronization/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ def setUpTestData(cls):
cls.source_3 = m.SourceVersion.objects.create(data_source=cls.data_source, number=3)

cls.data_source_sync_1 = m.DataSourceVersionsSynchronization.objects.create(
name="New synchronization",
name="Synchronization Foo",
source_version_to_update=cls.source_1,
source_version_to_compare_with=cls.source_2,
account=cls.account,
created_by=cls.user,
)

cls.data_source_sync_2 = m.DataSourceVersionsSynchronization.objects.create(
name="New synchronization",
name="Synchronization Bar",
source_version_to_update=cls.source_2,
source_version_to_compare_with=cls.source_3,
account=cls.account,
Expand Down Expand Up @@ -76,6 +76,18 @@ def test_list_ok(self):
self.assertJSONResponse(response, 200)
self.assertEqual(2, len(response.data["results"]))

def test_list_for_dropdown(self):
self.client.force_authenticate(self.user)
search_param = "name__icontains=foo"
fields_param = "fields=id,name"
response = self.client.get(f"/api/datasources/sync/?{search_param}&{fields_param}")
self.assertEqual(1, len(response.data["results"]))
expected_result = {
"id": self.data_source_sync_1.pk,
"name": self.data_source_sync_1.name,
}
self.assertEqual(response.data["results"][0], expected_result)

def test_retrieve_ok(self):
self.client.force_authenticate(self.user)
response = self.client.get(f"/api/datasources/sync/{self.data_source_sync_1.id}/")
Expand Down

0 comments on commit 3682606

Please sign in to comment.