From 384b0e1d10d6747a4f300b6d9638259b2b4fdfe7 Mon Sep 17 00:00:00 2001 From: Jon Date: Tue, 24 Dec 2024 15:35:30 +0930 Subject: [PATCH] feat(api): distinguish between read-only and authenticateed user permissions ref: #451 #452 --- app/api/viewsets/common.py | 23 ++++++++++++++++++----- app/app/viewsets/base/content_type.py | 4 ++-- app/app/viewsets/base/permisson.py | 4 ++-- app/app/viewsets/base/user.py | 4 ++-- app/core/viewsets/celery_log.py | 4 ++-- app/itim/viewsets/service_device.py | 4 ++-- 6 files changed, 28 insertions(+), 15 deletions(-) diff --git a/app/api/viewsets/common.py b/app/api/viewsets/common.py index c549f59eb..793bc08b6 100644 --- a/app/api/viewsets/common.py +++ b/app/api/viewsets/common.py @@ -557,16 +557,11 @@ def update(self, request, *args, **kwargs): - class ReadOnlyModelViewSet( viewsets.ReadOnlyModelViewSet, ModelViewSetBase ): - permission_classes = [ - IsAuthenticated, - ] - def retrieve(self, request, *args, **kwargs): """Sainty override @@ -639,3 +634,21 @@ def list(self, request, *args, **kwargs): ) return response + + + +class AuthUserReadOnlyModelViewSet( + ReadOnlyModelViewSet +): + """Authenticated User Read-Only Viewset + + Use this class if the model only requires that the user be authenticated + to obtain view permission. + + Args: + ReadOnlyModelViewSet (class): Read-Only base class + """ + + permission_classes = [ + IsAuthenticated, + ] diff --git a/app/app/viewsets/base/content_type.py b/app/app/viewsets/base/content_type.py index 2e66aa743..abda7e578 100644 --- a/app/app/viewsets/base/content_type.py +++ b/app/app/viewsets/base/content_type.py @@ -1,6 +1,6 @@ from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiResponse -from api.viewsets.common import ReadOnlyModelViewSet +from api.viewsets.common import AuthUserReadOnlyModelViewSet from app.serializers.content_type import ( ContentType, @@ -26,7 +26,7 @@ ), ) class ViewSet( - ReadOnlyModelViewSet + AuthUserReadOnlyModelViewSet ): diff --git a/app/app/viewsets/base/permisson.py b/app/app/viewsets/base/permisson.py index c10728007..1bc5d245e 100644 --- a/app/app/viewsets/base/permisson.py +++ b/app/app/viewsets/base/permisson.py @@ -1,6 +1,6 @@ from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiResponse -from api.viewsets.common import ReadOnlyModelViewSet +from api.viewsets.common import AuthUserReadOnlyModelViewSet from app.serializers.permission import ( Permission, @@ -26,7 +26,7 @@ ), ) class ViewSet( - ReadOnlyModelViewSet + AuthUserReadOnlyModelViewSet ): diff --git a/app/app/viewsets/base/user.py b/app/app/viewsets/base/user.py index 9a796a21c..292d05bc9 100644 --- a/app/app/viewsets/base/user.py +++ b/app/app/viewsets/base/user.py @@ -1,6 +1,6 @@ from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiResponse -from api.viewsets.common import ReadOnlyModelViewSet +from api.viewsets.common import AuthUserReadOnlyModelViewSet from app.serializers.user import ( User, @@ -28,7 +28,7 @@ ), ) class ViewSet( - ReadOnlyModelViewSet + AuthUserReadOnlyModelViewSet ): diff --git a/app/core/viewsets/celery_log.py b/app/core/viewsets/celery_log.py index 647e9e3c5..b9d4319e1 100644 --- a/app/core/viewsets/celery_log.py +++ b/app/core/viewsets/celery_log.py @@ -6,7 +6,7 @@ TaskResultViewSerializer ) -from api.viewsets.common import ReadOnlyModelViewSet +from api.viewsets.common import AuthUserReadOnlyModelViewSet @@ -29,7 +29,7 @@ } ), ) -class ViewSet(ReadOnlyModelViewSet): +class ViewSet(AuthUserReadOnlyModelViewSet): filterset_fields = [ 'periodic_task_name', diff --git a/app/itim/viewsets/service_device.py b/app/itim/viewsets/service_device.py index bfa7306f6..4d4cff864 100644 --- a/app/itim/viewsets/service_device.py +++ b/app/itim/viewsets/service_device.py @@ -1,6 +1,6 @@ from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiResponse -from api.viewsets.common import ModelViewSet +from api.viewsets.common import ReadOnlyModelViewSet from itim.serializers.service import ( Service, @@ -18,7 +18,7 @@ partial_update=extend_schema(exclude=True), destroy=extend_schema(exclude=True) ) -class ViewSet(ModelViewSet): +class ViewSet(ReadOnlyModelViewSet): filterset_fields = [ 'cluster',