Compare commits

..

2 Commits

6 changed files with 34 additions and 81 deletions

View File

@ -3,4 +3,3 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
from ki.actions.seed import seed # noqa
from ki.actions.delete_profile import delete_profile # noqa

View File

@ -1,52 +0,0 @@
# SPDX-FileCopyrightText: WTF Kooperative eG <https://wtf-eg.de/>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
from sqlalchemy.exc import NoResultFound
from app import app, db
from ki.models import User
import sys
def delete_profile(nickname: str):
# Getting the user
try:
user = User.query.filter(User.auth_id.__eq__(nickname)).one()
except NoResultFound:
sys.exit(f'Username »{nickname}« not found')
app.logger.info(f'Starting to delete user »{nickname}.')
# Deleting associated languages skills
try:
for language in (user.profile.languages):
print(language.language_id)
app.logger.info(f'Deleting language skil »{language.language_id}« for »{nickname}«.')
db.session.delete(language)
db.session.commit()
except AttributeError:
# No languages left over
pass
# Deleting associated skills
try:
app.logger.info(f'Deleting skils for »{nickname}«.')
for skill in user.profile.skills:
db.session.delete(skill)
db.session.commit()
except AttributeError:
# No skills left over
pass
# Cleaning up profile
try:
app.logger.info(f'Deleting profile for »{nickname}«.')
db.session.delete(user.profile)
except:
# No profile to be deleted
pass
# Deleting the user
app.logger.info(f'Delete user »{nickname}«.')
db.session.delete(user)
db.session.commit()

View File

@ -143,6 +143,8 @@ def seed(dev: bool):
db.session.add(peter_fr)
seed_user("klaus")
for i in range(1, 20):
seed_user(f"babsi{i}", visible=True)
seed_user("dirtydieter",
visible=True,

View File

@ -6,19 +6,9 @@ import click
from app import app
from ki.actions import seed
from ki.actions import delete_profile
@app.cli.command("seed")
@click.option("--dev", is_flag=True)
def seed_command(dev):
seed(dev)
@app.cli.command("delete", help="Delete a user profile")
@click.option(
"--profile",
help="Username of profile",
)
def delete_command(profile):
delete_profile(profile)

View File

@ -8,7 +8,10 @@ from ki.models import Profile, ProfileSkill, Skill, ProfileLanguage, Language
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)
@ -19,6 +22,7 @@ def find_profiles():
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)
@ -33,13 +37,15 @@ def find_profiles():
nickname = request.args.get("nickname")
query = query.filter(Profile.nickname.like(f"%{nickname}%"))
count = query.count()
offset = (page - 1) * page_size
db_profiles = query.limit(page_size).offset(offset).all()
paginated_result = query.paginate(page=page, per_page=page_size)
api_profiles = []
for db_profile in db_profiles:
for db_profile in paginated_result.items:
api_profiles.append(db_profile.to_dict())
return make_response({"total": count, "profiles": api_profiles})
return make_response({
"total": paginated_result.total,
"pages": paginated_result.pages,
"page": paginated_result.page,
"profiles": api_profiles
})

View File

@ -20,25 +20,33 @@ class TestFindProfilesEndpoint(ApiTest):
response = self.client.get("/users/profiles?nickname=horsthorsthorst",
headers={"Authorization": "Bearer " + token})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {"total": 0, "profiles": []})
self.assertEqual(response.json, {"total": 0, "page": 1, "pages": 0, "profiles": []})
def test_find_sql_specialchars(self):
token = self.login("peter", "geheim")["token"]
response = self.client.get("/users/profiles?nickname=%22%27%25", headers={"Authorization": "Bearer " + token})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {"total": 0, "profiles": []})
self.assertEqual(response.json, {"total": 0, "page": 1, "pages": 0, "profiles": []})
def test_find_all(self):
def test_find_all_page1(self):
token = self.login("peter", "geheim")["token"]
response = self.client.get("/users/profiles", headers={"Authorization": "Bearer " + token})
self.assertEqual(response.status_code, 200)
self.assertDictContainsSubset({"total": 4}, response.json)
self.assertDictContainsSubset({"nickname": "dirtydieter"}, response.json["profiles"][0])
self.assertDictContainsSubset({"total": 23, "page": 1, "pages": 2}, response.json)
self.assertDictContainsSubset({"nickname": "babsi1"}, response.json["profiles"][0])
self.assertDictContainsSubset({"nickname": "dirtydieter"}, response.json["profiles"][19])
def test_find_all_page2(self):
token = self.login("peter", "geheim")["token"]
response = self.client.get("/users/profiles?page=2", headers={"Authorization": "Bearer " + token})
self.assertEqual(response.status_code, 200)
self.assertDictContainsSubset({"total": 23, "page": 2, "pages": 2}, response.json)
self.assertDictContainsSubset({"nickname": "giesela"}, response.json["profiles"][0])
self.assertDictContainsSubset({"nickname": "jutta"}, response.json["profiles"][1])
self.assertDictContainsSubset({"nickname": "giesela"}, response.json["profiles"][2])
self.assertDictContainsSubset({"nickname": "monique"}, response.json["profiles"][3])
self.assertDictContainsSubset({"nickname": "monique"}, response.json["profiles"][2])
def test_find_dieter(self):
token = self.login("peter", "geheim")["token"]
@ -62,8 +70,8 @@ class TestFindProfilesEndpoint(ApiTest):
response = self.client.get("/users/profiles?search=sql", headers={"Authorization": "Bearer " + token})
self.assertEqual(response.status_code, 200)
self.assertDictContainsSubset({"total": 2}, response.json)
self.assertDictContainsSubset({"nickname": "jutta"}, response.json["profiles"][0])
self.assertDictContainsSubset({"nickname": "giesela"}, response.json["profiles"][1])
self.assertDictContainsSubset({"nickname": "giesela"}, response.json["profiles"][0])
self.assertDictContainsSubset({"nickname": "jutta"}, response.json["profiles"][1])
def test_find_postgres(self):
token = self.login("peter", "geheim")["token"]
@ -71,8 +79,8 @@ class TestFindProfilesEndpoint(ApiTest):
response = self.client.get("/users/profiles?search=post", headers={"Authorization": "Bearer " + token})
self.assertEqual(response.status_code, 200)
self.assertDictContainsSubset({"total": 2}, response.json)
self.assertDictContainsSubset({"nickname": "jutta"}, response.json["profiles"][0])
self.assertDictContainsSubset({"nickname": "giesela"}, response.json["profiles"][1])
self.assertDictContainsSubset({"nickname": "giesela"}, response.json["profiles"][0])
self.assertDictContainsSubset({"nickname": "jutta"}, response.json["profiles"][1])
def test_find_php_franzosen(self):
token = self.login("peter", "geheim")["token"]