Skip to content

Commit

Permalink
Merge pull request #78 from maykinmedia/feature/65-filter-destruction…
Browse files Browse the repository at this point in the history
…-list

[#65] Filter destruction list on assignee
  • Loading branch information
SilviaAmAm authored Jun 6, 2024
2 parents a516452 + 68f6312 commit 5387c37
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
15 changes: 13 additions & 2 deletions backend/src/openarchiefbeheer/destruction/api/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
from django_filters import FilterSet
from django_filters import FilterSet, NumberFilter

from ..models import DestructionListItem
from ..models import DestructionList, DestructionListItem


class DestructionListItemFilterset(FilterSet):
class Meta:
model = DestructionListItem
fields = ("destruction_list",)


class DestructionListFilterset(FilterSet):
assignee = NumberFilter(
field_name="assignee",
help_text="The pk of the user currently assigned to the list.",
)

class Meta:
model = DestructionList
fields = ("assignee",)
4 changes: 3 additions & 1 deletion backend/src/openarchiefbeheer/destruction/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rest_framework.permissions import IsAuthenticated

from ..models import DestructionList, DestructionListItem
from .filtersets import DestructionListItemFilterset
from .filtersets import DestructionListFilterset, DestructionListItemFilterset
from .permissions import CanStartDestructionPermission
from .serializers import (
DestructionListItemSerializer,
Expand Down Expand Up @@ -116,6 +116,8 @@ class DestructionListViewSet(
):
serializer_class = DestructionListSerializer
queryset = DestructionList.objects.all()
filter_backends = (DjangoFilterBackend,)
filterset_class = DestructionListFilterset

def get_permissions(self):
if self.action == "create":
Expand Down
25 changes: 25 additions & 0 deletions backend/src/openarchiefbeheer/destruction/tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,31 @@ def test_partially_update_destruction_list(self):
self.assertEqual(destruction_list.items.all().count(), 2)
self.assertEqual(destruction_list.assignees.all().count(), 2)

def test_destruction_list_filter_on_assignee(self):
user = UserFactory.create()
reviewer1 = UserFactory.create()
reviewer2 = UserFactory.create()
lists = DestructionListFactory.create_batch(3)
lists[0].assignee = reviewer1
lists[1].save()
lists[1].assignee = reviewer2
lists[1].save()
lists[2].assignee = reviewer2
lists[2].save()

self.client.force_authenticate(user=user)
endpoint = furl(reverse("api:destructionlist-list"))
endpoint.args["assignee"] = reviewer2.pk

response = self.client.get(endpoint.url)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.json()), 2)
self.assertEqual(
[destruction_list["pk"] for destruction_list in response.json()].sort(),
[lists[0].pk, lists[1].pk].sort(),
)


class DestructionListItemsViewSetTest(APITestCase):
def test_not_authenticated(self):
Expand Down

0 comments on commit 5387c37

Please sign in to comment.