Skip to content

Commit

Permalink
🐛(backend) manage is_main default value in serializers
Browse files Browse the repository at this point in the history
DRF verson 3.15 propagate 'default' from model field to serializer.
encode/django-rest-framework#9030
  • Loading branch information
kernicPanel committed Mar 18, 2024
1 parent e297901 commit 0929330
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/backend/joanie/core/serializers/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import markdown
from drf_spectacular.utils import extend_schema_field
from rest_framework import exceptions, serializers
from rest_framework.fields import empty
from rest_framework.generics import get_object_or_404

from joanie.core import enums, models
Expand Down Expand Up @@ -85,6 +86,15 @@ class Meta:
"id",
]

def run_validation(self, data=empty):
"""
Ignore is_main if not present in the data
"""
validated_data = super().run_validation(data)
if "is_main" not in data:
del validated_data["is_main"]
return validated_data


class CourseLightSerializer(AbilitiesModelSerializer):
"""
Expand Down
10 changes: 10 additions & 0 deletions src/backend/joanie/payment/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Serializers for api."""

from rest_framework import serializers
from rest_framework.fields import empty

from joanie.payment import models

Expand Down Expand Up @@ -30,3 +31,12 @@ class Meta:
"expiration_year",
"last_numbers",
]

def run_validation(self, data=empty):
"""
Ignore is_main if not present in the data
"""
validated_data = super().run_validation(data)
if "is_main" not in data:
del validated_data["is_main"]
return validated_data
2 changes: 2 additions & 0 deletions src/backend/joanie/tests/core/test_api_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ def test_api_address_create_update(self):
address = models.Address.objects.get()
self.assertEqual(address.owner, owner)
self.assertEqual(address.city, payload["city"])
self.assertTrue(address.is_main)

# finally update address
payload["title"] = "Office"
Expand All @@ -454,6 +455,7 @@ def test_api_address_create_update(self):
self.assertEqual(address.title, payload["title"])
self.assertEqual(address.owner, owner)
self.assertEqual(address.city, payload["city"])
self.assertTrue(address.is_main)

def test_api_address_create_update_read_only_fields(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def test_utils_contract_definition_generate_document_context_without_order(self)
"first_name": "<STUDENT_FIRST_NAME>",
"postcode": "<STUDENT_ADDRESS_POSTCODE>",
"title": "<STUDENT_ADDRESS_TITLE>",
"is_main": False,
},
"email": str(user.email),
"phone_number": str(user.phone_number),
Expand All @@ -209,6 +210,7 @@ def test_utils_contract_definition_generate_document_context_without_order(self)
"first_name": "<ORGANIZATION_FIRST_NAME>",
"postcode": "<ORGANIZATION_ADDRESS_POSTCODE>",
"title": "<ORGANIZATION_ADDRESS_TITLE>",
"is_main": False,
},
"logo": organization_fallback_logo,
"name": "<ORGANIZATION_NAME>",
Expand Down Expand Up @@ -273,6 +275,7 @@ def test_utils_contract_definition_generate_document_context_default_placeholder
"first_name": "<STUDENT_FIRST_NAME>",
"postcode": "<STUDENT_ADDRESS_POSTCODE>",
"title": "<STUDENT_ADDRESS_TITLE>",
"is_main": False,
},
"email": "<STUDENT_EMAIL>",
"phone_number": "<STUDENT_PHONE_NUMBER>",
Expand All @@ -286,6 +289,7 @@ def test_utils_contract_definition_generate_document_context_default_placeholder
"first_name": "<ORGANIZATION_FIRST_NAME>",
"postcode": "<ORGANIZATION_ADDRESS_POSTCODE>",
"title": "<ORGANIZATION_ADDRESS_TITLE>",
"is_main": False,
},
"logo": organization_fallback_logo,
"name": "<ORGANIZATION_NAME>",
Expand Down

0 comments on commit 0929330

Please sign in to comment.