diff --git a/frontend/src/routes/sponsor/register/frontPage.tsx b/frontend/src/routes/sponsor/register/frontPage.tsx index 5ad6ac4ffc4398a8bff0971ce52480c86347b282..b8db07d66978640c00b343353baf01c230b2b27a 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 c2fb05dab808f6cd78d1c03c15685a977084efed..008cc82854478699d39b8b43a0b647b9ad9a5bdb 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 b7173c76af9e087d5b31047b6f7df285148dfedc..101d9a4dd076dc7bda22cf044073eaad7aeb89e1 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 0000000000000000000000000000000000000000..04110d9a5566d422ca3e0c1bd9696cf8690c7c2a --- /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