open-law/app/views/book/section.py

104 lines
3.2 KiB
Python
Raw Normal View History

2023-05-17 16:21:53 +00:00
from flask import (
flash,
redirect,
url_for,
)
from flask_login import login_required
2023-05-30 14:03:05 +00:00
from app.controllers import register_book_verify_route
from app.controllers.delete_nested_book_entities import delete_nested_section_entities
2023-05-17 16:21:53 +00:00
from app import models as m, db, forms as f
from app.logger import log
from .bp import bp
@bp.route("/<int:book_id>/<int:collection_id>/create_section", methods=["POST"])
@register_book_verify_route(bp.name)
@login_required
2023-05-30 06:51:58 +00:00
def section_create(book_id: int, collection_id: int):
2023-05-17 16:21:53 +00:00
book: m.Book = db.session.get(m.Book, book_id)
collection: m.Collection = db.session.get(m.Collection, collection_id)
redirect_url = url_for("book.collection_view", book_id=book_id)
if collection_id:
redirect_url = url_for(
"book.collection_view",
2023-05-17 16:21:53 +00:00
book_id=book_id,
)
form = f.CreateSectionForm()
if form.validate_on_submit():
section: m.Section = m.Section(
label=form.label.data,
2023-05-30 06:51:58 +00:00
collection_id=collection_id,
2023-05-17 16:21:53 +00:00
version_id=book.last_version.id,
)
2023-05-30 06:51:58 +00:00
collection.is_leaf = True
2023-05-17 16:21:53 +00:00
log(log.INFO, "Create section [%s]. Collection: [%s]", section, collection_id)
section.save()
flash("Success!", "success")
return redirect(redirect_url)
else:
log(log.ERROR, "Section create errors: [%s]", form.errors)
for field, errors in form.errors.items():
field_label = form._fields[field].label.text
for error in errors:
flash(error.replace("Field", field_label), "danger")
return redirect(redirect_url)
2023-05-30 06:51:58 +00:00
@bp.route("/<int:book_id>/<int:section_id>/edit_section", methods=["POST"])
2023-05-17 16:21:53 +00:00
@register_book_verify_route(bp.name)
@login_required
2023-05-30 06:51:58 +00:00
def section_edit(book_id: int, section_id: int):
2023-05-17 16:21:53 +00:00
section: m.Section = db.session.get(m.Section, section_id)
form = f.EditSectionForm()
2023-05-30 06:51:58 +00:00
redirect_url = url_for("book.collection_view", book_id=book_id)
2023-05-17 16:21:53 +00:00
if form.validate_on_submit():
label = form.label.data
if label:
section.label = label
log(log.INFO, "Edit section [%s]", section.id)
section.save()
flash("Success!", "success")
return redirect(redirect_url)
else:
log(log.ERROR, "Section edit errors: [%s]", form.errors)
for field, errors in form.errors.items():
field_label = form._fields[field].label.text
for error in errors:
flash(error.replace("Field", field_label), "danger")
return redirect(redirect_url)
2023-05-30 06:51:58 +00:00
@bp.route("/<int:book_id>/<int:section_id>/delete_section", methods=["POST"])
2023-05-17 16:21:53 +00:00
@register_book_verify_route(bp.name)
@login_required
def section_delete(
book_id: int,
section_id: int,
):
section: m.Section = db.session.get(m.Section, section_id)
section.is_deleted = True
delete_nested_section_entities(section)
2023-05-30 06:51:58 +00:00
if not section.collection.active_sections:
2023-05-17 16:21:53 +00:00
log(
log.INFO,
"Section [%s] has no active section. Set is_leaf = False",
section.id,
)
2023-05-30 06:51:58 +00:00
section.collection.is_leaf = False
2023-05-17 16:21:53 +00:00
log(log.INFO, "Delete section [%s]", section.id)
section.save()
flash("Success!", "success")
2023-05-30 06:51:58 +00:00
return redirect(url_for("book.collection_view", book_id=book_id))