From 76f2c6c953b8979cf02bd4cd8b963ea163f7824d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Mih=C3=A1lik?= Date: Sat, 9 Dec 2023 22:03:00 +0100 Subject: [PATCH] Use native FileField --- competition/models.py | 10 +++++++--- competition/serializers.py | 23 ----------------------- 2 files changed, 7 insertions(+), 26 deletions(-) diff --git a/competition/models.py b/competition/models.py index 3865111d..c9b57924 100644 --- a/competition/models.py +++ b/competition/models.py @@ -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, diff --git a/competition/serializers.py b/competition/serializers.py index 2d2e2076..97f02db4 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -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):