From 567bef646814db79c445d791ae5e4a153c5041e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Mih=C3=A1lik?= Date: Sun, 15 Dec 2024 12:20:07 +0100 Subject: [PATCH] Do not require publication file on patch request (#485) --- ...r_series_options_alter_publication_file.py | 29 +++++++++++++++++++ competition/models.py | 4 ++- competition/views.py | 15 ++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 competition/migrations/0003_alter_series_options_alter_publication_file.py diff --git a/competition/migrations/0003_alter_series_options_alter_publication_file.py b/competition/migrations/0003_alter_series_options_alter_publication_file.py new file mode 100644 index 0000000..a1ccb59 --- /dev/null +++ b/competition/migrations/0003_alter_series_options_alter_publication_file.py @@ -0,0 +1,29 @@ +# Generated by Django 4.2.17 on 2024-12-15 11:12 + +import base.models +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("competition", "0002_initial"), + ] + + operations = [ + migrations.AlterModelOptions( + name="series", + options={ + "ordering": ["semester", "order"], + "verbose_name": "séria", + "verbose_name_plural": "série", + }, + ), + migrations.AlterField( + model_name="publication", + name="file", + field=base.models.RestrictedFileField( + blank=True, upload_to="publications/%Y/", verbose_name="súbor" + ), + ), + ] diff --git a/competition/models.py b/competition/models.py index a195784..ab8b74e 100644 --- a/competition/models.py +++ b/competition/models.py @@ -738,7 +738,9 @@ class Meta: file = RestrictedFileField( upload_to='publications/%Y/', content_types=['application/pdf', 'application/zip'], - verbose_name='súbor') + verbose_name='súbor', + blank=True, + ) order = models.PositiveSmallIntegerField( verbose_name='poradie', null=True, blank=True) diff --git a/competition/views.py b/competition/views.py index 542c13d..fb0d52f 100644 --- a/competition/views.py +++ b/competition/views.py @@ -957,12 +957,27 @@ def perform_create(self, serializer): Vola sa pri vytvarani objektu, checkuju sa tu permissions, ci user vie vytvorit publication v danom evente ''' + self._ensure_file_attached(serializer) + if Publication.can_user_create(self.request.user, serializer.validated_data): serializer.save() else: raise exceptions.PermissionDenied( 'Nedostatočné práva na vytvorenie tohoto objektu') + def perform_update(self, serializer): + if not serializer.partial: + self._ensure_file_attached(serializer) + + return super().perform_update(serializer) + + @staticmethod + def _ensure_file_attached(serializer: PublicationSerializer): + if 'file' not in serializer.validated_data: + raise exceptions.ValidationError( + 'Publikácia musí mať pripojený súbor' + ) + class GradeViewSet(viewsets.ReadOnlyModelViewSet): """Ročníky riešiteľov (Z9,S1 ...)"""