Skip to content

Commit

Permalink
♻️ [#574] Refactor user endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
SilviaAmAm committed Dec 27, 2024
1 parent 60b1664 commit f36a050
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 86 deletions.
9 changes: 9 additions & 0 deletions backend/src/openarchiefbeheer/accounts/api/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db import models
from django.utils.translation import gettext_lazy as _


class RoleFilterChoices(models.TextChoices):
record_manager = "record_manager", _("Record manager")
main_reviewer = "main_reviewer", _("Main reviewer")
co_reviewer = "co_reviewer", _("Co-reviewer")
archivist = "archivist", _("Archivist")
34 changes: 34 additions & 0 deletions backend/src/openarchiefbeheer/accounts/api/filtersets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.db.models import QuerySet

from django_filters import ChoiceFilter, FilterSet

from ..models import User
from .constants import RoleFilterChoices


class UsersFilterset(FilterSet):
role = ChoiceFilter(
field_name="role",
method="filter_role",
help_text="Filter on the user's role.",
choices=RoleFilterChoices.choices,
)

class Meta:
model = User
fields = ("role",)

def filter_role(
self, queryset: QuerySet[User], name: str, value: str
) -> QuerySet[User]:
match value:
case RoleFilterChoices.record_manager:
return User.objects.record_managers()
case RoleFilterChoices.main_reviewer:
return User.objects.main_reviewers()
case RoleFilterChoices.co_reviewer:
return User.objects.co_reviewers()
case RoleFilterChoices.archivist:
return User.objects.archivists()
case _:
return queryset
75 changes: 5 additions & 70 deletions backend/src/openarchiefbeheer/accounts/api/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from django.db.models import QuerySet
from django.utils.translation import gettext_lazy as _

from django_filters.rest_framework import DjangoFilterBackend
from drf_spectacular.utils import extend_schema
from rest_framework.generics import ListAPIView, RetrieveAPIView

from openarchiefbeheer.accounts.models import User

from ..models import User
from .filtersets import UsersFilterset
from .serializers import UserSerializer


Expand All @@ -19,79 +20,13 @@
)
class UsersView(ListAPIView):
serializer_class = UserSerializer
filter_backends = (DjangoFilterBackend,)
filterset_class = UsersFilterset

def get_queryset(self) -> QuerySet[User]:
return User.objects.all()


@extend_schema(
tags=["Users"],
summary=_("Record managers list"),
description=_(
"List all the users that have the permission to create destruction lists."
),
responses={
200: UserSerializer(many=True),
},
)
class RecordManagersView(ListAPIView):
serializer_class = UserSerializer

def get_queryset(self) -> QuerySet[User]:
return User.objects.record_managers()


@extend_schema(
tags=["Users"],
summary=_("Main reviewers list"),
description=_(
"List all the users that have the permission to review draft destruction lists."
),
responses={
200: UserSerializer(many=True),
},
)
class MainReviewersView(ListAPIView):
serializer_class = UserSerializer

def get_queryset(self) -> QuerySet[User]:
return User.objects.main_reviewers()


@extend_schema(
tags=["Users"],
summary=_("Co-reviewers list"),
description=_(
"List all the users that have the permission to co-review draft destruction lists."
),
responses={
200: UserSerializer(many=True),
},
)
class CoReviewersView(ListAPIView):
serializer_class = UserSerializer

def get_queryset(self) -> QuerySet[User]:
return User.objects.co_reviewers()


@extend_schema(
tags=["Users"],
summary=_("Archivists list"),
description=_(
"List all the users that have the permission to review final destruction lists."
),
responses={
200: UserSerializer(many=True),
},
)
class ArchivistsView(ListAPIView):
serializer_class = UserSerializer

def get_queryset(self) -> QuerySet[User]:
return User.objects.archivists()


@extend_schema(
tags=["Users"],
summary=_("Who Am I"),
Expand Down
17 changes: 1 addition & 16 deletions backend/src/openarchiefbeheer/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@
)
from rest_framework import routers

from openarchiefbeheer.accounts.api.views import (
ArchivistsView,
CoReviewersView,
MainReviewersView,
RecordManagersView,
UsersView,
WhoAmIView,
)
from openarchiefbeheer.accounts.api.views import UsersView, WhoAmIView
from openarchiefbeheer.config.api.views import ArchiveConfigView, OIDCInfoView
from openarchiefbeheer.destruction.api.views import ListStatusesListView
from openarchiefbeheer.destruction.api.viewsets import (
Expand Down Expand Up @@ -121,14 +114,6 @@
UsersView.as_view(),
name="users",
),
path(
"record-managers/",
RecordManagersView.as_view(),
name="record-managers",
),
path("reviewers/", MainReviewersView.as_view(), name="reviewers"),
path("archivists/", ArchivistsView.as_view(), name="archivists"),
path("co-reviewers/", CoReviewersView.as_view(), name="co-reviewers"),
path("whoami/", WhoAmIView.as_view(), name="whoami"),
path(
"destruction-list-statuses/",
Expand Down

0 comments on commit f36a050

Please sign in to comment.