Add a CLI to delete a user with it's profile information
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Frank Lanitz 2022-01-23 21:24:38 +01:00
parent dea781cc29
commit fc1681928b
3 changed files with 63 additions and 0 deletions

View File

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

View File

@ -0,0 +1,52 @@
# 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

@ -6,9 +6,19 @@ import click
from app import app from app import app
from ki.actions import seed from ki.actions import seed
from ki.actions import delete_profile
@app.cli.command("seed") @app.cli.command("seed")
@click.option("--dev", is_flag=True) @click.option("--dev", is_flag=True)
def seed_command(dev): def seed_command(dev):
seed(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)