Fix deleting soft-removed packages

This commit is contained in:
rubenwardy 2024-06-01 15:35:10 +01:00
parent 1fad818f05
commit e1e77033fe
2 changed files with 22 additions and 6 deletions

@ -13,7 +13,7 @@
#
# 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/>.
import datetime
import os
from typing import List
@ -23,7 +23,7 @@ from flask import redirect, url_for, flash, current_app
from sqlalchemy import or_, and_
from app.models import PackageRelease, db, Package, PackageState, PackageScreenshot, MetaPackage, User, \
NotificationType, PackageUpdateConfig, License, UserRank, PackageType, Thread
NotificationType, PackageUpdateConfig, License, UserRank, PackageType, Thread, AuditLogEntry
from app.tasks.emails import send_pending_digests
from app.tasks.forumtasks import import_topic_list, check_all_forum_accounts
from app.tasks.importtasks import import_repo_screenshot, check_zip_release, check_for_updates, update_all_game_support, \
@ -305,19 +305,35 @@ def do_notify_git_forums_links():
return redirect(url_for("tasks.check", id=task_id, r=url_for("admin.admin_page")))
@action("DANGER: Delete removed packages")
def del_removed_packages():
query = Package.query.filter_by(state=PackageState.DELETED)
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)
@action("DANGER: Delete all removed packages")
def del_removed_packages():
query = Package.query.filter_by(state=PackageState.DELETED)
return handle_delete_packages(query)
@action("DANGER: Check all releases (postReleaseCheckUpdate)")
def check_releases():
releases = PackageRelease.query.filter(PackageRelease.url.like("/uploads/%")).all()

@ -423,7 +423,7 @@ class Package(db.Model):
primaryjoin="and_(Package.id==PackageScreenshot.package_id, PackageScreenshot.approved)")
cover_image_id = db.Column(db.Integer, db.ForeignKey("package_screenshot.id"), nullable=True, default=None)
cover_image = db.relationship("PackageScreenshot", uselist=False, foreign_keys=[cover_image_id])
cover_image = db.relationship("PackageScreenshot", uselist=False, foreign_keys=[cover_image_id], post_update=True)
maintainers = db.relationship("User", secondary=maintainers)