diff --git a/app/blueprints/collections/__init__.py b/app/blueprints/collections/__init__.py index c723cf87..d3f12a5f 100644 --- a/app/blueprints/collections/__init__.py +++ b/app/blueprints/collections/__init__.py @@ -17,7 +17,7 @@ import re import typing -from flask import Blueprint, request, redirect, render_template, flash, abort +from flask import Blueprint, request, redirect, render_template, flash, abort, url_for from flask_babel import lazy_gettext, gettext from flask_login import current_user, login_required from flask_wtf import FlaskForm @@ -175,6 +175,27 @@ def handle_create_edit(collection: Collection, form: CollectionForm, return redirect(collection.get_url("collections.view")) +@bp.route("/collections///delete/", methods=["GET", "POST"]) +@login_required +def delete(author, name): + collection = Collection.query \ + .filter(Collection.name == name, Collection.author.has(username=author)) \ + .one_or_404() + if not collection.check_perm(current_user, Permission.EDIT_COLLECTION): + abort(403) + + if request.method == "POST": + add_audit_log(AuditSeverity.NORMAL, current_user, + f"Deleted collection {collection.author.username}/{collection.name}", + collection.get_url("collections.view"), None) + + db.session.delete(collection) + db.session.commit() + return redirect(url_for("collections.list_all", author=author)) + + return render_template("collections/delete.html", collection=collection) + + def toggle_package(collection: Collection, package: Package): severity = AuditSeverity.NORMAL if collection.author == current_user else AuditSeverity.EDITOR diff --git a/app/models/collections.py b/app/models/collections.py index 2f52b7e2..4c3ec623 100644 --- a/app/models/collections.py +++ b/app/models/collections.py @@ -48,7 +48,8 @@ class Collection(db.Model): private = db.Column(db.Boolean, nullable=False, default=False) packages = db.relationship("Package", secondary=CollectionPackage.__table__, backref="collections") - items = db.relationship("CollectionPackage", back_populates="collection", order_by=db.asc("created_at")) + items = db.relationship("CollectionPackage", back_populates="collection", order_by=db.asc("created_at"), + cascade="all, delete, delete-orphan") collection_name_valid = db.CheckConstraint("name ~* '^[a-z0-9_]+$' AND name != '_game'") __table_args__ = (db.UniqueConstraint("author_id", "name", name="_collection_uc"),) diff --git a/app/templates/collections/delete.html b/app/templates/collections/delete.html new file mode 100644 index 00000000..eb488aea --- /dev/null +++ b/app/templates/collections/delete.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} + +{% block title %} + {{ _('Delete collection "%(title)s" by %(author)s', title=collection.title, author=collection.author.username) }} +{% endblock %} + +{% block content %} +
+ +

{{ self.title() }}

+
+

{{ _("Deleting is permanent") }}

+ {{ _("Cancel") }} + +
+
+{% endblock %} diff --git a/app/templates/collections/view.html b/app/templates/collections/view.html index 523baa94..61282142 100644 --- a/app/templates/collections/view.html +++ b/app/templates/collections/view.html @@ -28,6 +28,9 @@ {% endif %} {% if collection.check_perm(current_user, "EDIT_COLLECTION") %} + + {{ _("Delete") }} + {{ _("Edit") }}