Remove package deletion to worker

This commit is contained in:
rubenwardy 2024-06-02 11:40:33 +01:00
parent e1e77033fe
commit 2d29fb1994
2 changed files with 28 additions and 22 deletions

@ -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"))) 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") @action("DANGER: Delete less popular removed packages")
def del_less_popular_removed_packages(): def del_less_popular_removed_packages():
one_year_ago = datetime.datetime.now() - datetime.timedelta(days=365) task_id = uuid()
query = Package.query.filter( clear_removed_packages.apply_async((False, ), task_id=task_id)
Package.state == PackageState.DELETED, return redirect(url_for("tasks.check", id=task_id, r=url_for("admin.admin_page")))
Package.downloads < 1000,
~Package.audit_log_entries.any(AuditLogEntry.created_at > one_year_ago))
return handle_delete_packages(query)
@action("DANGER: Delete all removed packages") @action("DANGER: Delete all removed packages")
def del_removed_packages(): def del_removed_packages():
query = Package.query.filter_by(state=PackageState.DELETED) task_id = uuid()
return handle_delete_packages(query) 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)") @action("DANGER: Check all releases (postReleaseCheckUpdate)")

@ -14,11 +14,12 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import datetime
import re import re
from sqlalchemy import or_, and_ 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.tasks import celery
from app.utils import post_bot_message from app.utils import post_bot_message
@ -82,3 +83,23 @@ def notify_about_git_forum_links():
post_bot_message(package, title, msg) post_bot_message(package, title, msg)
db.session.commit() 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"