From ef52ee66edb2ab56c528616c2cfc6ace195df4b5 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sun, 12 Nov 2023 14:37:42 +0100 Subject: [PATCH 1/9] Allow edit on competition --- competition/views.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/competition/views.py b/competition/views.py index c45d0d9f..941ec21a 100644 --- a/competition/views.py +++ b/competition/views.py @@ -100,13 +100,17 @@ def generate_result_row( } -class CompetitionViewSet(viewsets.ReadOnlyModelViewSet): +class CompetitionViewSet(viewsets.ModelViewSet): """Naše aktivity""" queryset = Competition.objects.all() serializer_class = CompetitionSerializer lookup_field = 'slug' permission_classes = (CompetitionRestrictedPermission,) + def destroy(self, request, *args, **kwargs): + raise exceptions.PermissionDenied( + 'Zmazanie súťaže je povolené iba v django admin') + class CommentViewSet( mixins.RetrieveModelMixin, From 427aa108bee935837cbd7c6b44cd886516ec0a02 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sun, 12 Nov 2023 14:54:38 +0100 Subject: [PATCH 2/9] Disable delete for Competition --- competition/views.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/competition/views.py b/competition/views.py index 941ec21a..3c3f1a98 100644 --- a/competition/views.py +++ b/competition/views.py @@ -100,17 +100,17 @@ def generate_result_row( } -class CompetitionViewSet(viewsets.ModelViewSet): +class CompetitionViewSet(mixins.CreateModelMixin, + mixins.RetrieveModelMixin, + mixins.UpdateModelMixin, + mixins.ListModelMixin, + viewsets.GenericViewSet): """Naše aktivity""" queryset = Competition.objects.all() serializer_class = CompetitionSerializer lookup_field = 'slug' permission_classes = (CompetitionRestrictedPermission,) - def destroy(self, request, *args, **kwargs): - raise exceptions.PermissionDenied( - 'Zmazanie súťaže je povolené iba v django admin') - class CommentViewSet( mixins.RetrieveModelMixin, From e5a5f1556012c8e87fb03cd0b14c8a5d47b4d293 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sun, 12 Nov 2023 15:04:15 +0100 Subject: [PATCH 3/9] Allow edit for schools --- personal/views.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/personal/views.py b/personal/views.py index cf030313..61dc107c 100644 --- a/personal/views.py +++ b/personal/views.py @@ -1,5 +1,5 @@ from django_filters.rest_framework import DjangoFilterBackend -from rest_framework import status, viewsets +from rest_framework import exceptions, status, viewsets from rest_framework.decorators import action from rest_framework.filters import SearchFilter from rest_framework.permissions import IsAdminUser, IsAuthenticated @@ -26,7 +26,7 @@ class DistrictViewSet(viewsets.ReadOnlyModelViewSet): filterset_fields = ['county', ] -class SchoolViewSet(viewsets.ReadOnlyModelViewSet): +class SchoolViewSet(viewsets.ModelViewSet): """Školy""" queryset = School.objects.all() serializer_class = SchoolSerializer @@ -34,6 +34,15 @@ class SchoolViewSet(viewsets.ReadOnlyModelViewSet): filter_backends = [DjangoFilterBackend, SearchFilter] search_fields = ['name', 'street'] + def destroy(self, request, *args, **kwargs): + """Zmazanie školy""" + instance = self.get_object() + if Profile.objects.filter(school=instance).exists(): + raise exceptions.ValidationError( + detail='Nie je možné zmazať školu, ktorá má priradených užívateľov.') + self.perform_destroy(instance) + return Response(status=status.HTTP_204_NO_CONTENT) + class ProfileViewSet(viewsets.ModelViewSet): """Užívateľské profily""" From f204ec7fac57f5814174ca2c72b04d2af2ed5588 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Mon, 13 Nov 2023 21:41:01 +0100 Subject: [PATCH 4/9] Remove competition create --- competition/views.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/competition/views.py b/competition/views.py index 3c3f1a98..b2fffe1d 100644 --- a/competition/views.py +++ b/competition/views.py @@ -100,8 +100,7 @@ def generate_result_row( } -class CompetitionViewSet(mixins.CreateModelMixin, - mixins.RetrieveModelMixin, +class CompetitionViewSet(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet): From 26f8c61ade99748f2c935675a2fb6b10efab4580 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Mon, 13 Nov 2023 22:29:20 +0100 Subject: [PATCH 5/9] Fix tests --- competition/serializers.py | 2 +- competition/tests.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/competition/serializers.py b/competition/serializers.py index 655d8f9e..ee2fd43b 100644 --- a/competition/serializers.py +++ b/competition/serializers.py @@ -60,7 +60,7 @@ class Meta: @ts_interface(context='competition') class CompetitionSerializer(serializers.ModelSerializer): - competition_type = CompetitionTypeSerializer(many=False) + competition_type = CompetitionTypeSerializer(many=False, read_only=True) upcoming_or_current_event = serializers.SerializerMethodField( 'get_upcoming_or_current') history_events = serializers.SerializerMethodField('get_history_events') diff --git a/competition/tests.py b/competition/tests.py index 023281ff..e3cbbdcd 100644 --- a/competition/tests.py +++ b/competition/tests.py @@ -337,7 +337,19 @@ def test_permission_get(self): def test_permission_update(self): ''' update permission OK''' self.check_permissions(self.URL_PREFIX + '/strom/', 'PUT', - self.ALL_FORBIDDEN, {'start_year': 2020}) + self.ONLY_STROM_OK_RESPONSES, + { + "id": 0, + "name": "STROM", + "slug": "strom", + "start_year": 1976, + "description": "popis", + "rules": "# Pravidlá", + "min_years_until_graduation": 0, + "sites": [ + 0 + ] + }) def test_permission_create(self): ''' create permission OK''' From 890be10be749264b25ce293fc555fb51f2680836 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Fri, 8 Dec 2023 14:18:29 +0100 Subject: [PATCH 6/9] Change lookup field to id and add get_one by slug --- competition/views.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/competition/views.py b/competition/views.py index b2fffe1d..fd88360f 100644 --- a/competition/views.py +++ b/competition/views.py @@ -12,6 +12,7 @@ from rest_framework.decorators import action from rest_framework.exceptions import ValidationError from rest_framework.permissions import IsAdminUser, IsAuthenticated +from rest_framework.request import Request from rest_framework.response import Response from base.utils import mime_type @@ -107,9 +108,15 @@ class CompetitionViewSet(mixins.RetrieveModelMixin, """Naše aktivity""" queryset = Competition.objects.all() serializer_class = CompetitionSerializer - lookup_field = 'slug' permission_classes = (CompetitionRestrictedPermission,) + @action(detail=False, url_path=r'slug/(?P\w+)') + def slug(self, request: Request, slug: str = None) -> Response: + competition: Competition = self.get_queryset().get(slug=slug) + return Response( + CompetitionSerializer(competition, many=False).data + ) + class CommentViewSet( mixins.RetrieveModelMixin, From cfb52584fa9e7f66c8cb653ff260524328ce16a4 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Fri, 8 Dec 2023 21:06:18 +0100 Subject: [PATCH 7/9] Fix test url --- competition/tests.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/competition/tests.py b/competition/tests.py index e3cbbdcd..ef172ec7 100644 --- a/competition/tests.py +++ b/competition/tests.py @@ -316,11 +316,11 @@ def test_get_competition_list(self): def test_get_competition_detail(self): '''detail format OK''' - response = self.client.get(self.URL_PREFIX + '/strom', {}, 'json') + response = self.client.get(self.URL_PREFIX + '/slug/strom', {}, 'json') self.assertEqual(response.status_code, 200) self.competition_assert_format(response.json(), 0) - response = self.client.get(self.URL_PREFIX + '/matik', {}, 'json') + response = self.client.get(self.URL_PREFIX + '/slug/matik', {}, 'json') self.assertEqual(response.status_code, 200) self.competition_assert_format(response.json(), 1) @@ -331,12 +331,12 @@ def test_permission_list(self): def test_permission_get(self): '''retrieve permission OK''' - self.check_permissions(self.URL_PREFIX + '/strom/', + self.check_permissions(self.URL_PREFIX + '/slug/strom/', 'GET', self.PUBLIC_OK_RESPONSES) def test_permission_update(self): ''' update permission OK''' - self.check_permissions(self.URL_PREFIX + '/strom/', 'PUT', + self.check_permissions(self.URL_PREFIX + '/slug/strom/', 'PUT', self.ONLY_STROM_OK_RESPONSES, { "id": 0, From ee281a34e1bf3178ad3bda1ecaab5e91e994d432 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Fri, 8 Dec 2023 21:18:00 +0100 Subject: [PATCH 8/9] Patch on /{id} endpoint instead of /slug/{slug} --- competition/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/competition/tests.py b/competition/tests.py index ef172ec7..4df28c50 100644 --- a/competition/tests.py +++ b/competition/tests.py @@ -336,7 +336,7 @@ def test_permission_get(self): def test_permission_update(self): ''' update permission OK''' - self.check_permissions(self.URL_PREFIX + '/slug/strom/', 'PUT', + self.check_permissions(self.URL_PREFIX + '/0/', 'PUT', self.ONLY_STROM_OK_RESPONSES, { "id": 0, From 1a7912bbc28142829d1462ccdc50f196ef9d8141 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Fri, 8 Dec 2023 21:48:15 +0100 Subject: [PATCH 9/9] empty commit