Skip to content

Commit

Permalink
Refactoring to remove duplication of code
Browse files Browse the repository at this point in the history
There is now a method to remove a user directly if one has the user object and another that retrieves the user based on a session before deleting them. The latter is used for the frontend.
  • Loading branch information
tfnribeiro committed Jun 12, 2024
1 parent eccc777 commit 1e01bd0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 73 deletions.
70 changes: 5 additions & 65 deletions tools/delete_dev_users.py
Original file line number Diff line number Diff line change
@@ -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())))
6 changes: 4 additions & 2 deletions zeeguu/api/endpoints/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 14 additions & 4 deletions zeeguu/core/account_management/user_account_deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -76,15 +74,25 @@ 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()
end_time = time.time() - start_time
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:
Expand All @@ -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)
6 changes: 4 additions & 2 deletions zeeguu/core/test/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

0 comments on commit 1e01bd0

Please sign in to comment.