Add ability to delete collections

This commit is contained in:
rubenwardy 2023-08-14 22:17:29 +01:00
parent 4a0653bcfd
commit af4f03d298
4 changed files with 44 additions and 2 deletions

@ -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/<author>/<name>/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

@ -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"),)

@ -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 %}
<form method="POST" action="" class="card box_grey">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
<h3 class="card-header">{{ self.title() }}</h3>
<div class="card-body">
<p>{{ _("Deleting is permanent") }}</p>
<a class="btn btn-secondary mr-3" href="{{ collection.get_url('collections.create_edit') }}">{{ _("Cancel") }}</a>
<input type="submit" value="{{ _('Delete') }}" class="btn btn-danger" />
</div>
</form>
{% endblock %}

@ -28,6 +28,9 @@
</form>
{% endif %}
{% if collection.check_perm(current_user, "EDIT_COLLECTION") %}
<a class="btn btn-danger ml-2" href="{{ collection.get_url('collections.delete') }}">
{{ _("Delete") }}
</a>
<a class="btn btn-primary ml-2" href="{{ collection.get_url('collections.create_edit') }}">
{{ _("Edit") }}
</a>