From f3840f18b72f954a19f800c3aff16e5f60e11e56 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Thu, 20 Apr 2023 17:33:19 +0200 Subject: [PATCH 1/3] Consider real name when searching (kompetenzinventar/ki-doku#48) --- ki/handlers/find_profiles.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ki/handlers/find_profiles.py b/ki/handlers/find_profiles.py index 1f11967..69a59c9 100644 --- a/ki/handlers/find_profiles.py +++ b/ki/handlers/find_profiles.py @@ -4,7 +4,7 @@ from flask import make_response, request -from ki.models import Profile, ProfileSkill, Skill, ProfileLanguage, Language +from ki.models import Profile, ProfileSkill, Skill, ProfileLanguage, Language, Address def find_profiles(): @@ -25,13 +25,18 @@ def find_profiles(): .order_by(Profile.nickname) \ .filter(Profile.visible.is_(True)) \ .join(Profile.skills, isouter=True).join(ProfileSkill.skill, isouter=True) \ - .join(Profile.languages, isouter=True).join(ProfileLanguage.language, isouter=True) + .join(Profile.languages, isouter=True).join(ProfileLanguage.language, isouter=True) \ + .join(Address, isouter=True) if "search" in request.args: terms = request.args["search"].split(" ") for term in terms: + query = query.filter( - Profile.nickname.like(f"%{term}%") | Skill.name.like(f"%{term}%") | Language.name.like(f"%{term}%")) + Profile.nickname.like(f"%{term}%") | # noqa: W504 + Skill.name.like(f"%{term}%") | # noqa: W504 + Language.name.like(f"%{term}%") | # noqa: W504 + Address.name.like(f"%{term}%")) if "nickname" in request.args: nickname = request.args.get("nickname") -- 2.30.2 From c65ef4a95c87780b9c9b24f7eeb1c2ce70fe279c Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Thu, 20 Apr 2023 18:07:01 +0200 Subject: [PATCH 2/3] Add a test to test search (kompetenzinventar/ki-doku#48) --- ki/actions/seed.py | 21 +++++++++++++-------- ki/test/test_find_profiles_endpoint.py | 8 ++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ki/actions/seed.py b/ki/actions/seed.py index 170d58f..e314a37 100644 --- a/ki/actions/seed.py +++ b/ki/actions/seed.py @@ -59,6 +59,8 @@ def seed_user(nickname, db.session.add(profile) + return profile + def seed(dev: bool): seed_contacttypes() @@ -145,14 +147,17 @@ def seed(dev: bool): for i in range(1, 20): seed_user(f"babsi{i}", visible=True) - seed_user("dirtydieter", - visible=True, - volunteerwork="Müll sammeln", - availability_status=True, - availability_hours_per_week=24, - availability_text="Nur Nachts!", - freetext="1001010010111!!!", - skills=[(Skill.skill_id_php, 5)]) + dieter = seed_user("dirtydieter", + visible=True, + volunteerwork="Müll sammeln", + availability_status=True, + availability_hours_per_week=24, + availability_text="Nur Nachts!", + freetext="1001010010111!!!", + skills=[(Skill.skill_id_php, 5)]) + + dieters_address = Address(name="Friedrich Witzig", profile=dieter) + db.session.add(dieters_address) all_skills = Skill.query.all() all_profile_skills = [] diff --git a/ki/test/test_find_profiles_endpoint.py b/ki/test/test_find_profiles_endpoint.py index 0146721..33dac44 100644 --- a/ki/test/test_find_profiles_endpoint.py +++ b/ki/test/test_find_profiles_endpoint.py @@ -99,6 +99,14 @@ class TestFindProfilesEndpoint(ApiTest): self.assertDictContainsSubset({"nickname": "jutta"}, response.json["profiles"][0]) self.assertDictContainsSubset({"nickname": "monique"}, response.json["profiles"][1]) + def test_find_peter_by_name(self): + token = self.login("peter", "geheim")["token"] + + response = self.client.get("/users/profiles?search=friedrich", headers={"Authorization": "Bearer " + token}) + self.assertEqual(response.status_code, 200) + self.assertDictContainsSubset({"total": 1}, response.json) + self.assertDictContainsSubset({"nickname": "dirtydieter"}, response.json["profiles"][0]) + if __name__ == "main": unittest.main() -- 2.30.2 From d7f4acf2513a0b5e0b47ef77355e69059b6fb7a7 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Thu, 20 Apr 2023 18:25:32 +0200 Subject: [PATCH 3/3] Fix test name (kompetenzinventar/ki-doku#48) --- ki/test/test_find_profiles_endpoint.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ki/test/test_find_profiles_endpoint.py b/ki/test/test_find_profiles_endpoint.py index 33dac44..74eeae3 100644 --- a/ki/test/test_find_profiles_endpoint.py +++ b/ki/test/test_find_profiles_endpoint.py @@ -99,7 +99,7 @@ class TestFindProfilesEndpoint(ApiTest): self.assertDictContainsSubset({"nickname": "jutta"}, response.json["profiles"][0]) self.assertDictContainsSubset({"nickname": "monique"}, response.json["profiles"][1]) - def test_find_peter_by_name(self): + def test_find_dieter_by_name(self): token = self.login("peter", "geheim")["token"] response = self.client.get("/users/profiles?search=friedrich", headers={"Authorization": "Bearer " + token}) -- 2.30.2