From cc586bd18cade86bfc2f9a104262581a40adc8b5 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sat, 13 Apr 2024 14:15:45 +0200 Subject: [PATCH] =?UTF-8?q?Pridan=C3=BD=20verbose=5Fname=20do=20serializer?= =?UTF-8?q?ov=20(#362)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- competition/models.py | 13 +++++++++---- competition/serializers.py | 22 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/competition/models.py b/competition/models.py index 9151278..3a5e1ea 100644 --- a/competition/models.py +++ b/competition/models.py @@ -178,10 +178,15 @@ def registered_profiles(self): return Profile.objects.filter(eventregistration_set__event=self.pk) def __str__(self): - if self.semester: + try: return str(self.semester) - - return f'{self.competition.name}, {self.year}. ročník - {self.season_code}' + except Event.semester.RelatedObjectDoesNotExist: + name_components = [f'{self.competition.name}, {self.year}. ročník'] + if self.season_code != 2: + name_components.append(self.season) + if self.additional_name: + name_components.append(self.additional_name) + return ' - '.join(name_components) @property def is_active(self): @@ -725,7 +730,7 @@ def generate_name(self, forced=False): self.save() def __str__(self): - return self.name + return f'{self.event} - {self.name}' def can_user_modify(self, user): return self.event.can_user_modify(user) diff --git a/competition/serializers.py b/competition/serializers.py index addf07d..8e9d643 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -36,10 +36,15 @@ class Meta: @ts_interface(context='competition') class PublicationSerializer(serializers.ModelSerializer): + verbose_name = serializers.SerializerMethodField('get_verbose_name') + class Meta: model = models.Publication fields = '__all__' + def get_verbose_name(self, obj): + return str(obj) + @ts_interface(context='competition') class RegistrationLinkSerializer(serializers.ModelSerializer): @@ -50,6 +55,7 @@ class Meta: @ts_interface(context='competition') class EventSerializer(ModelWithParticipationSerializer): + verbose_name = serializers.SerializerMethodField('get_verbose_name') publication_set = PublicationSerializer(many=True, read_only=True) registration_link = RegistrationLinkSerializer( many=False, @@ -74,6 +80,9 @@ def create(self, validated_data): **validated_data, ) + def get_verbose_name(self, obj): + return str(obj) + @ts_interface(context='competition') class CompetitionTypeSerializer(serializers.ModelSerializer): @@ -154,6 +163,7 @@ class Meta: 'get_submitted') num_comments = serializers.SerializerMethodField( 'get_num_comments') + verbose_name = serializers.SerializerMethodField('get_verbose_name') # correction = ProblemCorrectionSerializer(many=False,) def get_num_comments(self, obj): @@ -180,6 +190,9 @@ def get_submitted(self, obj): return SolutionSerializer(solution).data return None + def get_verbose_name(self, obj): + return str(obj) + @ts_interface(context='competition') class CommentSerializer(serializers.ModelSerializer): @@ -300,14 +313,16 @@ class SeriesWithProblemsSerializer(ModelWithParticipationSerializer): can_submit = serializers.SerializerMethodField('get_can_submit') can_resubmit = serializers.SerializerMethodField('get_can_resubmit') complete = serializers.SerializerMethodField('get_complete') + verbose_name = serializers.SerializerMethodField('get_verbose_name') class Meta: model = models.Series exclude = ['sum_method', 'frozen_results'] - include = ['complete', 'problems', 'can_submit', 'can_resubmit'] + include = ['complete', 'problems', 'can_submit', + 'can_resubmit', 'verbose_name'] read_only_fields = [ 'complete', - 'can_submit', 'can_resubmit'] + 'can_submit', 'can_resubmit', 'verbose_name'] def get_can_submit(self, obj): return obj.can_submit @@ -321,6 +336,9 @@ def get_can_resubmit(self, obj): def get_event(self, obj): return obj.semester + def get_verbose_name(self, obj): + return str(obj) + @ts_interface(context='competition') class SemesterSerializer(serializers.ModelSerializer):