Skip to content

Commit

Permalink
🐛(backend) improve openAPI schema
Browse files Browse the repository at this point in the history
As DRF encode/django-rest-framework#9030 has
been reverted in 3.15.1, the generated openAPI schema is missing default
values, which might be revelant in the documentation.
  • Loading branch information
kernicPanel committed Mar 25, 2024
1 parent f28f95b commit bb827db
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 61 deletions.
117 changes: 95 additions & 22 deletions src/backend/joanie/core/serializers/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ruff: noqa: SLF001
# pylint: disable=too-many-lines
"""Admin serializers for Joanie Core app."""

Expand All @@ -8,7 +9,6 @@
from rest_framework.generics import get_object_or_404

from joanie.core import models
from joanie.core.enums import ALL_LANGUAGES
from joanie.core.serializers.fields import (
ImageDetailField,
ISO8601DurationField,
Expand All @@ -21,6 +21,12 @@ class AdminContractDefinitionSerializer(serializers.ModelSerializer):
"""Serializer for ContractDefinition model."""

title = serializers.CharField()
name = serializers.ChoiceField(
required=False,
choices=models.ContractDefinition._meta.get_field("name").choices,
default=models.ContractDefinition._meta.get_field("name").default,
label=models.ContractDefinition._meta.get_field("name").verbose_name,
)

class Meta:
model = models.ContractDefinition
Expand Down Expand Up @@ -53,7 +59,22 @@ class AdminCourseRunLightSerializer(serializers.ModelSerializer):
"""Serializer for CourseRun model."""

title = serializers.CharField()
languages = serializers.MultipleChoiceField(choices=ALL_LANGUAGES)
languages = serializers.MultipleChoiceField(
choices=models.CourseRun._meta.get_field("languages").choices
)
is_gradable = serializers.BooleanField(
required=False,
# default=False,
# label=_("Is gradable")
default=models.CourseRun._meta.get_field("is_gradable").default,
label=models.CourseRun._meta.get_field("is_gradable").verbose_name,
)
is_listed = serializers.BooleanField(
required=False,
default=models.CourseRun._meta.get_field("is_listed").default,
label=models.CourseRun._meta.get_field("is_listed").verbose_name,
help_text=models.CourseRun._meta.get_field("is_listed").help_text,
)

class Meta:
model = models.CourseRun
Expand Down Expand Up @@ -125,6 +146,11 @@ class AdminOrganizationAccessSerializer(serializers.ModelSerializer):
"""Serializer for OrganizationAccess model."""

user = AdminUserSerializer(read_only=True)
role = serializers.ChoiceField(
required=False,
choices=models.OrganizationAccess._meta.get_field("role").choices,
default=models.OrganizationAccess._meta.get_field("role").default,
)

class Meta:
model = models.OrganizationAccess
Expand Down Expand Up @@ -169,6 +195,17 @@ def validate(self, attrs):
class AdminOrganizationAddressSerializer(serializers.ModelSerializer):
"""Serializer for the Address model for an organization"""

is_main = serializers.BooleanField(
required=False,
default=models.Address._meta.get_field("is_main").default,
label=models.Address._meta.get_field("is_main").verbose_name,
)
is_reusable = serializers.BooleanField(
required=False,
default=models.Address._meta.get_field("is_reusable").default,
label=models.Address._meta.get_field("is_reusable").verbose_name,
)

class Meta:
model = models.Address
fields = [
Expand Down Expand Up @@ -246,6 +283,12 @@ class AdminOrganizationSerializer(serializers.ModelSerializer):
signature = ImageDetailField(required=False)
accesses = AdminOrganizationAccessSerializer(many=True, read_only=True)
addresses = AdminOrganizationAddressSerializer(many=True, read_only=True)
country = serializers.ChoiceField(
required=False,
choices=models.Organization._meta.get_field("country").choices,
default=models.Organization._meta.get_field("country").default,
help_text=models.Organization._meta.get_field("country").help_text,
)

class Meta:
model = models.Organization
Expand Down Expand Up @@ -355,6 +398,22 @@ class AdminOrderGroupSerializer(serializers.ModelSerializer):
Admin Serializer for OrderGroup model
"""

nb_seats = serializers.IntegerField(
required=False,
label=models.OrderGroup._meta.get_field("nb_seats").verbose_name,
help_text=models.OrderGroup._meta.get_field("nb_seats").help_text,
default=models.OrderGroup._meta.get_field("nb_seats").default,
min_value=models.OrderGroup._meta.get_field("nb_seats")
.validators[0]
.limit_value,
max_value=models.OrderGroup._meta.get_field("nb_seats")
.validators[1]
.limit_value,
)
is_active = serializers.BooleanField(
required=False,
default=models.OrderGroup._meta.get_field("is_active").default,
)
nb_available_seats = serializers.SerializerMethodField(read_only=True)

class Meta:
Expand Down Expand Up @@ -522,6 +581,13 @@ class AdminCourseAccessSerializer(serializers.ModelSerializer):
"""Serializer for CourseAccess model."""

user = AdminUserSerializer(read_only=True)
role = serializers.ChoiceField(
required=False,
# choices=models.CourseAccess.ROLE_CHOICES,
# default=enums.INSTRUCTOR,
choices=models.CourseAccess._meta.get_field("role").choices,
default=models.CourseAccess._meta.get_field("role").default,
)

class Meta:
model = models.CourseAccess
Expand Down Expand Up @@ -665,31 +731,14 @@ def update(self, instance, validated_data):
return super().update(instance, validated_data)


class AdminCourseRunSerializer(serializers.ModelSerializer):
class AdminCourseRunSerializer(AdminCourseRunLightSerializer):
"""Serializer for CourseRun model."""

title = serializers.CharField()
course = AdminCourseLightSerializer(read_only=True)
languages = serializers.MultipleChoiceField(choices=ALL_LANGUAGES)

class Meta:
model = models.CourseRun
fields = [
"id",
"course",
"resource_link",
"title",
"is_gradable",
"is_listed",
"languages",
"start",
"end",
"enrollment_start",
"enrollment_end",
"uri",
"state",
]
read_only_fields = ["id", "uri", "state"]
model = AdminCourseRunLightSerializer.Meta.model
fields = AdminCourseRunLightSerializer.Meta.fields + ["course"]

def validate(self, attrs):
"""
Expand Down Expand Up @@ -827,6 +876,30 @@ class AdminProductTargetCourseRelationSerializer(serializers.ModelSerializer):
Serializer for ProductTargetCourseRelation model
"""

is_graded = serializers.BooleanField(
required=False,
default=models.ProductTargetCourseRelation._meta.get_field("is_graded").default,
label=models.ProductTargetCourseRelation._meta.get_field(
"is_graded"
).verbose_name,
help_text=models.ProductTargetCourseRelation._meta.get_field(
"is_graded"
).help_text,
)
position = serializers.IntegerField(
required=False,
default=models.ProductTargetCourseRelation._meta.get_field("position").default,
label=models.ProductTargetCourseRelation._meta.get_field(
"position"
).verbose_name,
min_value=models.ProductTargetCourseRelation._meta.get_field("position")
.validators[0]
.limit_value,
max_value=models.ProductTargetCourseRelation._meta.get_field("position")
.validators[1]
.limit_value,
)

class Meta:
model = models.ProductTargetCourseRelation
fields = ["id", "course", "product", "is_graded", "position", "course_runs"]
Expand Down
19 changes: 18 additions & 1 deletion src/backend/joanie/core/serializers/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ruff: noqa: SLF001
# pylint: disable=too-many-lines
"""Client serializers for Joanie Core app."""

# pylint: disable=too-many-lines
from django.conf import settings
from django.utils.translation import get_language
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -67,6 +68,10 @@ class AddressSerializer(serializers.ModelSerializer):
"""

id = serializers.CharField(read_only=True, required=False)
is_main = serializers.BooleanField(
required=False,
label=models.Address._meta.get_field("is_main").verbose_name,
)

class Meta:
model = models.Address
Expand Down Expand Up @@ -113,6 +118,12 @@ class CourseAccessSerializer(AbilitiesModelSerializer):
source="user",
)

role = serializers.ChoiceField(
required=False,
choices=models.CourseAccess._meta.get_field("role").choices,
default=models.CourseAccess._meta.get_field("role").default,
)

class Meta:
model = models.CourseAccess
fields = ["id", "role", "user_id"]
Expand Down Expand Up @@ -222,6 +233,12 @@ class OrganizationAccessSerializer(AbilitiesModelSerializer):
source="user",
)

role = serializers.ChoiceField(
required=False,
choices=models.OrganizationAccess._meta.get_field("role").choices,
default=models.OrganizationAccess._meta.get_field("role").default,
)

class Meta:
model = models.OrganizationAccess
fields = ["id", "role", "user_id"]
Expand Down
5 changes: 5 additions & 0 deletions src/backend/joanie/payment/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ruff: noqa: SLF001
"""Serializers for api."""

from rest_framework import serializers
Expand All @@ -11,6 +12,10 @@ class CreditCardSerializer(serializers.ModelSerializer):
"""

id = serializers.CharField(read_only=True, required=False)
is_main = serializers.BooleanField(
required=False,
label=models.CreditCard._meta.get_field("is_main").verbose_name,
)

class Meta:
model = models.CreditCard
Expand Down
Loading

0 comments on commit bb827db

Please sign in to comment.