From d0823fd59fb911b7adebd5ac514fde9275c14054 Mon Sep 17 00:00:00 2001
From: pka065 <pka065@it6100016.klientdrift.uib.no>
Date: Fri, 7 Oct 2022 11:21:22 +0200
Subject: [PATCH] GREG-292: search for person via v1-api

Add a possibility to search via v1-api, and add birth date to the data in such a case. No changes for previous gui-endpoint.
---
 greg/api/urls.py           |  6 ++++++
 gregui/api/views/person.py | 10 +++++++---
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/greg/api/urls.py b/greg/api/urls.py
index 55fc742f..8d8ad36a 100644
--- a/greg/api/urls.py
+++ b/greg/api/urls.py
@@ -17,6 +17,7 @@ from greg.api.views.sponsor import (
     SponsorGuestsViewSet,
     SponsorOrgunitLinkView,
 )
+from gregui.api.views.person import PersonSearchViewSet
 
 router = DefaultRouter(trailing_slash=False)
 router.register(r"persons", PersonViewSet, basename="person")
@@ -74,4 +75,9 @@ urlpatterns += [
         SponsorOrgunitLinkView.as_view({"post": "create", "delete": "destroy"}),
         name="sponsor_orgunit-detail",
     ),
+    re_path(
+        "person_search/",
+        PersonSearchViewSet.as_view({"get": "list"}),
+        name="person-search",
+    ),
 ]
diff --git a/gregui/api/views/person.py b/gregui/api/views/person.py
index 0ebb8c35..e67bd200 100644
--- a/gregui/api/views/person.py
+++ b/gregui/api/views/person.py
@@ -107,6 +107,9 @@ class PersonSearchViewSet(GenericViewSet):
 
     def get_hits(self):
         search = self.request.query_params["q"]
+        include_birth_date = False
+        if not "gregui" in self.request.version:
+            include_birth_date = True
 
         split_search = search.split()
         words_joined = "|".join(map(str, split_search))
@@ -124,9 +127,10 @@ class PersonSearchViewSet(GenericViewSet):
 
         included_persons = []
         for person in persons:
-            hits.append(
-                {"pid": person.id, "first": person.first_name, "last": person.last_name}
-            )
+            output_data = {"pid": person.id, "first": person.first_name, "last": person.last_name}
+            if include_birth_date:
+                output_data["date_of_birth"] = person.date_of_birth
+            hits.append(output_data)
             included_persons.append(person.id)
 
         if len(hits) == 10:
-- 
GitLab