From 777a0617e498a7a500eb5a807c338872c064af71 Mon Sep 17 00:00:00 2001 From: Michal Masrna Date: Fri, 22 Nov 2024 20:39:37 +0100 Subject: [PATCH 1/2] Rework EventRegistrationSerializer and its dependencies to return ids on all objects and the write alternative to receive only ids. This allows integration with the React Admin. --- competition/serializers.py | 37 ++++++++++++++++++++++++++----------- competition/views.py | 11 ++++++++--- personal/serializers.py | 2 +- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/competition/serializers.py b/competition/serializers.py index 665e15b..9d04d20 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -123,13 +123,19 @@ def get_history_events(self, obj): @ts_interface(context='competition') -class EventRegistrationSerializer(serializers.ModelSerializer): +class GradeSerializer(serializers.ModelSerializer): + class Meta: + model = models.Grade + exclude = ['is_active'] + + +@ts_interface(context='competition') +class EventRegistrationReadSerializer(serializers.ModelSerializer): class Meta: model = models.EventRegistration fields = ['school', 'grade', 'profile', 'verbose_name', 'id', 'event'] school = SchoolShortSerializer(many=False) - grade = serializers.SlugRelatedField( - slug_field='tag', many=False, read_only=True) + grade = GradeSerializer(many=False) profile = ProfileShortSerializer(many=False) verbose_name = serializers.SerializerMethodField('get_verbose_name') @@ -137,6 +143,22 @@ def get_verbose_name(self, obj): return str(obj) +class EventRegistrationWriteSerializer(serializers.ModelSerializer): + class Meta: + model = models.EventRegistration + fields = ['school', 'grade', 'profile', 'id', 'event'] + + id = serializers.ReadOnlyField() + school = serializers.PrimaryKeyRelatedField( + queryset=models.School.objects.all()) + grade = serializers.PrimaryKeyRelatedField( + queryset=models.Grade.objects.all()) + profile = serializers.PrimaryKeyRelatedField( + queryset=models.Profile.objects.all()) + event = serializers.PrimaryKeyRelatedField( + queryset=models.Event.objects.all()) + + @ts_interface(context='competition') class ProblemCorrectionSerializer(serializers.ModelSerializer): corrected_by = serializers.SerializerMethodField('get_corrected_by') @@ -225,7 +247,7 @@ class Meta: @ts_interface(context='competition') class SolutionAdministrationSerializer(serializers.ModelSerializer): - semester_registration = EventRegistrationSerializer(read_only=True) + semester_registration = EventRegistrationReadSerializer(read_only=True) class Meta: model = models.Solution @@ -398,10 +420,3 @@ class LateTagSerializer(serializers.ModelSerializer): class Meta: model = models.LateTag exclude = ['comment'] - - -@ts_interface(context='competition') -class GradeSerializer(serializers.ModelSerializer): - class Meta: - model = models.Grade - exclude = ['is_active'] diff --git a/competition/views.py b/competition/views.py index e5e6fbb..418bea3 100644 --- a/competition/views.py +++ b/competition/views.py @@ -28,7 +28,8 @@ ProblemPermission) from competition.serializers import (CommentSerializer, CompetitionSerializer, CompetitionTypeSerializer, - EventRegistrationSerializer, + EventRegistrationReadSerializer, + EventRegistrationWriteSerializer, EventSerializer, GradeSerializer, LateTagSerializer, ProblemSerializer, ProblemWithSolutionsSerializer, @@ -99,7 +100,7 @@ def generate_result_row( # Indikuje či sa zmenilo poradie od minulej priečky, slúži na delené miesta 'rank_changed': True, # primary key riešiteľovej registrácie do semestra - 'registration': EventRegistrationSerializer(semester_registration).data, + 'registration': EventRegistrationReadSerializer(semester_registration).data, # Súčty bodov po sériách 'subtotal': subtotal, # Celkový súčet za danú entitu @@ -878,10 +879,14 @@ def active(self, request): class EventRegistrationViewSet(viewsets.ModelViewSet): """Registrácie na akcie""" queryset = EventRegistration.objects.all() - serializer_class = EventRegistrationSerializer filterset_fields = ['event', 'profile', ] permission_classes = (CompetitionRestrictedPermission,) + def get_serializer_class(self): + if self.action in ['list', 'retrieve']: + return EventRegistrationReadSerializer + return EventRegistrationWriteSerializer + class PublicationTypeViewSet(viewsets.ReadOnlyModelViewSet): queryset = PublicationType.objects.all() diff --git a/personal/serializers.py b/personal/serializers.py index ec72cb0..b173df9 100644 --- a/personal/serializers.py +++ b/personal/serializers.py @@ -165,7 +165,7 @@ def create(self, validated_data): class ProfileShortSerializer(serializers.ModelSerializer): class Meta: model = Profile - fields = ['first_name', 'last_name'] + fields = ['id', 'first_name', 'last_name'] @ts_interface(context='personal') From 2009cfb346fdef3f8a071c3eddaf014cbcdd5707 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Fri, 22 Nov 2024 20:57:28 +0100 Subject: [PATCH 2/2] Readonly field `id` on ProfileShortSerializer --- personal/serializers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/personal/serializers.py b/personal/serializers.py index b173df9..8783250 100644 --- a/personal/serializers.py +++ b/personal/serializers.py @@ -166,6 +166,7 @@ class ProfileShortSerializer(serializers.ModelSerializer): class Meta: model = Profile fields = ['id', 'first_name', 'last_name'] + read_only_fields = ['id'] @ts_interface(context='personal')