ki-backend/ki/actions/delete_profile.py

53 lines
1.5 KiB
Python
Raw Normal View History

# 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()