Skip to content
Snippets Groups Projects
Commit a95dd98c authored by Marte Fossum's avatar Marte Fossum
Browse files

Merge branch 'GREG-252-filtering-roletype-person' into 'master'

Add filter for role type under /person and test

See merge request !320
parents a6fa6771 01ca5e8a
No related branches found
No related tags found
1 merge request!320Add filter for role type under /person and test
Pipeline #131186 passed
......@@ -7,7 +7,7 @@ PIP ?= pip -q
POETRY ?= poetry
PYLINT ?= pylint -sn
PYTEST ?= pytest -v -s --no-header
PYTHON ?= python3.9
PYTHON ?= python3.10
VENV ?= venv
mypy = $(MYPY) --config-file mypy.ini
......
......@@ -4,6 +4,7 @@ from django.db.models import Q, When, Case, BooleanField
from django_filters.rest_framework import (
BaseInFilter,
BooleanFilter,
CharFilter,
FilterSet,
)
......@@ -26,10 +27,11 @@ class RoleFilter(FilterSet):
class PersonFilter(FilterSet):
verified = BooleanFilter(method="verified_filter")
active = BooleanFilter(method="active_role_filter")
role_type = CharFilter(method="role_type_filter")
class Meta:
model = Person
fields = ["first_name", "last_name", "verified", "active"]
fields = ["first_name", "last_name", "verified", "active", "role_type"]
def verified_filter(self, queryset, name, value):
if value is True:
......@@ -56,6 +58,9 @@ class PersonFilter(FilterSet):
return queryset
def role_type_filter(self, queryset, name, value):
return queryset.filter(roles__type__identifier=value)
class IdentityFilter(FilterSet):
class Meta:
......
......@@ -47,6 +47,11 @@ class PersonViewSet(viewsets.ModelViewSet):
required=False,
type=bool,
),
OpenApiParameter(
name="role_type",
required=False,
type=str,
),
]
)
def list(self, request, *args, **kwargs):
......
......@@ -521,6 +521,52 @@ def test_filter_active_value_false(
assert len(results) == 1
@pytest.mark.django_db
def test_filter_role_type(
client: APIClient,
person_foo: Person,
person: Person,
person_bar: Person,
role_type_test_guest: Role,
sponsor_guy: Sponsor,
unit_foo: OrganizationalUnit,
):
date_today = datetime.date.today()
best_test_guest = RoleType.objects.create(
identifier="Best Test Guest",
)
Role.objects.create(
person=person_foo,
type=best_test_guest,
end_date=date_today + datetime.timedelta(days=1),
sponsor=sponsor_guy,
orgunit=unit_foo,
)
Role.objects.create(
person=person,
type=role_type_test_guest,
end_date=date_today + datetime.timedelta(days=1),
sponsor=sponsor_guy,
orgunit=unit_foo,
)
Role.objects.create(
person=person_bar,
type=role_type_test_guest,
end_date=date_today + datetime.timedelta(days=1),
sponsor=sponsor_guy,
orgunit=unit_foo,
)
url = reverse("v1:person-list")
response = client.get(url, {"role_type": "Best Test Guest"})
results = response.json()["results"]
assert len(results) == 1
@pytest.mark.django_db
def test_person_consents_get(
client: APIClient, person: Person, consent_fixture_choice_yes: Consent
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment