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..8783250 100644 --- a/personal/serializers.py +++ b/personal/serializers.py @@ -165,7 +165,8 @@ def create(self, validated_data): class ProfileShortSerializer(serializers.ModelSerializer): class Meta: model = Profile - fields = ['first_name', 'last_name'] + fields = ['id', 'first_name', 'last_name'] + read_only_fields = ['id'] @ts_interface(context='personal')