Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ #561 - feat: filter select options on co reviewer assignme… #569

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions backend/src/openarchiefbeheer/destruction/api/serializers.py
svenvandescheur marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def validate(self, attrs: dict) -> dict:
return attrs


class CoReviewerAssignementSerializer(serializers.Serializer):
class CoReviewerAssignmentSerializer(serializers.Serializer):
comment = serializers.CharField(required=True, allow_blank=False)
add = CoReviewerSerializer(many=True)
remove = CoReviewerSerializer(many=True, required=False)
Expand All @@ -114,12 +114,19 @@ def validate(self, attrs):
.assignees.filter(role=ListRole.co_reviewer)
.count()
)
if (
current_number_co_reviewers
+ len(attrs.get("add", []))
- len(attrs.get("remove", []))
> MAX_NUMBER_CO_REVIEWERS
):

# (New) number of co reviewers depends on whether a partial update has been mode or a full update is provided.
number_of_co_reviewers = (
(
current_number_co_reviewers
+ len(attrs.get("add", []))
- len(attrs.get("remove", []))
)
if self.partial
else len(attrs.get("add", []))
)

if number_of_co_reviewers > MAX_NUMBER_CO_REVIEWERS:
raise ValidationError(
_("The maximum number of allowed co-reviewers is %(max_co_reviewers)s.")
% {"max_co_reviewers": MAX_NUMBER_CO_REVIEWERS}
Expand Down
8 changes: 4 additions & 4 deletions backend/src/openarchiefbeheer/destruction/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
)
from .serializers import (
AbortDestructionSerializer,
CoReviewerAssignementSerializer,
CoReviewerAssignmentSerializer,
DestructionListAssigneeReadSerializer,
DestructionListCoReviewSerializer,
DestructionListItemReadSerializer,
Expand Down Expand Up @@ -555,7 +555,7 @@ def get_permissions(self):
description=_(
"Full update of the co-reviewers assigned to a destruction list."
),
request=CoReviewerAssignementSerializer,
request=CoReviewerAssignmentSerializer,
responses={200: DestructionListAssigneeReadSerializer},
),
partial_update=extend_schema(
Expand All @@ -564,7 +564,7 @@ def get_permissions(self):
description=_(
"Partial update of the co-reviewers assigned to a destruction list."
),
request=CoReviewerAssignementSerializer,
request=CoReviewerAssignmentSerializer,
responses={200: DestructionListAssigneeReadSerializer},
),
)
Expand Down Expand Up @@ -605,7 +605,7 @@ def get_serializer_class(self):
case "list":
return DestructionListAssigneeReadSerializer
case "update" | "partial_update" | "create":
return CoReviewerAssignementSerializer
return CoReviewerAssignmentSerializer
case default: # noqa
return DestructionListAssigneeReadSerializer

Expand Down
139 changes: 139 additions & 0 deletions backend/src/openarchiefbeheer/destruction/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
from openarchiefbeheer.emails.models import EmailConfig

from ...zaken.tests.factories import ZaakFactory
from ..api.constants import MAX_NUMBER_CO_REVIEWERS
from ..api.serializers import (
CoReviewerAssignmentSerializer,
DestructionListCoReviewSerializer,
DestructionListReviewSerializer,
DestructionListWriteSerializer,
Expand Down Expand Up @@ -1282,3 +1284,140 @@ def test_create_review(self):
},
message,
)


class CoReviewerAssignmentSerializerTests(TestCase):
def test_validate_max_co_reviewers_invalid_full(self):
destruction_list = DestructionListFactory.create(
status=ListStatus.ready_to_review
)

request = factory.put("/foo")
request.user = UserFactory.create()

serializer = CoReviewerAssignmentSerializer(
instance=destruction_list,
data={
"add": [
{"user": user.pk}
for user in UserFactory.create_batch(
10, post__can_co_review_destruction=True
)
],
"comment": "gh-561",
},
context={
"request": request,
"destruction_list": destruction_list,
},
partial=False,
)

self.assertFalse(serializer.is_valid())
self.assertEqual(
serializer.errors["non_field_errors"][0],
_("The maximum number of allowed co-reviewers is %(max_co_reviewers)s.")
% {"max_co_reviewers": MAX_NUMBER_CO_REVIEWERS},
)

def test_validate_max_co_reviewers_valid_full(self):
destruction_list = DestructionListFactory.create(
status=ListStatus.ready_to_review
)

request = factory.put("/foo")
request.user = UserFactory.create()

serializer = CoReviewerAssignmentSerializer(
instance=destruction_list,
data={
"add": [
{"user": user.pk}
for user in UserFactory.create_batch(
5, post__can_co_review_destruction=True
)
],
"comment": "gh-561",
},
context={
"request": request,
"destruction_list": destruction_list,
},
partial=False,
)

self.assertTrue(serializer.is_valid())

def test_validate_max_co_reviewers_invalid_partial(self):
destruction_list = DestructionListFactory.create(
status=ListStatus.ready_to_review
)
DestructionListAssigneeFactory.create_batch(
5, destruction_list=destruction_list, role=ListRole.co_reviewer
)

request = factory.patch("/foo")
request.user = UserFactory.create()

serializer = CoReviewerAssignmentSerializer(
instance=destruction_list,
data={
"add": [
{
"user": UserFactory.create(
post__can_co_review_destruction=True
).pk
}
],
"comment": "gh-561",
},
context={
"request": request,
"destruction_list": destruction_list,
},
partial=True,
)

self.assertFalse(serializer.is_valid())
self.assertEqual(
serializer.errors["non_field_errors"][0],
_("The maximum number of allowed co-reviewers is %(max_co_reviewers)s.")
% {"max_co_reviewers": MAX_NUMBER_CO_REVIEWERS},
)

def test_validate_max_co_reviewers_valid_partial(self):
destruction_list = DestructionListFactory.create(
status=ListStatus.ready_to_review
)
assignees = DestructionListAssigneeFactory.create_batch(
5, destruction_list=destruction_list, role=ListRole.co_reviewer
)

request = factory.patch("/foo")
request.user = UserFactory.create()

serializer = CoReviewerAssignmentSerializer(
instance=destruction_list,
data={
"add": [
{
"user": UserFactory.create(
post__can_co_review_destruction=True
).pk
}
],
"remove": [
{
"user": assignees[0].user.pk,
}
],
"comment": "gh-561",
},
context={
"request": request,
"destruction_list": destruction_list,
},
partial=True,
)

self.assertTrue(serializer.is_valid())
Loading
Loading