Skip to content

Commit

Permalink
Get rid of delete user as it seems to cause a lot of problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrillkuettel committed Jun 27, 2024
1 parent d72a9a1 commit f00ab27
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 165 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fanstatic.libraries =

console_scripts =
add_user = privatim.cli.user:add_user
delete_user = privatim.cli.user:delete_user
# delete_user = privatim.cli.user:delete_user
initialize_db = privatim.cli.initialize_db:main
add_meeting = privatim.cli.add_meeting:main
delete_meetings = privatim.cli.delete_meetings:main
Expand Down
82 changes: 0 additions & 82 deletions src/privatim/cli/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
from pyramid.paster import bootstrap
from pyramid.paster import get_appsettings
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError

from privatim.models import User
from privatim.orm import get_engine, Base
from sqlalchemy.orm import joinedload


from typing import TYPE_CHECKING
if TYPE_CHECKING:
from sqlalchemy.orm import Session


@click.command()
Expand Down Expand Up @@ -41,78 +34,3 @@ def add_user(
user = User(email=email, first_name=first_name, last_name=last_name)
user.set_password(password)
dbsession.add(user)


def _delete_user(session: 'Session', email: str) -> bool:
query = (
select(User)
.options(
joinedload(User.leading_groups),
joinedload(User.meetings),
joinedload(User.statements),
joinedload(User.comments),
joinedload(User.consultations),
)
.filter(User.email == email)
)

existing_user = session.execute(query).unique().scalar_one_or_none()
if not existing_user:
click.echo(f"No user found with email {email}.")
return False

try:
# # Remove user as leader from groups
for group in existing_user.leading_groups:
group.leader = None
#
# # Clear relationships
# existing_user.groups = []
# existing_user.meetings = []
#
# # Handle consultations
# for consultation in existing_user.consultations:
# consultation.creator = None

for comment in existing_user.comments:
comment.user_id = None

session.add(existing_user)
session.flush()
session.refresh(existing_user)
session.delete(existing_user)
session.flush()
click.echo(f"User with email {email} successfully deleted.")
return True
except SQLAlchemyError as e:
session.rollback()
click.echo(
f"Failed to delete user with email {email}. Error: {str(e)}"
)
return False


@click.command()
@click.argument('config_uri')
@click.option('--email', prompt=True)
def delete_user(config_uri: str, email: str) -> None:
env = bootstrap(config_uri)
settings = get_appsettings(config_uri)
engine = get_engine(settings)
Base.metadata.create_all(engine)

with env['request'].tm:
dbsession = env['request'].dbsession

if _delete_user(dbsession, email):
click.echo(
f"User with email {email} has been deleted along with related "
f"data."
)
else:
click.echo(f"Failed to delete user with email {email}.")

click.echo(
f"User with email {email} has been deleted along with related "
f"data."
)
82 changes: 0 additions & 82 deletions tests/cli/test_user_delete.py

This file was deleted.

0 comments on commit f00ab27

Please sign in to comment.