Commit 77cce1b1 authored by Florent Chehab's avatar Florent Chehab
Browse files

feat(backend): refactor/cleaned/ infer get_serializer

* Cleaned all init files
* Infer the serializer from the model instead of having it in the models
* Updated the doc accordingly
* Fixed typos

Fixes #93
Fixes #85
parent cb86531b
......@@ -17,10 +17,6 @@ class ForTestingVersioning(VersionedEssentialModule):
bbb = models.CharField(max_length=100)
def get_serializer(cls):
return ForTestingVersioningSerializer
class ForTestingVersioningSerializer(VersionedEssentialModuleSerializer):
......@@ -6,7 +6,7 @@ from backend_app.models.abstract.base import (
from backend_app.models import SEMESTER_OPTIONS
from backend_app.models.shared import SEMESTER_OPTIONS
class Offer(BaseModel):
......@@ -8,7 +8,7 @@ from backend_app.models.abstract.essentialModule import (
from backend_app.models import SEMESTER_OPTIONS
from backend_app.models.shared import SEMESTER_OPTIONS
class PreviousDeparture(EssentialModule):
# This file is not a model. It is file to hold shared things across models.
SEMESTER_OPTIONS = (("a", "autumn"), ("p", "spring"))
......@@ -6,7 +6,7 @@ from backend_app.models.abstract.essentialModule import (
from django.core.exceptions import ValidationError
from rest_framework.validators import ValidationError as RFValidationError
from backend_app.validators.tag.url import validate_extension
from backend_app.validators.tags import validate_extension
from django.conf import settings
......@@ -7,10 +7,6 @@ class UniversityDri(Module):
universities = models.ManyToManyField(University, related_name="university_dri")
def get_serializer(cls):
return UniversityDriSerializer
class UniversityDriSerializer(ModuleSerializer):
class Meta:
......@@ -25,10 +25,6 @@ class UniversityInfo(Module):
costs_currency = models.ForeignKey(Currency, on_delete=models.PROTECT, null=True)
def get_serializer(cls):
return UniversityInfoSerializer
class UniversityInfoSerializer(ModuleSerializer):
class Meta:
......@@ -13,10 +13,6 @@ class UniversityScholarship(Scholarship):
University, related_name="university_scholarships"
def get_serializer(cls):
return UniversityScholarshipSerializer
class UniversityScholarshipSerializer(ScholarshipSerializer):
class Meta:
......@@ -25,10 +25,6 @@ class UniversitySemestersDates(Module):
autumn_begin = models.DateField(null=True, blank=True)
autumn_end = models.DateField(null=True, blank=True)
def get_serializer(cls):
return UniversitySemestersDatesSerializer
class UniversitySemestersDatesSerializer(ModuleSerializer):
def validate(self, attrs):
......@@ -13,10 +13,6 @@ class UniversityTaggedItem(TaggedItem):
University, on_delete=models.PROTECT, related_name="university_tagged_items"
def get_serializer(cls):
return UniversityTaggedItemSerializer
class Meta:
unique_together = ("university", "tag", "importance_level")
......@@ -14,6 +14,39 @@ class VersionSerializer(BaseModelSerializer):
data = serializers.SerializerMethodField()
serializers_mapping = None
def get_serializers_mapping(cls) -> dict:
Function that returns a mapping from model name to the serializer
class that should be used to return the versioned data.
if cls.serializers_mapping is None:
# Prevent cyclic imports
from backend_app.config.viewsets import get_viewsets_info
# A little bit of optimization to easily find the serializer class associated with a model
cls.serializers_mapping = dict()
viewsets = map(
lambda v: v.Viewset,
get_viewsets_info(requires_testing="smart", is_api_view=False),
for viewset in viewsets:
serializer = viewset().get_serializer_class()
model = serializer.Meta.model
cls.serializers_mapping[model.__name__] = serializer
# Override if models has a get_serializer method
for viewset in viewsets:
model = viewset().get_serializer_class().Meta.model
cls.serializers_mapping[model.__name__] = model.get_serializer()
except AttributeError:
return cls.serializers_mapping
def get_data(self, obj):
Serilizer for the data field
......@@ -26,7 +59,8 @@ class VersionSerializer(BaseModelSerializer):
djangoSerializers.deserialize(obj.format, data, ignorenonexistent=True)
# Version is valid,
obj_serializer = tmp.object.get_serializer()
obj_serializer = self.get_serializers_mapping()[type(tmp.object).__name__]
new_context = dict(self.context)
new_context["view"].action = "list"
return obj_serializer(tmp.object, context=new_context).data
......@@ -4,7 +4,7 @@ from reversion.models import Version
def squash_revision_by_user(sender, obj, **kwargs):
It should also work with moderation as obj will be a versionned object
It should also work with moderation as obj will be a versioned object
versions = (
from django.test import TestCase
from backend_app.load_data import load_all
from backend_app.load_data.load_all import load_all
class ModerationTestCase(TestCase):
......@@ -2,7 +2,7 @@ from django.test import TestCase
import pytest
from rest_framework.validators import ValidationError as RFValidationError
from django.core.validators import ValidationError as DJValidationError
from backend_app.validators.tag.url import validate_extension, validate_url
from backend_app.validators.tags import validate_extension, validate_url
class ValidationUrlTestCase(TestCase):
......@@ -4,6 +4,7 @@ from django.conf import settings
from reversion.models import Version
from backend_app.signals.squash_revisions import new_revision_saved
from django.test import override_settings
from django.contrib.contenttypes.models import ContentType
class VersioningTestCase(WithUserTestCase):
......@@ -69,3 +70,7 @@ class VersioningTestCase(WithUserTestCase):
self.assertEqual(len(versions), 2)
self.assertEqual(len(versions), instance.nb_versions)
# Final test: we query the version viewset itself
ct = ContentType.objects.get_for_model(instance).id
from django.conf.urls import include, url
from backend_app.config.viewsets import get_viewsets_info
from backend_app.checks import check_viewsets
from rest_framework import routers
from rest_framework.documentation import include_docs_urls
STANDARD_VIEWSETS = get_viewsets_info(requires_testing="smart", is_api_view=False)
API_VIEW_VIEWSETS = get_viewsets_info(requires_testing="smart", is_api_view=True)
check_viewsets(map(lambda v: v.Viewset, STANDARD_VIEWSETS))
# Building the API routing
from .url import validate_url
from .text import validate_text
from .tagged_item_validation import (
__all__ = [
from rest_framework.validators import ValidationError
def missing_field(field):
return ValidationError("{} : this field is required".format(field))
def check_required(config, content):
for field in config:
if config[field]["required"]:
val = content[field]
if type(val) is str:
if len(val) == 0:
raise missing_field(field)
if val is None:
raise missing_field(field)
except KeyError:
raise missing_field(field)
from .photos import PHOTOS_TAG_CONFIG
from .useful_links import USEFULL_LINKS_CONFIG
from rest_framework.validators import ValidationError
def validate_text(config, string):
string = str(string) # might cause error with number ?
validators = config["validators"]
for validator in validators:
validator_content = validators[validator]
if validator == "max_length":
if len(string) > validator_content:
raise ValidationError("Your text is too long !")
raise Exception("Dev, you have implement something here...")
except KeyError:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment