Skip to content

Commit

Permalink
Nahravanie obrazku k ulohu
Browse files Browse the repository at this point in the history
  • Loading branch information
kovacspe committed Dec 9, 2023
1 parent 2fd1ac9 commit 48683f1
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion competition/serializers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 48683f1

Please sign in to comment.