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.route('/', methods=["POST"])
def create_collection():
data = request.form
name = data.get('collection_name')
if not name:
return render_template('collections/create_form.j2', collections_error='Введите название базы!')
if Collection.exists(name):
return render_template('collections/create_form.j2', collections_error='База уже существует!')
swap = 'afterbegin' if len(Collection.query.all()) > 0 else 'innerHTML'
collection = Collection(name=name)
db.session.add(collection)
db.session.commit()
return f"", 200, {'Hx-Reswap': swap, 'HX-Retarget': '#collection-select'}
@collections.route("/", 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 "Здесь ничего нет", 200
return "", 200