From ba7ed40d6ac807c899bb93026b8b414759cfc942 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Tue, 22 Nov 2022 21:55:40 +0000 Subject: [PATCH] Fix crash in upgrade_new_members task --- app/__init__.py | 11 +++++------ app/blueprints/admin/actions.py | 11 ++++------- app/tasks/usertasks.py | 11 ++++++----- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 0fd0b8c4..15cdd475 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -159,12 +159,11 @@ def get_locale(): locale = request.accept_languages.best_match(locales) if locale and current_user.is_authenticated: - new_session = models.db.create_session({})() - new_session.query(models.User) \ - .filter(models.User.username == current_user.username) \ - .update({ "locale": locale }) - new_session.commit() - new_session.close() + with models.db.create_session({})() as new_session: + new_session.query(models.User) \ + .filter(models.User.username == current_user.username) \ + .update({ "locale": locale }) + new_session.commit() return locale diff --git a/app/blueprints/admin/actions.py b/app/blueprints/admin/actions.py index 8d460c47..b0821838 100644 --- a/app/blueprints/admin/actions.py +++ b/app/blueprints/admin/actions.py @@ -29,6 +29,7 @@ from app.models import PackageRelease, db, Package, PackageState, PackageScreens from app.tasks.emails import send_pending_digests from app.tasks.forumtasks import importTopicList, checkAllForumAccounts from app.tasks.importtasks import importRepoScreenshot, checkZipRelease, check_for_updates, updateAllGameSupport +from app.tasks.usertasks import upgrade_new_members from app.utils import addNotification, get_system_user from app.utils.image import get_image_size @@ -339,10 +340,6 @@ def do_send_pending_digests(): @action("Set users to new member") def set_new_members(): - threshold = datetime.datetime.now() - datetime.timedelta(days=7) - - User.query.filter(User.rank == UserRank.MEMBER, - ~User.replies.any(ThreadReply.created_at < threshold), - ~User.packages.any(Package.approved_at < threshold)).update({"rank": UserRank.NEW_MEMBER}, synchronize_session=False) - - db.session.commit() + task_id = uuid() + upgrade_new_members.apply_async((), task_id=task_id) + return redirect(url_for("tasks.check", id=task_id, r=url_for("admin.admin_page"))) diff --git a/app/tasks/usertasks.py b/app/tasks/usertasks.py index ccc6338b..0f45457a 100644 --- a/app/tasks/usertasks.py +++ b/app/tasks/usertasks.py @@ -37,10 +37,11 @@ def delete_inactive_users(): @celery.task() def upgrade_new_members(): - threshold = datetime.datetime.now() - datetime.timedelta(days=7) + with db.create_session({})() as session: + threshold = datetime.datetime.now() - datetime.timedelta(days=7) - User.query.filter(and_(User.rank == UserRank.NEW_MEMBER, or_( - User.replies.any(ThreadReply.created_at < threshold), - User.packages.any(Package.approved_at < threshold)))).update({"rank": UserRank.MEMBER}) + session.query(User).filter(and_(User.rank == UserRank.NEW_MEMBER, or_( + User.replies.any(ThreadReply.created_at < threshold), + User.packages.any(Package.approved_at < threshold)))).update({"rank": UserRank.MEMBER}, synchronize_session=False) - db.session.commit() + session.commit()