-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring to remove duplication of code
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
1 parent
eccc777
commit 1e01bd0
Showing
4 changed files
with
27 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters