Skip to content

Commit

Permalink
🐛(backend) serializers readonly
Browse files Browse the repository at this point in the history
DRF verson 3.15 propagate 'default' from model field to serializer.
  • Loading branch information
kernicPanel committed Mar 18, 2024
1 parent 1cb3721 commit 3009054
Show file tree
Hide file tree
Showing 3 changed files with 22 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

0 comments on commit 3009054

Please sign in to comment.