diff --git a/competition/serializers.py b/competition/serializers.py index 031ca1cd..2d2e2076 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -1,6 +1,7 @@ from django.contrib.auth.models import AnonymousUser from django_typomatic import ts_interface from rest_framework import serializers +from rest_framework.request import Request from competition import models from competition.models import Event, Problem, RegistrationLink @@ -138,7 +139,7 @@ class ProblemSerializer(serializers.ModelSerializer): class Meta: model = models.Problem fields = '__all__' - read_only_fields = ['series'] + read_only_fields = ['series', 'submitted', 'num_comments'] submitted = serializers.SerializerMethodField( 'submitted_solution') @@ -170,6 +171,29 @@ def submitted_solution(self, obj): return SolutionSerializer(solution).data return None + def update_image(self, problem: Problem): + request: Request = self.context.get('request') + if request: + if len(request.FILES) > 1: + raise ValueError('Úloha môže obsahovať iba 1 obrázok') + if len(request.FILES) == 1: + for name, file in request.FILES.items(): + if '.' not in name: + raise ValueError('Nepodarilo sa zistiť formát súboru') + extension = name.split('.')[-1] + problem.image.save( + f'problem_image_{problem.pk}.{extension}', file) + + def create(self, validated_data): + problem: Problem = super().create(validated_data) + self.update_image(problem) + return problem + + def update(self, instance: Problem, validated_data): + problem = super().update(instance, validated_data) + self.update_image(problem) + return problem + @ts_interface(context='competition') class CommentSerializer(serializers.ModelSerializer):