diff --git a/tools/delete_dev_users.py b/tools/delete_dev_users.py index 0554b8fd..b819563f 100644 --- a/tools/delete_dev_users.py +++ b/tools/delete_dev_users.py @@ -1,80 +1,20 @@ -from zeeguu.api.app import create_app -from zeeguu.core.model import User, ArticleDifficultyFeedback, PersonalCopy, SearchFilter, UserPreference - import zeeguu.core -from zeeguu.core.model.starred_article import StarredArticle +from zeeguu.core.model import User +from zeeguu.core.account_management.user_account_deletion import delete_user_account +from zeeguu.api.app import create_app app = create_app() app.app_context().push() db_session = zeeguu.core.model.db.session -from zeeguu.core.model import ( - SearchSubscription, - TopicFilter, - TopicSubscription, - Teacher, - TeacherCohortMap, - Session, - User, - UserActivityData, - Bookmark, - UserArticle, - UserReadingSession, - UserExerciseSession, -) -from zeeguu.core.model import Article - -tables_to_modify = [ - SearchSubscription, - TopicFilter, - TopicSubscription, - Session, - Teacher, - TeacherCohortMap, - Bookmark, - UserActivityData, - UserArticle, - UserReadingSession, - UserExerciseSession, - StarredArticle, - ArticleDifficultyFeedback, - PersonalCopy, - SearchFilter, - UserPreference -] - - -def delete_user(subject): - articles = Article.uploaded_by(subject.id) - print(f"articles to update uploaded id in:") - for a in articles: - a.uploader_id = None - db_session.add(a) - db_session.commit() - - print(f"Deleting user {subject.name}...") - for each_table in tables_to_modify: - subject_related = each_table.query.filter_by(user_id=subject.id).all() - - print(f"{each_table.__tablename__}: {len(subject_related)}") - - for each in subject_related: - db_session.delete(each) - db_session.commit() - - print(f"Done deleting user {subject.id}") - db_session.delete(subject) - db_session.commit() - - # delete all anonymous users for user in User.find_all(): if "anon.zeeguu" in user.email: - delete_user(user) + delete_user_account(db_session, user) for user in User.query.filter_by(is_dev=True): print("deleting ... " + user.name) - delete_user(user) + delete_user_account(db_session, user) print("Remaining users: " + str(len(User.find_all()))) diff --git a/zeeguu/api/endpoints/accounts.py b/zeeguu/api/endpoints/accounts.py index 78ab743a..d7eeb560 100644 --- a/zeeguu/api/endpoints/accounts.py +++ b/zeeguu/api/endpoints/accounts.py @@ -18,10 +18,12 @@ @cross_domain @requires_session def remove_user(): - from zeeguu.core.account_management.user_account_deletion import delete_user_account + from zeeguu.core.account_management.user_account_deletion import ( + delete_user_account_w_session, + ) try: - delete_user_account(db_session, flask.g.session_uuid) + delete_user_account_w_session(db_session, flask.g.session_uuid) return "OK" except Exception as e: diff --git a/zeeguu/core/account_management/user_account_deletion.py b/zeeguu/core/account_management/user_account_deletion.py index 351a526c..e911e7ee 100644 --- a/zeeguu/core/account_management/user_account_deletion.py +++ b/zeeguu/core/account_management/user_account_deletion.py @@ -50,12 +50,10 @@ ] -def delete_user_account(db_session, user_session_uuid): +def delete_user_account(db_session, user_to_delete): try: start_time = time.time() total_rows_affected = 0 - user_session = Session.find(user_session_uuid) - user_to_delete = User.find_by_id(user_session.user_id) articles = Article.uploaded_by(user_to_delete.id) print(f"Removing author from Articles:") for a in articles: @@ -76,7 +74,7 @@ def delete_user_account(db_session, user_session_uuid): for each in subject_related: total_rows_affected += 1 db_session.delete(each) - # db_session.commit() + db_session.commit() db_session.delete(user_to_delete) db_session.commit() @@ -84,7 +82,17 @@ def delete_user_account(db_session, user_session_uuid): print( f"A total of {total_rows_affected} rows were affected. The process took: {end_time:.2f} seconds." ) + except Exception as e: + from sentry_sdk import capture_exception + capture_exception(e) + print(e) + raise Exception(f"Could not delete the user '{user_to_delete}'") + + +def delete_user_account_w_session(db_session, user_session_uuid): + try: + user_session = Session.find(user_session_uuid) except sqlalchemy.exc.IntegrityError: raise Exception("Integrity Error") except sqlalchemy.exc.NoResultFound: @@ -97,3 +105,5 @@ def delete_user_account(db_session, user_session_uuid): raise Exception( f"Could not delete the user with session: '{user_session_uuid}'" ) + user_to_delete = User.find_by_id(user_session.user_id) + delete_user_account(db_session, user_to_delete) diff --git a/zeeguu/core/test/test_user.py b/zeeguu/core/test/test_user.py index 5d1a33eb..2ea5b72a 100644 --- a/zeeguu/core/test/test_user.py +++ b/zeeguu/core/test/test_user.py @@ -10,7 +10,9 @@ from zeeguu.core.test.rules.user_rule import UserRule from zeeguu.core.model import User, Session from zeeguu.core.model import db -from zeeguu.core.account_management.user_account_deletion import delete_user_account +from zeeguu.core.account_management.user_account_deletion import ( + delete_user_account_w_session, +) class UserTest(ModelTestMixIn): @@ -157,5 +159,5 @@ def test_user_deletion(self): new_session = Session.create_for_user(self.user) db.session.add(new_session) db.session.commit() - delete_user_account(db.session, new_session.uuid) + delete_user_account_w_session(db.session, new_session.uuid) assert not User.exists(self.user)