2023-05-17 16:21:53 +00:00
|
|
|
from flask import (
|
|
|
|
render_template,
|
|
|
|
flash,
|
|
|
|
redirect,
|
|
|
|
url_for,
|
2023-06-07 12:49:21 +00:00
|
|
|
request,
|
2023-05-17 16:21:53 +00:00
|
|
|
)
|
|
|
|
from flask_login import login_required
|
|
|
|
|
2023-06-12 09:46:43 +00:00
|
|
|
from app.controllers import register_book_verify_route
|
|
|
|
from app.controllers.notification_producer import contributor_notification
|
2023-05-17 16:21:53 +00:00
|
|
|
from app import models as m, db, forms as f
|
2023-05-29 13:14:00 +00:00
|
|
|
from app.controllers.require_permission import require_permission
|
2023-06-09 13:36:09 +00:00
|
|
|
from app.controllers.contributor import (
|
|
|
|
add_contributor_to_book,
|
|
|
|
delete_contributor_from_book,
|
|
|
|
)
|
2023-06-15 14:15:22 +00:00
|
|
|
from app.controllers.error_flashes import create_error_flash
|
2023-05-17 16:21:53 +00:00
|
|
|
from app.logger import log
|
|
|
|
from .bp import bp
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/<int:book_id>/settings", methods=["GET"])
|
|
|
|
@register_book_verify_route(bp.name)
|
2023-05-29 13:14:00 +00:00
|
|
|
@require_permission(
|
|
|
|
entity_type=m.Permission.Entity.BOOK,
|
|
|
|
access=[m.Permission.Access.U],
|
2023-05-31 11:18:11 +00:00
|
|
|
entities=[m.Book],
|
2023-05-29 13:14:00 +00:00
|
|
|
)
|
2023-05-17 16:21:53 +00:00
|
|
|
@login_required
|
|
|
|
def settings(book_id: int):
|
|
|
|
book: m.Book = db.session.get(m.Book, book_id)
|
2023-06-07 12:49:21 +00:00
|
|
|
selected_tab = request.args.get("selected_tab", "book_settings")
|
2023-05-17 16:21:53 +00:00
|
|
|
|
|
|
|
return render_template(
|
2023-06-07 12:49:21 +00:00
|
|
|
"book/settings.html",
|
|
|
|
book=book,
|
|
|
|
selected_tab=selected_tab,
|
|
|
|
roles=m.BookContributor.Roles,
|
2023-05-17 16:21:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/<int:book_id>/add_contributor", methods=["POST"])
|
|
|
|
@register_book_verify_route(bp.name)
|
2023-05-29 13:14:00 +00:00
|
|
|
@require_permission(
|
|
|
|
entity_type=m.Permission.Entity.BOOK,
|
|
|
|
access=[m.Permission.Access.U],
|
2023-05-31 11:18:11 +00:00
|
|
|
entities=[m.Book],
|
2023-05-29 13:14:00 +00:00
|
|
|
)
|
2023-05-17 16:21:53 +00:00
|
|
|
@login_required
|
|
|
|
def add_contributor(book_id: int):
|
|
|
|
form = f.AddContributorForm()
|
2023-06-07 12:49:21 +00:00
|
|
|
selected_tab = "user_permissions"
|
2023-05-17 16:21:53 +00:00
|
|
|
if form.validate_on_submit():
|
2023-05-25 11:11:19 +00:00
|
|
|
user_id = form.user_id.data
|
2023-06-08 12:22:32 +00:00
|
|
|
# notifications
|
2023-06-12 09:53:14 +00:00
|
|
|
contributor_notification(m.Notification.Actions.CONTRIBUTING, book_id, user_id)
|
2023-06-08 12:22:32 +00:00
|
|
|
# -------------
|
2023-06-09 13:36:09 +00:00
|
|
|
response = add_contributor_to_book(form, book_id, selected_tab)
|
|
|
|
return response
|
2023-05-17 16:21:53 +00:00
|
|
|
else:
|
|
|
|
log(log.ERROR, "Book create errors: [%s]", form.errors)
|
2023-06-15 14:15:22 +00:00
|
|
|
create_error_flash(form)
|
2023-06-07 12:49:21 +00:00
|
|
|
return redirect(
|
|
|
|
url_for("book.settings", selected_tab=selected_tab, book_id=book_id)
|
|
|
|
)
|
2023-05-17 16:21:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/<int:book_id>/delete_contributor", methods=["POST"])
|
|
|
|
@register_book_verify_route(bp.name)
|
2023-05-29 13:14:00 +00:00
|
|
|
@require_permission(
|
|
|
|
entity_type=m.Permission.Entity.BOOK,
|
|
|
|
access=[m.Permission.Access.U],
|
2023-05-31 11:18:11 +00:00
|
|
|
entities=[m.Book],
|
2023-05-29 13:14:00 +00:00
|
|
|
)
|
2023-05-17 16:21:53 +00:00
|
|
|
@login_required
|
|
|
|
def delete_contributor(book_id: int):
|
|
|
|
form = f.DeleteContributorForm()
|
2023-06-07 12:49:21 +00:00
|
|
|
selected_tab = "user_permissions"
|
2023-05-17 16:21:53 +00:00
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2023-06-12 09:53:14 +00:00
|
|
|
user_id = form.user_id.data
|
2023-06-08 12:22:32 +00:00
|
|
|
# notifications
|
2023-06-12 09:53:14 +00:00
|
|
|
contributor_notification(m.Notification.Actions.DELETE, book_id, user_id)
|
2023-06-08 12:22:32 +00:00
|
|
|
# -------------
|
2023-06-09 13:36:09 +00:00
|
|
|
response = delete_contributor_from_book(form, book_id, selected_tab)
|
|
|
|
return response
|
2023-05-17 16:21:53 +00:00
|
|
|
else:
|
2023-06-06 14:47:35 +00:00
|
|
|
log(log.ERROR, "Delete contributor errors: [%s]", form.errors)
|
2023-06-15 14:15:22 +00:00
|
|
|
create_error_flash(form)
|
2023-06-07 12:49:21 +00:00
|
|
|
return redirect(
|
|
|
|
url_for("book.settings", selected_tab=selected_tab, book_id=book_id)
|
|
|
|
)
|
2023-05-17 16:21:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/<int:book_id>/edit_contributor_role", methods=["POST"])
|
|
|
|
@register_book_verify_route(bp.name)
|
2023-05-29 13:14:00 +00:00
|
|
|
@require_permission(
|
|
|
|
entity_type=m.Permission.Entity.BOOK,
|
|
|
|
access=[m.Permission.Access.U],
|
2023-05-31 11:18:11 +00:00
|
|
|
entities=[m.Book],
|
2023-05-29 13:14:00 +00:00
|
|
|
)
|
2023-05-17 16:21:53 +00:00
|
|
|
@login_required
|
|
|
|
def edit_contributor_role(book_id: int):
|
|
|
|
form = f.EditContributorRoleForm()
|
2023-06-07 12:49:21 +00:00
|
|
|
selected_tab = "user_permissions"
|
2023-05-17 16:21:53 +00:00
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2023-05-31 12:51:14 +00:00
|
|
|
book_contributor: m.BookContributor = m.BookContributor.query.filter_by(
|
2023-05-17 16:21:53 +00:00
|
|
|
user_id=int(form.user_id.data), book_id=book_id
|
|
|
|
).first()
|
|
|
|
if not book_contributor:
|
|
|
|
log(
|
|
|
|
log.INFO,
|
|
|
|
"BookContributor does not exists user: [%s], book: [%s]",
|
|
|
|
form.user_id.data,
|
|
|
|
book_id,
|
|
|
|
)
|
|
|
|
flash("Does not exists!", "success")
|
2023-06-07 12:49:21 +00:00
|
|
|
return redirect(
|
|
|
|
url_for("book.settings", selected_tab=selected_tab, book_id=book_id)
|
|
|
|
)
|
2023-05-17 16:21:53 +00:00
|
|
|
|
|
|
|
role = m.BookContributor.Roles(int(form.role.data))
|
2023-05-31 12:51:14 +00:00
|
|
|
|
|
|
|
# change access group
|
|
|
|
current_group = m.AccessGroup.query.filter_by(
|
|
|
|
book_id=book_id, name=book_contributor.role.name.lower()
|
|
|
|
).first()
|
2023-06-01 04:57:31 +00:00
|
|
|
user_access_group = m.UserAccessGroups.query.filter_by(
|
|
|
|
user_id=book_contributor.user_id, access_group_id=current_group.id
|
|
|
|
).first()
|
|
|
|
if user_access_group:
|
|
|
|
db.session.delete(user_access_group)
|
2023-05-31 12:51:14 +00:00
|
|
|
|
|
|
|
new_group = m.AccessGroup.query.filter_by(
|
|
|
|
book_id=book_id, name=role.name.lower()
|
|
|
|
).first()
|
|
|
|
m.UserAccessGroups(
|
|
|
|
user_id=book_contributor.user_id, access_group_id=new_group.id
|
|
|
|
).save(False)
|
|
|
|
|
2023-05-17 16:21:53 +00:00
|
|
|
book_contributor.role = role
|
|
|
|
|
|
|
|
log(
|
|
|
|
log.INFO,
|
|
|
|
"Update contributor [%s] role: new role: [%s]",
|
|
|
|
book_contributor,
|
|
|
|
role,
|
|
|
|
)
|
|
|
|
book_contributor.save()
|
|
|
|
|
|
|
|
flash("Success!", "success")
|
2023-06-07 12:49:21 +00:00
|
|
|
return redirect(
|
|
|
|
url_for("book.settings", selected_tab=selected_tab, book_id=book_id)
|
|
|
|
)
|
2023-05-17 16:21:53 +00:00
|
|
|
else:
|
2023-06-06 14:47:35 +00:00
|
|
|
log(log.ERROR, "Edit contributor errors: [%s]", form.errors)
|
2023-06-15 14:15:22 +00:00
|
|
|
create_error_flash(form)
|
2023-06-07 12:49:21 +00:00
|
|
|
return redirect(
|
|
|
|
url_for("book.settings", selected_tab=selected_tab, book_id=book_id)
|
|
|
|
)
|