From 4945987b2f01e2ea718c68152e692050aa52200c Mon Sep 17 00:00:00 2001 From: Tore Brede <Tore.Brede@uib.no> Date: Tue, 4 Jan 2022 18:25:51 +0100 Subject: [PATCH] GREG-161: Working on updating guest search --- .../src/routes/sponsor/register/frontPage.tsx | 5 ++++- gregui/api/urls.py | 7 ++----- gregui/api/views/person.py | 15 ++++++++++++++- gregui/tests/api/views/test_search.py | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 gregui/tests/api/views/test_search.py diff --git a/frontend/src/routes/sponsor/register/frontPage.tsx b/frontend/src/routes/sponsor/register/frontPage.tsx index 5ad6ac4f..b8db07d6 100644 --- a/frontend/src/routes/sponsor/register/frontPage.tsx +++ b/frontend/src/routes/sponsor/register/frontPage.tsx @@ -28,7 +28,10 @@ function FrontPage() { const getGuests = (event: React.ChangeEvent<HTMLInputElement>) => { if (event.target.value) { setSearchHasInput(true) - fetch(`/api/ui/v1/person/search/${event.target.value}`, fetchJsonOpts()) + fetch( + `/api/ui/v1/person/search/?q=${event.target.value}`, + fetchJsonOpts() + ) .then((response) => { if (response.ok) { return response.json() diff --git a/gregui/api/urls.py b/gregui/api/urls.py index c2fb05da..008cc828 100644 --- a/gregui/api/urls.py +++ b/gregui/api/urls.py @@ -36,10 +36,7 @@ urlpatterns += [ name="invite-resend", ), path("invite/", InvitationView.as_view(), name="invitation"), - re_path( - r"person/search/(?P<searchstring>\S+)", # search for sequence of any non-whitespace char - PersonSearchViewSet.as_view({"get": "list"}), - name="person-search", - ), + path("person/search/", PersonSearchViewSet.as_view({"get": "list"}), name="person-search", + ), path("userinfo/", UserInfoView.as_view(), name="userinfo"), ] diff --git a/gregui/api/views/person.py b/gregui/api/views/person.py index b7173c76..101d9a4d 100644 --- a/gregui/api/views/person.py +++ b/gregui/api/views/person.py @@ -4,6 +4,8 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.viewsets import GenericViewSet from greg.api.serializers.person import PersonSearchSerializer, SpecialPersonSerializer +from rest_framework.response import Response +from rest_framework import status from greg.models import Identity, Person from greg.permissions import IsSponsor from gregui import validation @@ -45,7 +47,18 @@ class PersonSearchViewSet(mixins.ListModelMixin, GenericViewSet): serializer_class = PersonSearchSerializer def get_queryset(self): - search = self.kwargs["searchstring"] + if "q" not in self.request.query_params: + return Response(status=status.HTTP_400_BAD_REQUEST) + + search = self.request.query_params["q"] + + # TODO Implement search + + split_search = search.split() + + name_matches = Person.objects.filter(first_name__icontains=search, + last_name__icontains=search) + return Identity.objects.filter( value__icontains=search, # icontains to include wrong case emails type__in=[ diff --git a/gregui/tests/api/views/test_search.py b/gregui/tests/api/views/test_search.py new file mode 100644 index 00000000..04110d9a --- /dev/null +++ b/gregui/tests/api/views/test_search.py @@ -0,0 +1,19 @@ +import pytest +from rest_framework import status +from rest_framework.reverse import reverse + + +@pytest.mark.django_db +def test_name_search(client, log_in, user_sponsor, create_person): + person = create_person( + first_name="foo", + last_name="bar", + email="foo@bar.com", + ) + + url = reverse("gregui-v1:person-search", kwargs={"searchstring": "foo bar"}) + + log_in(user_sponsor) + response = client.get(url) + + assert response.status_code == status.HTTP_200_OK -- GitLab