From 5494398c1d21937f4b4c8ae7037d16a37ac6533b Mon Sep 17 00:00:00 2001 From: Hironsan Date: Mon, 14 Mar 2022 14:02:45 +0900 Subject: [PATCH] Add MyRole API --- backend/projects/tests/test_member.py | 18 ++++++++++++++++++ backend/projects/urls.py | 3 ++- backend/projects/views/member.py | 13 ++++++++++++- .../domain/models/member/memberRepository.ts | 2 ++ frontend/layouts/project.vue | 3 +-- frontend/layouts/workspace.vue | 3 +-- frontend/middleware/check-admin.js | 2 +- frontend/pages/projects/_id/dataset/index.vue | 4 +--- .../repositories/member/apiMemberRepository.ts | 6 ++++++ .../member/memberApplicationService.ts | 6 +++--- 10 files changed, 47 insertions(+), 13 deletions(-) diff --git a/backend/projects/tests/test_member.py b/backend/projects/tests/test_member.py index 5aa47f6b..b25ef8e1 100644 --- a/backend/projects/tests/test_member.py +++ b/backend/projects/tests/test_member.py @@ -116,6 +116,24 @@ class TestMemberFilter(CRUDMixin): self.assertEqual(len(response.data), 1) +class TestMyRole(CRUDMixin): + def setUp(self): + self.project = prepare_project() + self.url = reverse(viewname="my_role", args=[self.project.item.id]) + + def test_admin(self): + response = self.assert_fetch(self.project.admin, status.HTTP_200_OK) + self.assertEqual(response.data["rolename"], settings.ROLE_PROJECT_ADMIN) + + def test_approver(self): + response = self.assert_fetch(self.project.approver, status.HTTP_200_OK) + self.assertEqual(response.data["rolename"], settings.ROLE_ANNOTATION_APPROVER) + + def test_annotator(self): + response = self.assert_fetch(self.project.annotator, status.HTTP_200_OK) + self.assertEqual(response.data["rolename"], settings.ROLE_ANNOTATOR) + + class TestMemberManager(CRUDMixin): def test_has_role(self): project = prepare_project() diff --git a/backend/projects/urls.py b/backend/projects/urls.py index e514298e..2f67641b 100644 --- a/backend/projects/urls.py +++ b/backend/projects/urls.py @@ -1,12 +1,13 @@ from django.urls import path -from .views.member import MemberDetail, MemberList +from .views.member import MemberDetail, MemberList, MyRole from .views.project import ProjectDetail, ProjectList from .views.tag import TagDetail, TagList urlpatterns = [ path(route="projects", view=ProjectList.as_view(), name="project_list"), path(route="projects/", view=ProjectDetail.as_view(), name="project_detail"), + path(route="projects//my-role", view=MyRole.as_view(), name="my_role"), path(route="projects//tags", view=TagList.as_view(), name="tag_list"), path(route="projects//tags/", view=TagDetail.as_view(), name="tag_detail"), path(route="projects//members", view=MemberList.as_view(), name="member_list"), diff --git a/backend/projects/views/member.py b/backend/projects/views/member.py index 36c70279..905a3985 100644 --- a/backend/projects/views/member.py +++ b/backend/projects/views/member.py @@ -1,4 +1,5 @@ from django.db import IntegrityError +from django.shortcuts import get_object_or_404 from django_filters.rest_framework import DjangoFilterBackend from rest_framework import generics, status from rest_framework.permissions import IsAuthenticated @@ -6,7 +7,7 @@ from rest_framework.response import Response from projects.exceptions import RoleAlreadyAssignedException, RoleConstraintException from projects.models import Member -from projects.permissions import IsProjectAdmin +from projects.permissions import IsProjectAdmin, IsProjectMember from projects.serializers import MemberSerializer @@ -51,3 +52,13 @@ class MemberDetail(generics.RetrieveUpdateAPIView): super().perform_update(serializer) except IntegrityError: raise RoleAlreadyAssignedException + + +class MyRole(generics.RetrieveAPIView): + queryset = Member.objects.all() + serializer_class = MemberSerializer + permission_classes = [IsAuthenticated & IsProjectMember] + + def get_object(self): + kwargs = {"user": self.request.user, "project_id": self.kwargs["project_id"]} + return get_object_or_404(self.queryset, **kwargs) diff --git a/frontend/domain/models/member/memberRepository.ts b/frontend/domain/models/member/memberRepository.ts index 65b870d1..30813116 100644 --- a/frontend/domain/models/member/memberRepository.ts +++ b/frontend/domain/models/member/memberRepository.ts @@ -8,4 +8,6 @@ export interface MemberRepository { update(projectId: string, item: MemberItem): Promise bulkDelete(projectId: string, memberIds: number[]): Promise + + fetchMyRole(projectId: string): Promise } diff --git a/frontend/layouts/project.vue b/frontend/layouts/project.vue index f560c1cd..1b8b4979 100644 --- a/frontend/layouts/project.vue +++ b/frontend/layouts/project.vue @@ -58,11 +58,10 @@ export default { computed: { ...mapGetters('projects', ['getLink', 'currentProject']), - ...mapGetters('auth', ['getUserId']) }, async created() { - this.isProjectAdmin = await this.$services.member.isProjectAdmin(this.$route.params.id, this.getUserId) + this.isProjectAdmin = await this.$services.member.isProjectAdmin(this.$route.params.id) } } diff --git a/frontend/layouts/workspace.vue b/frontend/layouts/workspace.vue index 6b4b7865..6777dd6e 100644 --- a/frontend/layouts/workspace.vue +++ b/frontend/layouts/workspace.vue @@ -46,7 +46,6 @@ export default { computed: { ...mapGetters('projects', ['getLink', 'currentProject']), - ...mapGetters('auth', ['getUserId']) }, watch: { @@ -56,7 +55,7 @@ export default { }, async created() { - this.isProjectAdmin = await this.$services.member.isProjectAdmin(this.$route.params.id, this.getUserId) + this.isProjectAdmin = await this.$services.member.isProjectAdmin(this.$route.params.id) } } diff --git a/frontend/middleware/check-admin.js b/frontend/middleware/check-admin.js index 52f04fea..200137cc 100644 --- a/frontend/middleware/check-admin.js +++ b/frontend/middleware/check-admin.js @@ -7,7 +7,7 @@ export default _.debounce(async function({ app, store, route, redirect }) { redirect('/projects') } const userId = store.getters['auth/getUserId'] - const isProjectAdmin = await app.$services.member.isProjectAdmin(route.params.id, userId) + const isProjectAdmin = await app.$services.member.isProjectAdmin(route.params.id) const projectRoot = app.localePath('/projects/' + route.params.id) const path = route.fullPath.replace(/\/$/g, '') diff --git a/frontend/pages/projects/_id/dataset/index.vue b/frontend/pages/projects/_id/dataset/index.vue index cad72c19..6da3d340 100644 --- a/frontend/pages/projects/_id/dataset/index.vue +++ b/frontend/pages/projects/_id/dataset/index.vue @@ -69,7 +69,6 @@