forked from kompetenzinventar/ki-backend
implement profile search
This commit is contained in:
@ -2,4 +2,5 @@
|
||||
#
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
from ki.handlers.find_profiles import find_profiles # noqa
|
||||
from ki.handlers.update_profile import update_profile # noqa
|
||||
|
28
ki/handlers/find_profiles.py
Normal file
28
ki/handlers/find_profiles.py
Normal file
@ -0,0 +1,28 @@
|
||||
from flask import make_response, request
|
||||
|
||||
from ki.models import Profile
|
||||
|
||||
|
||||
def find_profiles():
|
||||
page = int(request.args.get("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)
|
||||
|
||||
offset = (page - 1) * page_size
|
||||
|
||||
query = Profile.query.filter(Profile.visible is True)
|
||||
count = query.count()
|
||||
|
||||
db_profiles = query.limit(page_size).offset(offset).all()
|
||||
api_profiles = []
|
||||
|
||||
for db_profile in db_profiles:
|
||||
api_profiles.append(db_profile.to_dict())
|
||||
|
||||
return make_response({"total": count, "profiles": api_profiles})
|
Reference in New Issue
Block a user