2023-04-25 11:56:51 +03:00
|
|
|
from flask import Blueprint, render_template, flash, redirect, url_for
|
2023-04-24 11:30:44 +03:00
|
|
|
from flask_login import login_required, current_user
|
2023-04-21 10:48:06 +03:00
|
|
|
|
2023-04-24 11:30:44 +03:00
|
|
|
from app import models as m, db, forms as f
|
2023-04-21 10:48:06 +03:00
|
|
|
from app.logger import log
|
|
|
|
|
2023-04-24 09:57:38 +03:00
|
|
|
bp = Blueprint("book", __name__, url_prefix="/book")
|
2023-04-21 10:48:06 +03:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/", methods=["GET"])
|
|
|
|
def get_all():
|
2023-04-21 17:02:43 +03:00
|
|
|
# q = request.args.get("q", type=str, default=None)
|
|
|
|
# books = m.Book.query.order_by(m.Book.id)
|
|
|
|
# if q:
|
|
|
|
# books = books.filter(m.Book.label.like(f"{q}"))
|
|
|
|
|
|
|
|
# pagination = create_pagination(total=books.count())
|
|
|
|
|
|
|
|
# return render_template(
|
|
|
|
# "books/index.html",
|
|
|
|
# books=books.paginate(page=pagination.page, per_page=pagination.per_page),
|
|
|
|
# page=pagination,
|
|
|
|
# search_query=q,
|
|
|
|
# )
|
2023-04-21 10:48:06 +03:00
|
|
|
return render_template(
|
2023-04-24 18:02:21 +03:00
|
|
|
"book/index.html",
|
2023-04-21 10:48:06 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/create", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
def create():
|
2023-04-24 09:57:38 +03:00
|
|
|
form = f.CreateBookForm()
|
2023-04-21 10:48:06 +03:00
|
|
|
if form.validate_on_submit():
|
|
|
|
book = m.Book(
|
|
|
|
label=form.label.data,
|
|
|
|
)
|
2023-04-24 09:57:38 +03:00
|
|
|
log(log.INFO, "Form submitted. Book: [%s]", book)
|
2023-04-21 10:48:06 +03:00
|
|
|
book.save()
|
2023-04-24 10:20:31 +03:00
|
|
|
m.BookVersion(semver="1.0.0", book_id=book.id).save()
|
|
|
|
|
|
|
|
flash("Book added!", "success")
|
2023-04-24 09:57:38 +03:00
|
|
|
return redirect(url_for("book.get_all"))
|
|
|
|
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.get_all"))
|
2023-04-24 11:30:44 +03:00
|
|
|
|
|
|
|
|
2023-04-26 11:20:29 +03:00
|
|
|
@bp.route("/<int:book_id>/settings", methods=["GET"])
|
2023-04-24 18:02:21 +03:00
|
|
|
@login_required
|
2023-04-26 11:20:29 +03:00
|
|
|
def settings(book_id: int):
|
2023-04-24 18:02:21 +03: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 11:30:44 +03:00
|
|
|
@bp.route("/<int:book_id>/add_contributor", methods=["POST"])
|
|
|
|
@login_required
|
2023-04-26 11:20:29 +03:00
|
|
|
def add_contributor(book_id: int):
|
2023-04-24 11:30:44 +03:00
|
|
|
book: m.Book = db.session.get(m.Book, book_id)
|
2023-04-25 14:24:04 +03: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 11:30:44 +03:00
|
|
|
flash("You are not owner of this book!", "danger")
|
|
|
|
return redirect(url_for("book.get_all"))
|
|
|
|
|
|
|
|
form = f.AddContributorForm()
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2023-04-25 11:20:37 +03:00
|
|
|
book_contributor = m.BookContributor.query.filter_by(
|
|
|
|
user_id=form.user_id.data, book_id=book_id
|
|
|
|
).first()
|
|
|
|
if book_contributor:
|
2023-04-25 14:24:04 +03:00
|
|
|
log(log.INFO, "Contributor: [%s] already exists", book_contributor)
|
2023-04-25 11:20:37 +03:00
|
|
|
flash("Already exists!", "danger")
|
|
|
|
return redirect(url_for("book.settings", book_id=book_id))
|
|
|
|
|
2023-04-24 11:30:44 +03:00
|
|
|
role = m.BookContributor.Roles(int(form.role.data))
|
2023-04-25 14:24:04 +03: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 11:30:44 +03:00
|
|
|
|
|
|
|
flash("Contributor was added!", "success")
|
2023-04-25 11:20:37 +03:00
|
|
|
return redirect(url_for("book.settings", book_id=book_id))
|
2023-04-24 11:30:44 +03: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-04-25 11:20:37 +03:00
|
|
|
return redirect(url_for("book.settings", book_id=book_id))
|
2023-04-25 11:56:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/<int:book_id>/delete_contributor", methods=["POST"])
|
|
|
|
@login_required
|
2023-04-26 11:20:29 +03:00
|
|
|
def delete_contributor(book_id: int):
|
2023-04-25 11:56:51 +03:00
|
|
|
book: m.Book = db.session.get(m.Book, book_id)
|
2023-04-25 14:24:04 +03: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 11:56:51 +03:00
|
|
|
flash("You are not owner of this book!", "danger")
|
|
|
|
return redirect(url_for("book.get_all"))
|
|
|
|
|
|
|
|
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 14:24:04 +03:00
|
|
|
log(
|
|
|
|
log.INFO,
|
|
|
|
"BookContributor does not exists user: [%s], book: [%s]",
|
|
|
|
form.user_id.data,
|
|
|
|
book.id,
|
|
|
|
)
|
2023-04-25 11:56:51 +03:00
|
|
|
flash("Does not exists!", "success")
|
|
|
|
return redirect(url_for("book.settings", book_id=book_id))
|
|
|
|
|
2023-04-25 14:24:04 +03:00
|
|
|
log(log.INFO, "Delete BookContributor [%s]", book_contributor)
|
2023-04-25 11:56:51 +03: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 12:39:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/<int:book_id>/edit_contributor_role", methods=["POST"])
|
|
|
|
@login_required
|
2023-04-26 11:20:29 +03:00
|
|
|
def edit_contributor_role(book_id: int):
|
2023-04-25 12:39:26 +03:00
|
|
|
book: m.Book = db.session.get(m.Book, book_id)
|
2023-04-25 14:24:04 +03: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 12:39:26 +03:00
|
|
|
flash("You are not owner of this book!", "danger")
|
|
|
|
return redirect(url_for("book.get_all"))
|
|
|
|
|
|
|
|
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 14:24:04 +03:00
|
|
|
log(
|
|
|
|
log.INFO,
|
|
|
|
"BookContributor does not exists user: [%s], book: [%s]",
|
|
|
|
form.user_id.data,
|
|
|
|
book.id,
|
|
|
|
)
|
2023-04-25 12:39:26 +03: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 14:24:04 +03:00
|
|
|
|
|
|
|
log(
|
|
|
|
log.INFO,
|
|
|
|
"Update contributor [%s] role: new role: [%s]",
|
|
|
|
book_contributor,
|
|
|
|
role,
|
|
|
|
)
|
2023-04-25 12:39:26 +03: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))
|