From 5dd3f1b1d0d12be537098ac80175fe98ea3d84ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjam=C3=ADn=20Mravec?= Date: Sat, 11 Nov 2023 17:38:11 +0100 Subject: [PATCH 1/4] Added stats to problem administation --- competition/serializers.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/competition/serializers.py b/competition/serializers.py index ecba2979..516ee7fb 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -195,12 +195,21 @@ class ProblemWithSolutionsSerializer(serializers.ModelSerializer): solution_set = SolutionAdministrationSerializer(many=True) correction = ProblemCorrectionSerializer(many=False) series = SeriesSerializer() + + histogram = serializers.SerializerMethodField('get_series_histogram') + total_solutions = serializers.SerializerMethodField('get_series_num_solutions') class Meta: model = models.Problem - fields = ['solution_set', 'text', 'order', + fields = ['histogram', 'total_solutions', 'solution_set', 'text', 'order', 'correction', 'series', 'solution_pdf'] - read_only_fields = ['text', 'order', 'series'] + read_only_fields = ['histogram', 'num_solutions', 'text', 'order', 'series'] + + def get_series_histogram(self, obj): + return models.Problem.get_stats(obj).get('histogram') + + def get_series_num_solutions(self, obj): + return models.Problem.get_stats(obj).get('num_solutions') # class ProblemStatsSerializer(serializers.Serializer): From 0f1b530c89862d2c00e61273bd0c731a144034df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjam=C3=ADn=20Mravec?= Date: Sat, 11 Nov 2023 17:40:07 +0100 Subject: [PATCH 2/4] Added stats to problem administation --- competition/serializers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/competition/serializers.py b/competition/serializers.py index 516ee7fb..3cc12a7c 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -195,7 +195,7 @@ class ProblemWithSolutionsSerializer(serializers.ModelSerializer): solution_set = SolutionAdministrationSerializer(many=True) correction = ProblemCorrectionSerializer(many=False) series = SeriesSerializer() - + histogram = serializers.SerializerMethodField('get_series_histogram') total_solutions = serializers.SerializerMethodField('get_series_num_solutions') @@ -207,7 +207,7 @@ class Meta: def get_series_histogram(self, obj): return models.Problem.get_stats(obj).get('histogram') - + def get_series_num_solutions(self, obj): return models.Problem.get_stats(obj).get('num_solutions') From d84d370abb68a018d87ca3e879e0e5ad41a548e6 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sat, 11 Nov 2023 20:32:38 +0100 Subject: [PATCH 3/4] Add: Add latex header --- competition/serializers.py | 45 +++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/competition/serializers.py b/competition/serializers.py index 3cc12a7c..48fc38dd 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -3,6 +3,7 @@ from rest_framework import serializers from competition import models +from competition.models import Problem from personal.serializers import ProfileShortSerializer, SchoolShortSerializer @@ -197,13 +198,51 @@ class ProblemWithSolutionsSerializer(serializers.ModelSerializer): series = SeriesSerializer() histogram = serializers.SerializerMethodField('get_series_histogram') - total_solutions = serializers.SerializerMethodField('get_series_num_solutions') + total_solutions = serializers.SerializerMethodField( + 'get_series_num_solutions') + tex_header = serializers.SerializerMethodField('get_tex_header') class Meta: model = models.Problem fields = ['histogram', 'total_solutions', 'solution_set', 'text', 'order', - 'correction', 'series', 'solution_pdf'] - read_only_fields = ['histogram', 'num_solutions', 'text', 'order', 'series'] + 'correction', 'series', 'solution_pdf', + 'tex_header'] + read_only_fields = ['histogram', 'tex_header' + 'num_solutions', 'text', 'order', 'series'] + + def format_list_of_names(self, names: list[str]) -> str: + if names is None or len(names) == 0: + return '' + if len(names) == 1: + return names[0] + return ', '.join(names[:-1])+' a '+names[-1] + + def format_histogram(self, histogram: list[dict[str, int]]) -> str: + return ''.join([f'({item["score"]},{item["count"]})' for item in histogram]) + + def get_tex_header(self, obj: Problem) -> str: + """Generuje tex hlavicku vzoraku do casaku""" + try: + corrected_by = [user.get_full_name() + for user in obj.correction.corrected_by.all()] + corrected_suffix = 'i' if len(corrected_by) > 1 else '' + + best_solutions = [user.get_full_name() + for user in obj.correction.corrected_by.all()] + best_solution_suffix = 'e' if len(best_solutions) > 1 else 'a' + except Problem.correction.RelatedObjectDoesNotExist: + corrected_by = None + corrected_suffix = '' + best_solutions = None + best_solution_suffix = 'a' + num_solutions = self.get_series_num_solutions(obj) + histogram = self.get_series_histogram(obj) + return f'\\vzorak{{{corrected_suffix}}}'\ + f'{{{self.format_list_of_names(corrected_by)}}}'\ + f'{{{num_solutions}}}'\ + f'{{{best_solution_suffix}}}'\ + f'{{{self.format_list_of_names(best_solutions)}}}'\ + f'{{{self.format_histogram(histogram)}}}' def get_series_histogram(self, obj): return models.Problem.get_stats(obj).get('histogram') From a159650e2d98f4e8c4699b47dc8514f5e502f8a0 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sat, 11 Nov 2023 20:40:35 +0100 Subject: [PATCH 4/4] lint --- competition/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/competition/serializers.py b/competition/serializers.py index 48fc38dd..655d8f9e 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -230,7 +230,7 @@ def get_tex_header(self, obj: Problem) -> str: best_solutions = [user.get_full_name() for user in obj.correction.corrected_by.all()] best_solution_suffix = 'e' if len(best_solutions) > 1 else 'a' - except Problem.correction.RelatedObjectDoesNotExist: + except Problem.correction.RelatedObjectDoesNotExist: # pylint: disable=no-member corrected_by = None corrected_suffix = '' best_solutions = None