From 2d29fb19945eebfae75d0349312cfa81992175cf Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 2 Jun 2024 11:40:33 +0100 Subject: [PATCH] Remove package deletion to worker --- app/blueprints/admin/actions.py | 27 ++++++--------------------- app/tasks/pkgtasks.py | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/app/blueprints/admin/actions.py b/app/blueprints/admin/actions.py index 43101bb2..ca529f53 100644 --- a/app/blueprints/admin/actions.py +++ b/app/blueprints/admin/actions.py @@ -305,33 +305,18 @@ def do_notify_git_forums_links(): return redirect(url_for("tasks.check", id=task_id, r=url_for("admin.admin_page"))) -def handle_delete_packages(query): - count = query.count() - for pkg in query.all(): - pkg.review_thread = None - db.session.delete(pkg) - db.session.commit() - - clean_uploads() - - flash("Deleted {} soft deleted packages packages".format(count), "success") - return redirect(url_for("admin.admin_page")) - - @action("DANGER: Delete less popular removed packages") def del_less_popular_removed_packages(): - one_year_ago = datetime.datetime.now() - datetime.timedelta(days=365) - query = Package.query.filter( - Package.state == PackageState.DELETED, - Package.downloads < 1000, - ~Package.audit_log_entries.any(AuditLogEntry.created_at > one_year_ago)) - return handle_delete_packages(query) + task_id = uuid() + clear_removed_packages.apply_async((False, ), task_id=task_id) + return redirect(url_for("tasks.check", id=task_id, r=url_for("admin.admin_page"))) @action("DANGER: Delete all removed packages") def del_removed_packages(): - query = Package.query.filter_by(state=PackageState.DELETED) - return handle_delete_packages(query) + task_id = uuid() + clear_removed_packages.apply_async((True, ), task_id=task_id) + return redirect(url_for("tasks.check", id=task_id, r=url_for("admin.admin_page"))) @action("DANGER: Check all releases (postReleaseCheckUpdate)") diff --git a/app/tasks/pkgtasks.py b/app/tasks/pkgtasks.py index 888ea6eb..0a294b64 100644 --- a/app/tasks/pkgtasks.py +++ b/app/tasks/pkgtasks.py @@ -14,11 +14,12 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import datetime import re from sqlalchemy import or_, and_ -from app.models import Package, db, PackageState +from app.models import Package, db, PackageState, AuditLogEntry from app.tasks import celery from app.utils import post_bot_message @@ -82,3 +83,23 @@ def notify_about_git_forum_links(): post_bot_message(package, title, msg) db.session.commit() + + +@celery.task() +def clear_removed_packages(all_packages: bool): + if all_packages: + query = Package.query.filter_by(state=PackageState.DELETED) + else: + one_year_ago = datetime.datetime.now() - datetime.timedelta(days=365) + query = Package.query.filter( + Package.state == PackageState.DELETED, + Package.downloads < 1000, + ~Package.audit_log_entries.any(AuditLogEntry.created_at > one_year_ago)) + + count = query.count() + for pkg in query.all(): + pkg.review_thread = None + db.session.delete(pkg) + db.session.commit() + + return f"Deleted {count} soft deleted packages packages"