Skip to content

Commit

Permalink
Merge pull request #244 from ZdruzenieSTROM/ProblemWithSolutionsSeria…
Browse files Browse the repository at this point in the history
…lizer-update

Added stats to problem administation
  • Loading branch information
kovacspe authored Nov 11, 2023
2 parents 68e1743 + a159650 commit 5e1411a
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions competition/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rest_framework import serializers

from competition import models
from competition.models import Problem
from personal.serializers import ProfileShortSerializer, SchoolShortSerializer


Expand Down Expand Up @@ -196,11 +197,58 @@ class ProblemWithSolutionsSerializer(serializers.ModelSerializer):
correction = ProblemCorrectionSerializer(many=False)
series = SeriesSerializer()

histogram = serializers.SerializerMethodField('get_series_histogram')
total_solutions = serializers.SerializerMethodField(
'get_series_num_solutions')
tex_header = serializers.SerializerMethodField('get_tex_header')

class Meta:
model = models.Problem
fields = ['solution_set', 'text', 'order',
'correction', 'series', 'solution_pdf']
read_only_fields = ['text', 'order', 'series']
fields = ['histogram', 'total_solutions', 'solution_set', 'text', 'order',
'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: # pylint: disable=no-member
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')

def get_series_num_solutions(self, obj):
return models.Problem.get_stats(obj).get('num_solutions')


# class ProblemStatsSerializer(serializers.Serializer):
Expand Down

0 comments on commit 5e1411a

Please sign in to comment.