1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-22 14:54:22 +00:00

🚧 WIP routing, deliberately left incomplete

This commit is contained in:
Steffo 2021-04-15 18:12:26 +02:00 committed by Stefano Pigozzi
parent 6a72f8de4c
commit e6ee7ce8de
4 changed files with 54 additions and 47 deletions

View file

@ -4,16 +4,19 @@ from rest_framework import permissions
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class CanViewObject(permissions.BasePermission): class CanAdministrateProject(permissions.BasePermission):
def has_object_permission(self, request, view, obj): 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): 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): 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)

View file

@ -54,11 +54,7 @@ class DataFlowSerializer(serializers.ModelSerializer):
] ]
class ProjectExternalSerializer(serializers.ModelSerializer): class ProjectListSerializer(serializers.ModelSerializer):
"""
Serializer for :class:`.models.Project` when accessed from outside.
"""
class Meta: class Meta:
model = models.Project model = models.Project
fields = [ fields = [
@ -76,10 +72,6 @@ class ProjectExternalSerializer(serializers.ModelSerializer):
class ProjectCollaboratorSerializer(serializers.ModelSerializer): class ProjectCollaboratorSerializer(serializers.ModelSerializer):
"""
Serializer for :class:`.models.Project` when accessed as a collaborator.
"""
class Meta: class Meta:
model = models.Project model = models.Project
fields = [ fields = [
@ -117,4 +109,5 @@ class ProjectOwnerSerializer(serializers.ModelSerializer):
] ]
read_only_fields = [ read_only_fields = [
"slug", "slug",
"owner",
] ]

View file

@ -4,13 +4,16 @@ from . import views
router = DefaultRouter() 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("datasources", views.DataSourceViewSet)
router.register("dataflows", views.DataFlowViewSet) router.register("dataflows", views.DataFlowViewSet)
urlpatterns = [ 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)),
] ]

View file

@ -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 . import models, serializers, permissions as custom_permissions
from datetime import datetime from datetime import datetime
from logging import getLogger from logging import getLogger
@ -6,39 +6,47 @@ from logging import getLogger
log = getLogger(__name__) log = getLogger(__name__)
class ProjectExternalViewSet(viewsets.ReadOnlyModelViewSet): class ProjectViewSet(viewsets.GenericViewSet):
"""
Viewset for :class:`.models.Project` instances when viewed by the outside.
"""
queryset = models.Project.objects.all() 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 = [] permission_classes = []
class ProjectContributorViewSet(viewsets.GenericViewSet, class ProjectCreateView(generics.CreateAPIView):
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.
"""
queryset = models.Project.objects.all() queryset = models.Project.objects.all()
serializer_class = serializers.ProjectOwnerSerializer 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): class DataFlowViewSet(viewsets.ModelViewSet):