Added deleting of collections

This commit is contained in:
Анатолий Богомолов 2024-03-07 02:56:27 +10:00
parent cf76eb5b90
commit 09b5b36f86
3 changed files with 31 additions and 12 deletions

View File

@ -1,3 +1,21 @@
from flask import Blueprint, render_template, request from flask import Blueprint, render_template, request
from app.models.collection import Collection
from app.extensions import db
collections = Blueprint("collections", __name__, url_prefix="/collections", template_folder="templates") collections = Blueprint("collections", __name__, url_prefix="/collections", template_folder="templates")
@collections.route("/<int:id>", methods=["DELETE"])
def delete_collection(id: int):
collection: Collection = Collection.query.get_or_404(id)
length = len(Collection.query.all()) - 1
db.session.delete(collection)
db.session.commit()
if length <= 0:
return "<small class=\"form-text\">Здесь ничего нет</small>", 200
return "", 204

View File

@ -1,19 +1,18 @@
<div data-collection-id="{{ collection.id }}" class="card"> <div data-collection-id="{{ collection.id }}" class="card">
<div class="card-header">{{ collection.name }}</div> <div class="card-header"><h5>{{ collection.name }}</h5></div>
<div class="card-body"> <ul class="list-group list-group-flush">
<ul class="list-group list-group-flush"> <li class="list-group-item">Кол-во пользрвателей: {{ collection.users|length }}</li>
<li class="list-group-item">{{ collection.users.lenght() }}</li> </ul>
</ul> <form action="/api/collections/{{ collection.id }}" method="DELETE" class="card-footer px-2 pt-2">
</div>
<from action="/api/collection/{{ collection.id }}" method="DELETE" class="card-footer px-2 pt-2">
<button <button
type="submit" type="submit"
hx-delete='[data-collection-id="{{ collection.id }}"]' hx-delete="/api/collections/{{ collection.id }}"
hx-swap="outerHTML" hx-swap="outerHTML"
hx-target='[data-collection-id="{{ collection.id }}"]'
hx-confirm="Вы уверены, что хотите удалить коллекцию?" hx-confirm="Вы уверены, что хотите удалить коллекцию?"
class="btn btn-outline-danger" class="btn btn-outline-danger"
> >
Удалить Удалить
</button> </button>
</from> </form>
</div> </div>

View File

@ -1,7 +1,9 @@
<div id="cards-grid" class="row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4">
{% for collection in collections %} {% for collection in collections %}
<div id="cards-grid" class="row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4"> <div class="col mb-3">
{% include "collections/card.j2" %} {% include "collections/card.j2" %}
</div> </div>
{% else %} {% else %}
Здесь ничего нет <small class="form-text">Здесь ничего нет.</small>
{% endfor %} {% endfor %}
</div>