Skip to content

Commit

Permalink
Use native FileField
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Mihálik committed Dec 9, 2023
1 parent 48683f1 commit 76f2c6c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 26 deletions.
10 changes: 7 additions & 3 deletions competition/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,14 @@ class Meta:
Series, verbose_name='úloha zaradená do série',
related_name='problems',
on_delete=models.CASCADE,)
image = RestrictedFileField(
content_types=['image/svg+xml', 'image/png'],

image = models.ImageField(
upload_to='problem_images/',
verbose_name='Obrázok k úlohe', null=True, blank=True)
verbose_name='Obrázok k úlohe',
null=True,
blank=True,
)

solution_pdf = RestrictedFileField(
content_types=['application/pdf'],
verbose_name='Vzorové riešenie', null=True, blank=True,
Expand Down
23 changes: 0 additions & 23 deletions competition/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,29 +171,6 @@ 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 76f2c6c

Please sign in to comment.