Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework EventRegistrationSerializer and its dependencies to return ids… #426

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions competition/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,42 @@ 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')

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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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']
11 changes: 8 additions & 3 deletions competition/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
ProblemPermission)
from competition.serializers import (CommentSerializer, CompetitionSerializer,
CompetitionTypeSerializer,
EventRegistrationSerializer,
EventRegistrationReadSerializer,
EventRegistrationWriteSerializer,
EventSerializer, GradeSerializer,
LateTagSerializer, ProblemSerializer,
ProblemWithSolutionsSerializer,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion personal/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down