1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-22 23:04:21 +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 f495722c6e
commit 507e3e6d53
4 changed files with 54 additions and 47 deletions

View file

@ -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)

View file

@ -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",
]

View file

@ -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)),
]

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 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):