diff --git a/sophon/core/permissions.py b/sophon/core/permissions.py index 65aa273..b4e7670 100644 --- a/sophon/core/permissions.py +++ b/sophon/core/permissions.py @@ -4,16 +4,19 @@ from rest_framework import permissions log = logging.getLogger(__name__) -class CanViewObject(permissions.BasePermission): +class CanAdministrateProject(permissions.BasePermission): def has_object_permission(self, request, view, obj): - return obj.can_be_viewed_by(request.user) + project = obj.get_project() + return project.can_be_administrated_by(request.user) -class CanEditObject(permissions.BasePermission): +class CanEditProject(permissions.BasePermission): def has_object_permission(self, request, view, obj): - return obj.can_be_edited_by(request.user) + project = obj.get_project() + return project.can_be_edited_by(request.user) -class CanAdministrateObject(permissions.BasePermission): +class CanViewProject(permissions.BasePermission): def has_object_permission(self, request, view, obj): - return obj.can_be_administrated_by(request.user) + project = obj.get_project() + return project.can_be_viewed_by(request.user) diff --git a/sophon/core/serializers.py b/sophon/core/serializers.py index 7b6e6ff..5afac33 100644 --- a/sophon/core/serializers.py +++ b/sophon/core/serializers.py @@ -54,11 +54,7 @@ class DataFlowSerializer(serializers.ModelSerializer): ] -class ProjectExternalSerializer(serializers.ModelSerializer): - """ - Serializer for :class:`.models.Project` when accessed from outside. - """ - +class ProjectListSerializer(serializers.ModelSerializer): class Meta: model = models.Project fields = [ @@ -76,10 +72,6 @@ class ProjectExternalSerializer(serializers.ModelSerializer): class ProjectCollaboratorSerializer(serializers.ModelSerializer): - """ - Serializer for :class:`.models.Project` when accessed as a collaborator. - """ - class Meta: model = models.Project fields = [ @@ -117,4 +109,5 @@ class ProjectOwnerSerializer(serializers.ModelSerializer): ] read_only_fields = [ "slug", + "owner", ] diff --git a/sophon/core/urls.py b/sophon/core/urls.py index 8508606..b6da8b0 100644 --- a/sophon/core/urls.py +++ b/sophon/core/urls.py @@ -4,13 +4,16 @@ from . import views router = DefaultRouter() -router.register("projects/as_external", views.ProjectExternalViewSet) -router.register("projects/as_contributor", views.ProjectContributorViewSet) -router.register("projects/as_owner", views.ProjectOwnerViewSet) router.register("datasources", views.DataSourceViewSet) router.register("dataflows", views.DataFlowViewSet) urlpatterns = [ - path("", include(router.urls)) + path("projects/", views.ProjectListView), + path("projects/", views.ProjectCreateView), + path("projects/", views.ProjectRetrieveView), + path("projects/", views.ProjectUpdateCollaboratorView), + path("projects/", views.ProjectUpdateOwnerView), + path("projects/", views.ProjectDestroyView), + path("", include(router.urls)), ] diff --git a/sophon/core/views.py b/sophon/core/views.py index 3f30f86..615d7cb 100644 --- a/sophon/core/views.py +++ b/sophon/core/views.py @@ -1,4 +1,4 @@ -from rest_framework import viewsets, decorators, response, permissions, mixins +from rest_framework import viewsets, decorators, response, permissions, mixins, generics from . import models, serializers, permissions as custom_permissions from datetime import datetime from logging import getLogger @@ -6,39 +6,47 @@ from logging import getLogger log = getLogger(__name__) -class ProjectExternalViewSet(viewsets.ReadOnlyModelViewSet): - """ - Viewset for :class:`.models.Project` instances when viewed by the outside. - """ - +class ProjectViewSet(viewsets.GenericViewSet): queryset = models.Project.objects.all() - serializer_class = serializers.ProjectExternalSerializer + + def get_serializer_class(self): + if self.action == "" + + +class ProjectListView(generics.ListAPIView): + queryset = models.Project.objects.all() + serializer_class = serializers.ProjectListSerializer permission_classes = [] -class ProjectContributorViewSet(viewsets.GenericViewSet, - mixins.RetrieveModelMixin, - mixins.UpdateModelMixin): - """ - Viewset for :class:`.models.Project` instances when viewed by a project contributor. - """ - - queryset = models.Project.objects.all() - serializer_class = serializers.ProjectCollaboratorSerializer - permission_classes = [custom_permissions.CanEditObject] - - -class ProjectOwnerViewSet(viewsets.GenericViewSet, - mixins.RetrieveModelMixin, - mixins.UpdateModelMixin, - mixins.DestroyModelMixin): - """ - Viewset for :class:`.models.Project` instances when viewed by a project owner. - """ - +class ProjectCreateView(generics.CreateAPIView): queryset = models.Project.objects.all() serializer_class = serializers.ProjectOwnerSerializer - permission_classes = [custom_permissions.CanAdministrateObject] + permission_classes = [permissions.IsAuthenticated] + + +class ProjectRetrieveView(generics.RetrieveAPIView): + queryset = models.Project.objects.all() + serializer_class = serializers.ProjectCollaboratorSerializer + permission_classes = [custom_permissions.CanViewProject] + + +class ProjectUpdateCollaboratorView(generics.UpdateAPIView): + queryset = models.Project.objects.all() + serializer_class = serializers.ProjectCollaboratorSerializer + permission_classes = [custom_permissions.CanEditProject] + + +class ProjectUpdateOwnerView(generics.DestroyAPIView): + queryset = models.Project.objects.all() + serializer_class = serializers.ProjectOwnerSerializer + permission_classes = [custom_permissions.CanAdministrateProject] + + +class ProjectDestroyView(generics.DestroyAPIView): + queryset = models.Project.objects.all() + serializer_class = serializers.ProjectCollaboratorSerializer + permission_classes = [custom_permissions.CanAdministrateProject] class DataFlowViewSet(viewsets.ModelViewSet):