57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
# SPDX-FileCopyrightText: WTF Kooperative eG <https://wtf-eg.de/>
|
|
#
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
from flask import make_response, request
|
|
|
|
from ki.models import Profile, ProfileSkill, Skill, ProfileLanguage, Language, Address
|
|
|
|
|
|
def find_profiles():
|
|
try:
|
|
page = int(request.args.get("page", 1))
|
|
except ValueError:
|
|
page = 1
|
|
|
|
if page < 1:
|
|
return make_response({"messages": {"page": "Die angefragte Seite muss mindestens 1 sein"}}, 400)
|
|
|
|
page_size = int(request.args.get("page_size", 20))
|
|
|
|
if page_size > 100:
|
|
return make_response({"messages": {"page_size": "Die maximale Anzahl Einträge pro Seite beträgt 100"}}, 400)
|
|
|
|
query = Profile.query.distinct(Profile.id) \
|
|
.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(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}%") | # 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")
|
|
query = query.filter(Profile.nickname.like(f"%{nickname}%"))
|
|
|
|
paginated_result = query.paginate(page=page, per_page=page_size)
|
|
api_profiles = []
|
|
|
|
for db_profile in paginated_result.items:
|
|
api_profiles.append(db_profile.to_dict())
|
|
|
|
return make_response({
|
|
"total": paginated_result.total,
|
|
"pages": paginated_result.pages,
|
|
"page": paginated_result.page,
|
|
"profiles": api_profiles
|
|
})
|