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 ...)"""