Skip to content

Commit

Permalink
Merge pull request #1332 from BLSQ/IA-2948-change-request-status-filter
Browse files Browse the repository at this point in the history
IA-2948: Change request: allow to filter on multiple status
  • Loading branch information
beygorghor authored May 30, 2024
2 parents f8a4c91 + 050bf2b commit 75997c5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
20 changes: 13 additions & 7 deletions iaso/api/org_unit_change_requests/filters.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import django_filters
from rest_framework.exceptions import ValidationError

from django.conf import settings
from django.contrib.gis.db.models import PointField
from django.contrib.gis.geos import GEOSGeometry
from django.db.models import Q
from django.db.models.functions import Cast
from django.db.models.query import QuerySet
from django.utils.translation import gettext_lazy as _
from django.contrib.gis.geos import GEOSGeometry
from django.db.models.functions import Cast
from django.contrib.gis.db.models import PointField
from rest_framework.exceptions import ValidationError

from iaso.models import OrgUnitChangeRequest, OrgUnit
from iaso.models import OrgUnit, OrgUnitChangeRequest


class MobileOrgUnitChangeRequestListFilter(django_filters.rest_framework.FilterSet):
Expand All @@ -32,6 +31,7 @@ class OrgUnitChangeRequestListFilter(django_filters.rest_framework.FilterSet):
users = django_filters.CharFilter(method="filter_users", label=_("Users IDs (comma-separated)"))
user_roles = django_filters.CharFilter(method="filter_user_roles", label=_("User roles IDs (comma-separated)"))
with_location = django_filters.CharFilter(method="filter_with_location", label=_("With or without location"))
status = django_filters.CharFilter(method="filter_status", label=_("Status (comma-separated)"))

@staticmethod
def parse_comma_separated_numeric_values(value: str, field_name: str) -> list:
Expand All @@ -51,7 +51,7 @@ def __init__(self, *args, **kwargs):

class Meta:
model = OrgUnitChangeRequest
fields = ["status"]
fields = []

def filter_parent_id(self, queryset: QuerySet, _, value: str) -> QuerySet:
try:
Expand Down Expand Up @@ -89,6 +89,12 @@ def filter_user_roles(self, queryset: QuerySet, name: str, value: str) -> QueryS
| Q(updated_by__iaso_profile__user_roles__id__in=users_roles_ids)
)

def filter_status(self, queryset: QuerySet, name: str, value: str) -> QuerySet:
if value:
statuses = value.split(",")
queryset = queryset.filter(status__in=statuses)
return queryset

def filter_with_location(self, queryset: QuerySet, name: str, value: str) -> QuerySet:
"""
`value` is intended to be boolean string "true" or "false".
Expand Down
27 changes: 25 additions & 2 deletions iaso/tests/api/org_unit_change_requests/test_filters.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import datetime

from django.contrib.auth.models import Group

from iaso.test import APITestCase
from django.contrib.gis.geos import Point

from iaso import models as m
from iaso.test import APITestCase


class FilterOrgUnitChangeRequestAPITestCase(APITestCase):
Expand Down Expand Up @@ -228,3 +228,26 @@ def test_filter_on_with_location(self):
self.assertJSONResponse(response, 200)
self.assertEqual(len(response.data["results"]), 1)
self.assertIn(change_request_without_location.id, [change["id"] for change in response.data["results"]])

def test_filter_by_multiple_statuses(self):
change_request_new = m.OrgUnitChangeRequest.objects.create(
org_unit=self.org_unit, new_name="New Request", status=m.OrgUnitChangeRequest.Statuses.NEW
)
change_request_rejected = m.OrgUnitChangeRequest.objects.create(
org_unit=self.org_unit, new_name="Rejected Request", status=m.OrgUnitChangeRequest.Statuses.REJECTED
)
change_request_approved = m.OrgUnitChangeRequest.objects.create(
org_unit=self.org_unit, new_name="Approved Request", status=m.OrgUnitChangeRequest.Statuses.APPROVED
)

# Authenticate the user with review permissions
self.client.force_authenticate(self.user_with_review_perm)

# Test filtering by multiple statuses
response = self.client.get("/api/orgunits/changes/?status=new,rejected")
self.assertJSONResponse(response, 200)
self.assertEqual(len(response.data["results"]), 2)
result_statuses = {change["status"] for change in response.data["results"]}
self.assertIn(m.OrgUnitChangeRequest.Statuses.NEW, result_statuses)
self.assertIn(m.OrgUnitChangeRequest.Statuses.REJECTED, result_statuses)
self.assertNotIn(m.OrgUnitChangeRequest.Statuses.APPROVED, result_statuses)

0 comments on commit 75997c5

Please sign in to comment.