open-law/app/views/book.py

1267 lines
41 KiB
Python
Raw Normal View History

2023-04-24 08:31:54 +00:00
from flask import (
Blueprint,
render_template,
flash,
redirect,
url_for,
request,
)
2023-04-24 08:30:44 +00:00
from flask_login import login_required, current_user
2023-04-24 08:31:54 +00:00
2023-05-11 08:02:12 +00:00
from app.controllers import (
create_pagination,
create_breadcrumbs,
register_book_verify_route,
book_validator,
)
2023-04-24 08:30:44 +00:00
from app import models as m, db, forms as f
2023-04-21 07:48:06 +00:00
from app.logger import log
2023-04-24 08:31:54 +00:00
bp = Blueprint("book", __name__, url_prefix="/book")
2023-05-11 08:02:12 +00:00
@bp.before_request
def before_request():
if res := book_validator():
return res
@bp.route("/all", methods=["GET"])
2023-04-24 08:31:54 +00:00
def get_all():
q = request.args.get("q", type=str, default=None)
books: m.Book = m.Book.query.order_by(m.Book.id)
2023-04-24 08:31:54 +00:00
if q:
books = books.filter(m.Book.label.like(f"{q}"))
pagination = create_pagination(total=books.count())
return render_template(
2023-04-28 07:41:07 +00:00
"book/all.html",
books=books.paginate(page=pagination.page, per_page=pagination.per_page),
page=pagination,
search_query=q,
all_books=True,
)
@bp.route("/", methods=["GET"])
2023-04-26 11:32:15 +00:00
@login_required
def my_books():
q = request.args.get("q", type=str, default=None)
books: m.Book = m.Book.query.order_by(m.Book.id)
books = books.filter_by(user_id=current_user.id)
if q:
books = books.filter(m.Book.label.like(f"{q}"))
pagination = create_pagination(total=books.count())
2023-04-24 08:31:54 +00:00
return render_template(
2023-04-24 08:36:03 +00:00
"book/index.html",
2023-04-24 08:31:54 +00:00
books=books.paginate(page=pagination.page, per_page=pagination.per_page),
page=pagination,
search_query=q,
)
@bp.route("/create", methods=["POST"])
@login_required
def create():
2023-04-24 06:57:38 +00:00
form = f.CreateBookForm()
2023-04-24 08:31:54 +00:00
if form.validate_on_submit():
book: m.Book = m.Book(label=form.label.data, user_id=current_user.id)
2023-04-24 06:57:38 +00:00
log(log.INFO, "Form submitted. Book: [%s]", book)
2023-04-24 08:31:54 +00:00
book.save()
version = m.BookVersion(semver="1.0.0", book_id=book.id).save()
m.Collection(
label="Root Collection", version_id=version.id, is_root=True
).save()
2023-04-24 07:20:31 +00:00
flash("Book added!", "success")
return redirect(url_for("book.my_books"))
2023-04-24 06:57:38 +00:00
else:
log(log.ERROR, "Book 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(url_for("book.my_books"))
2023-04-24 08:31:54 +00:00
2023-05-01 15:17:46 +00:00
@bp.route("/<int:book_id>/edit", methods=["POST"])
2023-05-11 08:02:12 +00:00
@register_book_verify_route(bp.name)
2023-05-01 15:17:46 +00:00
@login_required
def edit(book_id: int):
form = f.EditBookForm()
if form.validate_on_submit():
book: m.Book = db.session.get(m.Book, book_id)
label = form.label.data
book.label = label
log(log.INFO, "Update Book: [%s]", book)
book.save()
flash("Success!", "success")
2023-05-02 06:48:10 +00:00
return redirect(url_for("book.collection_view", book_id=book_id))
2023-05-01 15:17:46 +00:00
else:
log(log.ERROR, "Book 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")
2023-05-02 06:48:10 +00:00
return redirect(url_for("book.settings", book_id=book_id))
2023-05-01 15:17:46 +00:00
@bp.route("/<int:book_id>/collections", methods=["GET"])
def collection_view(book_id: int):
2023-04-24 14:59:50 +00:00
book = db.session.get(m.Book, book_id)
2023-05-01 14:01:03 +00:00
breadcrumbs = create_breadcrumbs(book_id=book_id, collection_path=())
if not book or book.is_deleted:
2023-04-24 14:59:50 +00:00
log(log.WARNING, "Book with id [%s] not found", book_id)
flash("Book not found", "danger")
return redirect(url_for("book.my_books"))
2023-04-24 14:59:50 +00:00
else:
2023-05-01 14:01:03 +00:00
return render_template(
"book/collection_view.html", book=book, breadcrumbs=breadcrumbs
)
2023-04-24 14:59:50 +00:00
@bp.route("/<int:book_id>/<int:collection_id>/subcollections", methods=["GET"])
def sub_collection_view(book_id: int, collection_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.is_deleted:
2023-04-24 14:59:50 +00:00
log(log.WARNING, "Book with id [%s] not found", book_id)
flash("Book not found", "danger")
return redirect(url_for("book.my_books"))
2023-04-24 14:59:50 +00:00
collection: m.Collection = db.session.get(m.Collection, collection_id)
if not collection or collection.is_deleted:
2023-04-24 14:59:50 +00:00
log(log.WARNING, "Collection with id [%s] not found", collection_id)
2023-04-25 06:36:15 +00:00
flash("Collection not found", "danger")
return redirect(url_for("book.collection_view", book_id=book_id))
2023-05-01 14:01:03 +00:00
breadcrumbs = create_breadcrumbs(book_id=book_id, collection_path=(collection.id,))
2023-04-24 14:59:50 +00:00
if collection.is_leaf:
return redirect(
url_for("book.section_view", book_id=book.id, collection_id=collection.id)
2023-04-24 14:59:50 +00:00
)
else:
return render_template(
2023-05-01 14:01:03 +00:00
"book/sub_collection_view.html",
book=book,
collection=collection,
breadcrumbs=breadcrumbs,
2023-04-24 14:59:50 +00:00
)
@bp.route("/<int:book_id>/<int:collection_id>/sections", methods=["GET"])
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/sections",
methods=["GET"],
)
def section_view(
book_id: int, collection_id: int, sub_collection_id: int | None = None
):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.is_deleted:
2023-04-24 14:59:50 +00:00
log(log.WARNING, "Book with id [%s] not found", book_id)
flash("Book not found", "danger")
return redirect(url_for("book.my_books"))
2023-04-24 14:59:50 +00:00
collection: m.Collection = db.session.get(m.Collection, collection_id)
if not collection or collection.is_deleted:
2023-04-24 14:59:50 +00:00
log(log.WARNING, "Collection with id [%s] not found", collection_id)
2023-04-25 06:36:15 +00:00
flash("Collection not found", "danger")
return redirect(url_for("book.collection_view", book_id=book_id))
sub_collection = None
if sub_collection_id:
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection or sub_collection.is_deleted:
log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id)
flash("Sub_collection not found", "danger")
return redirect(
url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
)
)
if sub_collection:
sections = sub_collection.active_sections
2023-04-24 14:59:50 +00:00
else:
sections = collection.active_sections
breadcrumbs = create_breadcrumbs(
book_id=book_id,
collection_path=(
collection_id,
sub_collection_id,
),
)
return render_template(
"book/section_view.html",
book=book,
collection=collection,
sections=sections,
sub_collection=sub_collection,
breadcrumbs=breadcrumbs,
)
2023-04-24 14:59:50 +00:00
2023-04-25 11:29:40 +00:00
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/interpretations",
methods=["GET"],
)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>/interpretations",
2023-04-25 11:29:40 +00:00
methods=["GET"],
)
def interpretation_view(
book_id: int,
collection_id: int,
section_id: int,
sub_collection_id: int | None = None,
):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.is_deleted:
2023-04-24 14:59:50 +00:00
log(log.WARNING, "Book with id [%s] not found", book_id)
flash("Book not found", "danger")
return redirect(url_for("book.my_books"))
2023-04-24 14:59:50 +00:00
collection: m.Collection = db.session.get(m.Collection, collection_id)
if not collection or collection.is_deleted:
2023-04-24 14:59:50 +00:00
log(log.WARNING, "Collection with id [%s] not found", collection_id)
2023-04-25 06:36:15 +00:00
flash("Collection not found", "danger")
return redirect(url_for("book.collection_view", book_id=book_id))
if sub_collection_id:
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection or sub_collection.is_deleted:
log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id)
flash("Sub_collection not found", "danger")
return redirect(
url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
)
)
2023-04-25 11:29:40 +00:00
section: m.Section = db.session.get(m.Section, section_id)
if not section:
log(log.WARNING, "Section with id [%s] not found", section_id)
flash("Section not found", "danger")
return redirect(
url_for(
"book.section_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
)
)
2023-04-24 14:59:50 +00:00
else:
2023-05-01 14:01:03 +00:00
breadcrumbs = create_breadcrumbs(
book_id=book_id,
collection_path=(
collection_id,
sub_collection_id,
),
section_id=section_id,
)
2023-04-24 14:59:50 +00:00
return render_template(
2023-04-25 11:29:40 +00:00
"book/interpretation_view.html",
2023-04-24 14:59:50 +00:00
book=book,
collection=collection,
sub_collection=sub_collection if sub_collection_id else None,
2023-04-25 11:29:40 +00:00
section=section,
2023-05-01 14:01:03 +00:00
breadcrumbs=breadcrumbs,
2023-04-24 14:59:50 +00:00
)
2023-04-26 08:20:29 +00:00
@bp.route("/<int:book_id>/settings", methods=["GET"])
2023-05-11 08:08:19 +00:00
@register_book_verify_route(bp.name)
2023-04-24 15:02:21 +00:00
@login_required
2023-04-26 08:20:29 +00:00
def settings(book_id: int):
2023-04-24 15:02:21 +00:00
book: m.Book = db.session.get(m.Book, book_id)
return render_template(
"book/settings.html", book=book, roles=m.BookContributor.Roles
)
2023-04-24 08:30:44 +00:00
@bp.route("/<int:book_id>/add_contributor", methods=["POST"])
2023-05-11 08:14:22 +00:00
@register_book_verify_route(bp.name)
2023-04-24 08:30:44 +00:00
@login_required
2023-04-26 08:20:29 +00:00
def add_contributor(book_id: int):
2023-04-24 08:30:44 +00:00
form = f.AddContributorForm()
if form.validate_on_submit():
book_contributor = m.BookContributor.query.filter_by(
user_id=form.user_id.data, book_id=book_id
).first()
if book_contributor:
2023-04-25 11:24:04 +00:00
log(log.INFO, "Contributor: [%s] already exists", book_contributor)
flash("Already exists!", "danger")
return redirect(url_for("book.settings", book_id=book_id))
2023-04-24 08:30:44 +00:00
role = m.BookContributor.Roles(int(form.role.data))
2023-04-25 11:24:04 +00:00
contributor = m.BookContributor(
user_id=form.user_id.data, book_id=book_id, role=role
)
log(log.INFO, "New contributor [%s]", contributor)
contributor.save()
2023-04-24 08:30:44 +00:00
flash("Contributor was added!", "success")
return redirect(url_for("book.settings", book_id=book_id))
2023-04-24 08:30:44 +00:00
else:
log(log.ERROR, "Book 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(url_for("book.settings", book_id=book_id))
2023-04-25 08:56:51 +00:00
@bp.route("/<int:book_id>/delete_contributor", methods=["POST"])
2023-05-11 08:14:22 +00:00
@register_book_verify_route(bp.name)
2023-04-25 08:56:51 +00:00
@login_required
2023-04-26 08:20:29 +00:00
def delete_contributor(book_id: int):
2023-04-25 08:56:51 +00:00
form = f.DeleteContributorForm()
if form.validate_on_submit():
book_contributor = m.BookContributor.query.filter_by(
2023-05-11 08:14:22 +00:00
user_id=int(form.user_id.data), book_id=book_id
2023-04-25 08:56:51 +00:00
).first()
if not book_contributor:
2023-04-25 11:24:04 +00:00
log(
log.INFO,
"BookContributor does not exists user: [%s], book: [%s]",
form.user_id.data,
2023-05-11 08:14:22 +00:00
book_id,
2023-04-25 11:24:04 +00:00
)
2023-04-25 08:56:51 +00:00
flash("Does not exists!", "success")
return redirect(url_for("book.settings", book_id=book_id))
2023-04-25 11:24:04 +00:00
log(log.INFO, "Delete BookContributor [%s]", book_contributor)
2023-04-25 08:56:51 +00:00
db.session.delete(book_contributor)
db.session.commit()
flash("Success!", "success")
return redirect(url_for("book.settings", book_id=book_id))
else:
log(log.ERROR, "Book 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(url_for("book.settings", book_id=book_id))
2023-04-25 09:39:26 +00:00
@bp.route("/<int:book_id>/edit_contributor_role", methods=["POST"])
2023-05-11 08:14:22 +00:00
@register_book_verify_route(bp.name)
2023-04-25 09:39:26 +00:00
@login_required
2023-04-26 08:20:29 +00:00
def edit_contributor_role(book_id: int):
2023-04-25 09:39:26 +00:00
form = f.EditContributorRoleForm()
if form.validate_on_submit():
book_contributor = m.BookContributor.query.filter_by(
2023-05-11 08:14:22 +00:00
user_id=int(form.user_id.data), book_id=book_id
2023-04-25 09:39:26 +00:00
).first()
if not book_contributor:
2023-04-25 11:24:04 +00:00
log(
log.INFO,
"BookContributor does not exists user: [%s], book: [%s]",
form.user_id.data,
2023-05-11 08:14:22 +00:00
book_id,
2023-04-25 11:24:04 +00:00
)
2023-04-25 09:39:26 +00:00
flash("Does not exists!", "success")
return redirect(url_for("book.settings", book_id=book_id))
role = m.BookContributor.Roles(int(form.role.data))
book_contributor.role = role
2023-04-25 11:24:04 +00:00
log(
log.INFO,
"Update contributor [%s] role: new role: [%s]",
book_contributor,
role,
)
2023-04-25 09:39:26 +00:00
book_contributor.save()
flash("Success!", "success")
return redirect(url_for("book.settings", book_id=book_id))
else:
log(log.ERROR, "Book 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(url_for("book.settings", book_id=book_id))
2023-04-26 13:14:57 +00:00
2023-04-27 12:45:13 +00:00
###############################
# Collection/SubCollection CRUD
###############################
2023-04-26 13:14:57 +00:00
@bp.route("/<int:book_id>/create_collection", methods=["POST"])
2023-04-27 12:45:13 +00:00
@bp.route("/<int:book_id>/<int:collection_id>/create_sub_collection", methods=["POST"])
2023-05-11 09:32:59 +00:00
@register_book_verify_route(bp.name)
2023-04-26 13:14:57 +00:00
@login_required
2023-04-27 12:45:13 +00:00
def collection_create(book_id: int, collection_id: int | None = None):
2023-04-26 13:14:57 +00:00
book: m.Book = db.session.get(m.Book, book_id)
2023-05-11 09:32:59 +00:00
redirect_url = url_for("book.collection_view", book_id=book_id)
2023-04-27 12:45:13 +00:00
if collection_id:
collection: m.Collection = db.session.get(m.Collection, collection_id)
2023-05-11 09:32:59 +00:00
if collection.is_leaf:
2023-04-27 12:45:13 +00:00
log(log.WARNING, "Collection with id [%s] is leaf", collection_id)
flash("You can't create subcollection for this collection", "danger")
return redirect(
url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
)
)
redirect_url = url_for(
"book.sub_collection_view", book_id=book_id, collection_id=collection_id
)
2023-04-26 13:14:57 +00:00
form = f.CreateCollectionForm()
if form.validate_on_submit():
label = form.label.data
collection: m.Collection = m.Collection.query.filter_by(
2023-04-28 07:09:42 +00:00
is_deleted=False,
label=label,
)
if collection_id:
2023-04-28 07:10:55 +00:00
collection = collection.filter_by(parent_id=collection_id)
2023-04-28 07:09:42 +00:00
else:
collection = collection.filter_by(
2023-04-28 07:10:55 +00:00
parent_id=book.versions[-1].root_collection.id
2023-04-28 07:09:42 +00:00
)
collection = collection.first()
2023-04-26 13:14:57 +00:00
if collection:
log(
log.INFO,
"Collection with similar label already exists. Book: [%s], Collection: [%s], Label: [%s]",
book.id,
collection.id,
label,
)
flash("Collection label must be unique!", "danger")
2023-04-27 12:45:13 +00:00
return redirect(redirect_url)
2023-04-26 13:14:57 +00:00
collection: m.Collection = m.Collection(
label=label,
about=form.about.data,
2023-04-28 07:10:55 +00:00
parent_id=book.versions[-1].root_collection.id,
2023-04-26 13:14:57 +00:00
)
2023-04-27 12:45:13 +00:00
if collection_id:
2023-04-28 07:10:55 +00:00
collection.parent_id = collection_id
2023-04-27 12:45:13 +00:00
collection.is_leaf = True
2023-04-26 13:14:57 +00:00
log(log.INFO, "Create collection [%s]. Book: [%s]", collection, book.id)
collection.save()
flash("Success!", "success")
if collection_id:
redirect_url = url_for(
"book.sub_collection_view", book_id=book_id, collection_id=collection_id
)
2023-04-27 12:45:13 +00:00
return redirect(redirect_url)
2023-04-26 13:14:57 +00:00
else:
2023-04-27 12:45:13 +00:00
log(log.ERROR, "Collection/Subcollection create errors: [%s]", form.errors)
2023-04-26 13:14:57 +00:00
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")
2023-04-27 12:45:13 +00:00
return redirect(redirect_url)
2023-04-26 13:14:57 +00:00
@bp.route("/<int:book_id>/<int:collection_id>/edit", methods=["POST"])
2023-04-27 13:35:55 +00:00
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/edit", methods=["POST"]
)
2023-05-11 09:32:59 +00:00
@register_book_verify_route(bp.name)
2023-04-26 13:14:57 +00:00
@login_required
2023-04-27 13:35:55 +00:00
def collection_edit(
book_id: int, collection_id: int, sub_collection_id: int | None = None
):
2023-04-26 13:14:57 +00:00
book: m.Book = db.session.get(m.Book, book_id)
collection: m.Collection = db.session.get(m.Collection, collection_id)
2023-04-27 13:35:55 +00:00
if sub_collection_id:
2023-05-11 09:32:59 +00:00
collection = db.session.get(m.Collection, sub_collection_id)
2023-04-26 13:14:57 +00:00
form = f.EditCollectionForm()
redirect_url = url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
)
if form.validate_on_submit():
label = form.label.data
2023-04-28 07:09:42 +00:00
collection_query: m.Collection = m.Collection.query.filter_by(
is_deleted=False,
label=label,
2023-05-11 09:32:59 +00:00
).filter(m.Collection.id != collection.id)
2023-04-28 07:09:42 +00:00
if sub_collection_id:
2023-04-28 07:10:55 +00:00
collection_query = collection_query.filter_by(parent_id=collection_id)
2023-04-28 07:09:42 +00:00
else:
collection_query = collection_query.filter_by(
2023-05-11 09:32:59 +00:00
parent_id=collection.parent.id
2023-04-28 07:09:42 +00:00
)
2023-04-26 13:14:57 +00:00
2023-04-28 07:09:42 +00:00
if collection_query.first():
2023-04-26 13:14:57 +00:00
log(
log.INFO,
"Collection with similar label already exists. Book: [%s], Collection: [%s], Label: [%s]",
book.id,
2023-05-11 09:32:59 +00:00
collection_id,
2023-04-26 13:14:57 +00:00
label,
)
flash("Collection label must be unique!", "danger")
return redirect(redirect_url)
if label:
2023-05-11 09:32:59 +00:00
collection.label = label
2023-04-26 13:14:57 +00:00
about = form.about.data
if about:
2023-05-11 09:32:59 +00:00
collection.about = about
2023-04-26 13:14:57 +00:00
2023-05-11 09:32:59 +00:00
log(log.INFO, "Edit collection [%s]", collection.id)
collection.save()
2023-04-26 13:14:57 +00:00
flash("Success!", "success")
if sub_collection_id:
redirect_url = url_for(
"book.section_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
)
2023-04-26 13:14:57 +00:00
return redirect(redirect_url)
else:
2023-04-27 13:35:55 +00:00
log(log.ERROR, "Collection edit errors: [%s]", form.errors)
2023-04-26 13:14:57 +00:00
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)
@bp.route("/<int:book_id>/<int:collection_id>/delete", methods=["POST"])
2023-04-27 13:35:55 +00:00
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/delete",
methods=["POST"],
)
2023-05-11 09:32:59 +00:00
@register_book_verify_route(bp.name)
2023-04-26 13:14:57 +00:00
@login_required
2023-04-27 13:35:55 +00:00
def collection_delete(
book_id: int, collection_id: int, sub_collection_id: int | None = None
):
2023-04-26 13:14:57 +00:00
collection: m.Collection = db.session.get(m.Collection, collection_id)
2023-04-27 13:35:55 +00:00
if sub_collection_id:
2023-05-11 09:32:59 +00:00
collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
2023-04-26 13:14:57 +00:00
2023-05-11 09:32:59 +00:00
collection.is_deleted = True
2023-04-26 13:14:57 +00:00
2023-05-11 09:32:59 +00:00
log(log.INFO, "Delete collection [%s]", collection.id)
collection.save()
2023-04-26 13:14:57 +00:00
flash("Success!", "success")
return redirect(
url_for(
"book.collection_view",
book_id=book_id,
)
)
2023-05-02 14:07:37 +00:00
###############
# Sections CRUD
###############
@bp.route("/<int:book_id>/<int:collection_id>/create_section", methods=["POST"])
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/create_section",
methods=["POST"],
)
2023-05-11 09:39:50 +00:00
@register_book_verify_route(bp.name)
2023-05-02 14:07:37 +00:00
@login_required
def section_create(
book_id: int, collection_id: int, sub_collection_id: int | None = None
):
book: m.Book = db.session.get(m.Book, book_id)
collection: m.Collection = db.session.get(m.Collection, collection_id)
2023-05-11 09:50:20 +00:00
sub_collection = None
if sub_collection_id:
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
2023-05-02 14:07:37 +00:00
redirect_url = url_for("book.collection_view", book_id=book_id)
if collection_id:
redirect_url = url_for(
2023-05-02 14:38:27 +00:00
"book.section_view",
2023-05-02 14:07:37 +00:00
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
)
form = f.CreateSectionForm()
if form.validate_on_submit():
section: m.Section = m.Section(
label=form.label.data,
about=form.about.data,
collection_id=sub_collection_id or collection_id,
version_id=book.last_version.id,
)
2023-05-02 14:38:27 +00:00
if sub_collection:
sub_collection.is_leaf = True
else:
collection.is_leaf = True
2023-05-02 14:07:37 +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)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/edit_section", methods=["POST"]
)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>/edit_section",
methods=["POST"],
)
2023-05-11 09:39:50 +00:00
@register_book_verify_route(bp.name)
2023-05-02 14:07:37 +00:00
@login_required
def section_edit(
book_id: int,
collection_id: int,
section_id: int,
sub_collection_id: int | None = None,
):
redirect_url = url_for(
2023-05-04 06:56:09 +00:00
"book.interpretation_view",
2023-05-02 14:07:37 +00:00
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
2023-05-04 06:56:09 +00:00
section_id=section_id,
2023-05-02 14:07:37 +00:00
)
section: m.Section = db.session.get(m.Section, section_id)
form = f.EditSectionForm()
if form.validate_on_submit():
label = form.label.data
if label:
section.label = label
about = form.about.data
if about:
section.about = about
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)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/delete_section",
methods=["POST"],
)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>/delete_section",
methods=["POST"],
)
2023-05-11 09:39:50 +00:00
@register_book_verify_route(bp.name)
2023-05-02 14:07:37 +00:00
@login_required
def section_delete(
book_id: int,
collection_id: int,
section_id: int,
sub_collection_id: int | None = None,
):
2023-05-11 09:39:50 +00:00
collection: m.Collection = db.session.get(
m.Collection, sub_collection_id or collection_id
)
2023-05-02 14:07:37 +00:00
section: m.Section = db.session.get(m.Section, section_id)
section.is_deleted = True
2023-05-11 09:39:50 +00:00
if not collection.active_sections:
log(
log.INFO,
"Section [%s] has no active section. Set is_leaf = False",
section.id,
)
2023-05-11 09:39:50 +00:00
collection.is_leaf = False
2023-05-02 14:07:37 +00:00
log(log.INFO, "Delete section [%s]", section.id)
section.save()
flash("Success!", "success")
return redirect(
url_for(
"book.collection_view",
book_id=book_id,
)
)
2023-05-04 12:14:17 +00:00
2023-05-04 13:57:47 +00:00
#####################
2023-05-04 12:14:17 +00:00
# Interpretation CRUD
2023-05-04 13:57:47 +00:00
#####################
2023-05-04 12:14:17 +00:00
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/create_interpretation",
methods=["POST"],
)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>/create_interpretation",
methods=["POST"],
)
2023-05-11 09:50:20 +00:00
@register_book_verify_route(bp.name)
2023-05-04 12:14:17 +00:00
@login_required
def interpretation_create(
book_id: int,
collection_id: int,
section_id: int,
sub_collection_id: int | None = None,
):
2023-05-05 09:02:35 +00:00
section: m.Section = db.session.get(m.Section, section_id)
2023-05-11 09:55:49 +00:00
form = f.CreateInterpretationForm()
2023-05-04 12:14:17 +00:00
redirect_url = url_for(
2023-05-05 09:02:35 +00:00
"book.interpretation_view",
2023-05-04 12:14:17 +00:00
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
2023-05-05 09:02:35 +00:00
section_id=section.id,
2023-05-04 12:14:17 +00:00
)
if form.validate_on_submit():
interpretation: m.Interpretation = m.Interpretation(
label=form.label.data,
text=form.text.data,
section_id=section_id,
2023-05-05 13:42:20 +00:00
user_id=current_user.id,
2023-05-04 12:14:17 +00:00
)
log(
log.INFO,
"Create interpretation [%s]. Section: [%s]",
interpretation,
section,
)
interpretation.save()
flash("Success!", "success")
return redirect(redirect_url)
else:
log(log.ERROR, "Interpretation 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-04 13:32:56 +00:00
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/edit_interpretation",
methods=["POST"],
)
@bp.route(
(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/"
"<int:section_id>/<int:interpretation_id>/edit_interpretation"
),
methods=["POST"],
)
2023-05-11 09:50:20 +00:00
@register_book_verify_route(bp.name)
2023-05-04 13:32:56 +00:00
@login_required
def interpretation_edit(
book_id: int,
collection_id: int,
section_id: int,
interpretation_id: int,
sub_collection_id: int | None = None,
):
2023-05-11 09:55:49 +00:00
interpretation: m.Interpretation = db.session.get(
m.Interpretation, interpretation_id
)
form = f.EditInterpretationForm()
2023-05-04 13:32:56 +00:00
redirect_url = url_for(
2023-05-05 16:51:55 +00:00
"book.qa_view",
2023-05-04 13:32:56 +00:00
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
section_id=section_id,
2023-05-05 16:51:55 +00:00
interpretation_id=interpretation_id,
2023-05-04 13:32:56 +00:00
)
if form.validate_on_submit():
label = form.label.data
if label:
interpretation.label = label
interpretation.text = form.text.data
log(log.INFO, "Edit interpretation [%s]", interpretation.id)
interpretation.save()
flash("Success!", "success")
return redirect(redirect_url)
else:
log(log.ERROR, "Interpretation 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-04 13:57:47 +00:00
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/delete_interpretation",
methods=["POST"],
)
@bp.route(
(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/"
"<int:section_id>/<int:interpretation_id>/delete_interpretation"
),
methods=["POST"],
)
2023-05-11 09:50:20 +00:00
@register_book_verify_route(bp.name)
2023-05-04 13:57:47 +00:00
@login_required
def interpretation_delete(
book_id: int,
collection_id: int,
section_id: int,
interpretation_id: int,
sub_collection_id: int | None = None,
):
interpretation: m.Interpretation = db.session.get(
m.Interpretation, interpretation_id
)
interpretation.is_deleted = True
log(log.INFO, "Delete interpretation [%s]", interpretation)
interpretation.save()
flash("Success!", "success")
return redirect(
url_for(
"book.collection_view",
book_id=book_id,
)
)
2023-05-05 16:51:55 +00:00
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/preview",
2023-05-09 14:45:48 +00:00
methods=["GET"],
2023-05-05 16:51:55 +00:00
)
@bp.route(
(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/"
"<int:section_id>/<int:interpretation_id>/preview"
),
2023-05-09 14:45:48 +00:00
methods=["GET"],
2023-05-05 16:51:55 +00:00
)
@login_required
def qa_view(
book_id: int,
collection_id: int,
section_id: int,
interpretation_id: int,
sub_collection_id: int | None = None,
):
book: m.Book = db.session.get(m.Book, book_id)
2023-05-09 14:45:48 +00:00
if not book or book.is_deleted:
2023-05-05 16:51:55 +00:00
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
flash("You are not owner of this book!", "danger")
return redirect(url_for("book.my_books"))
collection: m.Collection = db.session.get(m.Collection, collection_id)
if not collection or collection.is_deleted:
log(log.WARNING, "Collection with id [%s] not found", collection_id)
flash("Collection not found", "danger")
return redirect(url_for("book.collection_view", book_id=book_id))
if sub_collection_id:
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection or sub_collection.is_deleted:
log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id)
flash("SubCollection not found", "danger")
return redirect(
url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
)
)
redirect_url = url_for(
"book.interpretation_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
section_id=section_id,
)
section: m.Section = db.session.get(m.Section, section_id)
if not section or section.is_deleted:
log(log.WARNING, "Section with id [%s] not found", section_id)
flash("Section not found", "danger")
return redirect(redirect_url)
interpretation: m.Interpretation = db.session.get(
m.Interpretation, interpretation_id
)
if not interpretation or interpretation.is_deleted:
log(log.WARNING, "Interpretation with id [%s] not found", interpretation_id)
flash("Interpretation not found", "danger")
return redirect(redirect_url)
breadcrumbs = create_breadcrumbs(
book_id=book_id,
collection_path=(
collection_id,
sub_collection_id,
),
section_id=section_id,
interpretation_id=interpretation.id,
)
return render_template(
"book/qa_view.html",
book=book,
collection=collection,
sub_collection=sub_collection if sub_collection_id else None,
section=section,
interpretation=interpretation,
breadcrumbs=breadcrumbs,
)
2023-05-08 12:19:22 +00:00
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/preview/create_comment",
methods=["POST"],
)
@bp.route(
(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/"
"<int:section_id>/<int:interpretation_id>/preview/create_comment"
),
methods=["POST"],
)
@login_required
def create_comment(
book_id: int,
collection_id: int,
section_id: int,
interpretation_id: int,
sub_collection_id: int | None = None,
):
book: m.Book = db.session.get(m.Book, book_id)
2023-05-09 14:45:48 +00:00
if not book or book.is_deleted:
2023-05-08 12:19:22 +00:00
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
flash("You are not owner of this book!", "danger")
return redirect(url_for("book.my_books"))
collection: m.Collection = db.session.get(m.Collection, collection_id)
if not collection or collection.is_deleted:
log(log.WARNING, "Collection with id [%s] not found", collection_id)
flash("Collection not found", "danger")
return redirect(url_for("book.collection_view", book_id=book_id))
sub_collection = None
if sub_collection_id:
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection or sub_collection.is_deleted:
log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id)
flash("SubCollection not found", "danger")
return redirect(
url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
)
)
breadcrumbs = create_breadcrumbs(
book_id=book_id,
collection_path=(
collection_id,
sub_collection_id,
),
section_id=section_id,
interpretation_id=interpretation_id,
)
redirect_url = url_for(
"book.qa_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
section_id=section_id,
interpretation_id=interpretation_id,
breadcrumbs=breadcrumbs,
)
section: m.Section = db.session.get(m.Section, section_id)
if not section or section.is_deleted:
log(log.WARNING, "Section with id [%s] not found", section_id)
flash("Section not found", "danger")
return redirect(redirect_url)
interpretation: m.Interpretation = db.session.get(
m.Interpretation, interpretation_id
)
if not interpretation or interpretation.is_deleted:
log(log.WARNING, "Interpretation with id [%s] not found", interpretation_id)
flash("Interpretation not found", "danger")
return redirect(redirect_url)
form = f.CreateCommentForm()
if form.validate_on_submit():
comment: m.Comment = m.Comment(
text=form.text.data,
user_id=current_user.id,
interpretation_id=interpretation_id,
)
2023-05-09 08:37:23 +00:00
if form.parent_id.data:
comment.parent_id = form.parent_id.data
comment.interpretation = None
2023-05-08 12:19:22 +00:00
log(
log.INFO,
"Create comment for interpretation [%s]. Section: [%s]",
interpretation,
section,
)
comment.save()
flash("Success!", "success")
return redirect(redirect_url)
else:
log(log.ERROR, "Comment 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.lower().replace("field", field_label).title(), "danger")
return redirect(redirect_url)
2023-05-09 08:37:23 +00:00
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/comment_delete",
methods=["POST"],
)
@bp.route(
(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/"
"<int:section_id>/<int:interpretation_id>/comment_delete"
),
methods=["POST"],
)
@login_required
def comment_delete(
book_id: int,
collection_id: int,
section_id: int,
interpretation_id: int,
sub_collection_id: int | None = None,
):
2023-05-09 10:01:05 +00:00
book: m.Book = db.session.get(m.Book, book_id)
2023-05-09 08:37:23 +00:00
2023-05-09 14:45:48 +00:00
if not book or book.is_deleted:
2023-05-09 10:01:05 +00:00
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
flash("You are not owner of this book!", "danger")
return redirect(url_for("book.my_books"))
2023-05-09 08:37:23 +00:00
2023-05-09 10:01:05 +00:00
collection: m.Collection = db.session.get(m.Collection, collection_id)
if not collection or collection.is_deleted:
log(log.WARNING, "Collection with id [%s] not found", collection_id)
flash("Collection not found", "danger")
return redirect(url_for("book.collection_view", book_id=book_id))
if sub_collection_id:
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection or sub_collection.is_deleted:
log(
log.WARNING,
"Sub_collection with id [%s] not found",
sub_collection_id,
2023-05-09 08:37:23 +00:00
)
2023-05-09 10:01:05 +00:00
flash("SubCollection not found", "danger")
return redirect(
url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
2023-05-09 08:37:23 +00:00
)
2023-05-09 10:01:05 +00:00
)
2023-05-09 08:37:23 +00:00
2023-05-09 10:01:05 +00:00
redirect_url = url_for(
"book.qa_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
section_id=section_id,
interpretation_id=interpretation_id,
)
section: m.Section = db.session.get(m.Section, section_id)
if not section or section.is_deleted:
log(log.WARNING, "Section with id [%s] not found", section_id)
flash("Section not found", "danger")
return redirect(redirect_url)
2023-05-09 08:37:23 +00:00
2023-05-09 10:01:05 +00:00
interpretation: m.Interpretation = db.session.get(
m.Interpretation, interpretation_id
)
if not interpretation or interpretation.is_deleted:
log(log.WARNING, "Interpretation with id [%s] not found", interpretation_id)
flash("Interpretation not found", "danger")
return redirect(redirect_url)
2023-05-09 08:37:23 +00:00
2023-05-09 10:01:05 +00:00
form = f.DeleteCommentForm()
comment_id = form.comment_id.data
comment: m.Comment = db.session.get(m.Comment, comment_id)
if not comment or comment.is_deleted:
log(log.WARNING, "Comment with id [%s] not found", comment_id)
flash("Comment not found", "danger")
return redirect(redirect_url)
2023-05-09 08:37:23 +00:00
2023-05-09 10:01:05 +00:00
if form.validate_on_submit():
2023-05-09 08:37:23 +00:00
comment.is_deleted = True
log(log.INFO, "Delete comment [%s]", comment)
comment.save()
flash("Success!", "success")
return redirect(redirect_url)
return redirect(
url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
)
)
2023-05-09 14:32:07 +00:00
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/comment_edit",
methods=["POST"],
)
@bp.route(
(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/"
"<int:section_id>/<int:interpretation_id>/comment_edit"
),
methods=["POST"],
)
@login_required
def comment_edit(
book_id: int,
collection_id: int,
section_id: int,
interpretation_id: int,
sub_collection_id: int | None = None,
):
book: m.Book = db.session.get(m.Book, book_id)
2023-05-09 14:45:48 +00:00
if not book or book.is_deleted:
2023-05-09 14:32:07 +00:00
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
flash("You are not owner of this book!", "danger")
return redirect(url_for("book.my_books"))
collection: m.Collection = db.session.get(m.Collection, collection_id)
if not collection or collection.is_deleted:
log(log.WARNING, "Collection with id [%s] not found", collection_id)
flash("Collection not found", "danger")
return redirect(url_for("book.collection_view", book_id=book_id))
if sub_collection_id:
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection or sub_collection.is_deleted:
log(
log.WARNING,
"Sub_collection with id [%s] not found",
sub_collection_id,
)
flash("SubCollection not found", "danger")
return redirect(
url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
)
)
redirect_url = url_for(
"book.qa_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
section_id=section_id,
interpretation_id=interpretation_id,
)
section: m.Section = db.session.get(m.Section, section_id)
if not section or section.is_deleted:
log(log.WARNING, "Section with id [%s] not found", section_id)
flash("Section not found", "danger")
return redirect(redirect_url)
interpretation: m.Interpretation = db.session.get(
m.Interpretation, interpretation_id
)
if not interpretation or interpretation.is_deleted:
log(log.WARNING, "Interpretation with id [%s] not found", interpretation_id)
flash("Interpretation not found", "danger")
return redirect(redirect_url)
form = f.EditCommentForm()
comment_id = form.comment_id.data
comment: m.Comment = db.session.get(m.Comment, comment_id)
if not comment or comment.is_deleted:
log(log.WARNING, "Comment with id [%s] not found", comment_id)
flash("Comment not found", "danger")
return redirect(redirect_url)
if form.validate_on_submit():
comment.text = form.text.data
comment.edited = True
log(log.INFO, "Delete comment [%s]", comment)
comment.save()
flash("Success!", "success")
return redirect(redirect_url)
return redirect(
url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
)
)