open-law/app/views/book.py

473 lines
17 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
from app.controllers import create_pagination
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")
@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(
"book/index.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()
2023-04-24 07:20:31 +00:00
m.BookVersion(semver="1.0.0", book_id=book.id).save()
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
@bp.route("/<int:book_id>", methods=["GET"])
def collection_view(book_id: int):
2023-04-24 14:59:50 +00:00
book = db.session.get(m.Book, book_id)
if not book:
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:
return render_template("book/collection_view.html", book=book)
@bp.route("/<int:book_id>/<int:collection_id>", methods=["GET"])
def sub_collection_view(book_id: int, collection_id: int):
book: m.Book = db.session.get(m.Book, book_id)
2023-04-24 14:59:50 +00:00
if not book:
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:
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-04-24 14:59:50 +00:00
if collection.is_leaf:
return render_template(
"book/section_view.html",
book=book,
collection=collection,
sub_collection=collection,
)
else:
return render_template(
"book/sub_collection_view.html", book=book, collection=collection
)
2023-04-25 11:29:40 +00:00
@bp.route("/<int:book_id>/<int:collection_id>/<int:sub_collection_id>", methods=["GET"])
def section_view(book_id: int, collection_id: int, sub_collection_id: int):
book: m.Book = db.session.get(m.Book, book_id)
2023-04-24 14:59:50 +00:00
if not book:
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:
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-04-25 11:29:40 +00:00
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection:
2023-04-24 14:59:50 +00:00
log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id)
2023-04-25 06:36:15 +00:00
flash("Sub_collection not found", "danger")
return redirect(
url_for(
"book.sub_collection_view", book_id=book_id, collection_id=collection_id
)
)
2023-04-24 14:59:50 +00:00
else:
return render_template(
2023-04-25 11:29:40 +00:00
"book/section_view.html",
2023-04-24 14:59:50 +00:00
book=book,
collection=collection,
sub_collection=sub_collection,
)
2023-04-25 11:29:40 +00:00
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>",
methods=["GET"],
)
def interpretation_view(
book_id: int, collection_id: int, sub_collection_id: int, section_id: int
):
book: m.Book = db.session.get(m.Book, book_id)
2023-04-24 14:59:50 +00:00
if not book:
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:
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-04-24 14:59:50 +00:00
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection:
2023-04-24 14:59:50 +00:00
log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id)
2023-04-25 06:36:15 +00:00
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:
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,
2023-04-25 11:29:40 +00:00
section=section,
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-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)
2023-04-26 11:33:49 +00:00
if book.owner != current_user:
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-04-24 15:02:21 +00:00
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"])
@login_required
2023-04-26 08:20:29 +00:00
def add_contributor(book_id: int):
2023-04-24 08:30:44 +00:00
book: m.Book = db.session.get(m.Book, book_id)
2023-04-25 11:24:04 +00:00
if not book or book.owner != current_user:
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
2023-04-24 08:30:44 +00:00
flash("You are not owner of this book!", "danger")
return redirect(url_for("book.my_books"))
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"])
@login_required
2023-04-26 08:20:29 +00:00
def delete_contributor(book_id: int):
2023-04-25 08:56:51 +00:00
book: m.Book = db.session.get(m.Book, book_id)
2023-04-25 11:24:04 +00:00
if not book or book.owner != current_user:
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
2023-04-25 08:56:51 +00:00
flash("You are not owner of this book!", "danger")
return redirect(url_for("book.my_books"))
2023-04-25 08:56:51 +00:00
form = f.DeleteContributorForm()
if form.validate_on_submit():
book_contributor = m.BookContributor.query.filter_by(
user_id=int(form.user_id.data), book_id=book.id
).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,
book.id,
)
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"])
@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
book: m.Book = db.session.get(m.Book, book_id)
2023-04-25 11:24:04 +00:00
if not book or book.owner != current_user:
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
2023-04-25 09:39:26 +00:00
flash("You are not owner of this book!", "danger")
return redirect(url_for("book.my_books"))
2023-04-25 09:39:26 +00:00
form = f.EditContributorRoleForm()
if form.validate_on_submit():
book_contributor = m.BookContributor.query.filter_by(
user_id=int(form.user_id.data), book_id=book.id
).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,
book.id,
)
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
#################
# Collection CRUD
#################
@bp.route("/<int:book_id>/create_collection", methods=["POST"])
@login_required
def collection_create(book_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.owner != current_user or book.is_deleted:
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"))
form = f.CreateCollectionForm()
if form.validate_on_submit():
label = form.label.data
collection: m.Collection = m.Collection.query.filter_by(
2023-04-26 14:57:40 +00:00
is_deleted=False, label=label, version_id=book.versions[-1].id
2023-04-26 13:14:57 +00:00
).first()
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")
return redirect(url_for("book.collection_view", book_id=book_id))
collection: m.Collection = m.Collection(
label=label, about=form.about.data, version_id=book.versions[-1].id
)
log(log.INFO, "Create collection [%s]. Book: [%s]", collection, book.id)
collection.save()
flash("Success!", "success")
return redirect(url_for("book.collection_view", 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))
@bp.route("/<int:book_id>/<int:collection_id>/edit", methods=["POST"])
@login_required
def collection_edit(book_id: int, collection_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.owner != current_user or book.is_deleted:
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))
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
if (
2023-04-26 14:57:40 +00:00
m.Collection.query.filter_by(label=label, version_id=book.versions[-1].id)
.filter(m.Collection.id != collection_id)
.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,
collection.id,
label,
)
flash("Collection label must be unique!", "danger")
return redirect(redirect_url)
if label:
collection.label = label
about = form.about.data
if about:
collection.about = about
log(log.INFO, "Edit collection [%s]", collection.id)
collection.save()
flash("Success!", "success")
return redirect(redirect_url)
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(redirect_url)
@bp.route("/<int:book_id>/<int:collection_id>/delete", methods=["POST"])
@login_required
def collection_delete(book_id: int, collection_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.owner != current_user:
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))
collection.is_deleted = True
log(log.INFO, "Delete collection [%s]", collection.id)
collection.save()
flash("Success!", "success")
return redirect(
url_for(
"book.collection_view",
book_id=book_id,
)
)