diff --git a/backend/src/openarchiefbeheer/accounts/api/constants.py b/backend/src/openarchiefbeheer/accounts/api/constants.py new file mode 100644 index 000000000..ab848ddd4 --- /dev/null +++ b/backend/src/openarchiefbeheer/accounts/api/constants.py @@ -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") diff --git a/backend/src/openarchiefbeheer/accounts/api/filtersets.py b/backend/src/openarchiefbeheer/accounts/api/filtersets.py new file mode 100644 index 000000000..26711ba51 --- /dev/null +++ b/backend/src/openarchiefbeheer/accounts/api/filtersets.py @@ -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 diff --git a/backend/src/openarchiefbeheer/accounts/api/views.py b/backend/src/openarchiefbeheer/accounts/api/views.py index 95ca761cd..d4e0a4d66 100644 --- a/backend/src/openarchiefbeheer/accounts/api/views.py +++ b/backend/src/openarchiefbeheer/accounts/api/views.py @@ -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 @@ -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"), diff --git a/backend/src/openarchiefbeheer/api/urls.py b/backend/src/openarchiefbeheer/api/urls.py index a34910a30..24d53af01 100644 --- a/backend/src/openarchiefbeheer/api/urls.py +++ b/backend/src/openarchiefbeheer/api/urls.py @@ -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 ( @@ -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/",