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

Added stats to problem administation #244

Merged
merged 4 commits into from
Nov 11, 2023
Merged
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
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
Loading