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

145 lines
4.2 KiB
Python
Raw Normal View History

2023-05-22 11:44:12 +00:00
from flask import flash, redirect, url_for, current_app
2023-05-17 16:21:53 +00:00
from flask_login import login_required, current_user
from app.controllers import (
register_book_verify_route,
)
from app.controllers.delete_nested_book_entities import (
delete_nested_comment_entities,
)
from app import models as m, db, forms as f
from app.controllers.tags import set_comment_tags
2023-05-17 16:21:53 +00:00
from app.logger import log
from .bp import bp
@bp.route(
2023-05-30 07:02:00 +00:00
"/<int:book_id>/<int:interpretation_id>/create_comment",
2023-05-17 16:21:53 +00:00
methods=["POST"],
)
@login_required
def create_comment(
book_id: int,
interpretation_id: int,
):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.is_deleted:
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
flash("Book not found!", "danger")
2023-05-17 16:21:53 +00:00
return redirect(url_for("book.my_library"))
redirect_url = url_for(
2023-05-30 07:12:43 +00:00
"book.qa_view", book_id=book_id, interpretation_id=interpretation_id
2023-05-17 16:21:53 +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)
form = f.CreateCommentForm()
if form.validate_on_submit():
2023-05-22 11:44:12 +00:00
text = form.text.data
2023-05-17 16:21:53 +00:00
comment: m.Comment = m.Comment(
2023-05-22 11:44:12 +00:00
text=text,
2023-05-17 16:21:53 +00:00
user_id=current_user.id,
interpretation_id=interpretation_id,
)
if form.parent_id.data:
comment.parent_id = form.parent_id.data
comment.interpretation = None
log(
log.INFO,
2023-05-30 07:02:00 +00:00
"Create comment for interpretation [%s].",
2023-05-17 16:21:53 +00:00
interpretation,
)
comment.save()
2023-05-22 11:44:12 +00:00
tags = current_app.config["TAG_REGEX"].findall(text)
2023-05-26 07:14:21 +00:00
set_comment_tags(comment, tags)
# TODO Send notifications
# users_mentions = current_app.config["USER_MENTION_REGEX"].findall(text)
2023-05-22 11:44:12 +00:00
2023-05-17 16:21:53 +00:00
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-30 07:34:53 +00:00
@bp.route("/<int:book_id>/<int:interpretation_id>/comment_delete", methods=["POST"])
2023-05-17 16:21:53 +00:00
@register_book_verify_route(bp.name)
@login_required
2023-05-30 07:34:53 +00:00
def comment_delete(book_id: int, interpretation_id: int):
2023-05-30 07:24:25 +00:00
redirect_url = url_for(
"book.qa_view", book_id=book_id, interpretation_id=interpretation_id
)
2023-05-17 16:21:53 +00:00
form = f.DeleteCommentForm()
comment_id = form.comment_id.data
comment: m.Comment = db.session.get(m.Comment, comment_id)
if form.validate_on_submit():
comment.is_deleted = True
delete_nested_comment_entities(comment)
log(log.INFO, "Delete comment [%s]", comment)
comment.save()
flash("Success!", "success")
2023-05-30 07:24:25 +00:00
return redirect(redirect_url)
2023-05-30 07:02:00 +00:00
flash("Invalid id!", "danger")
2023-05-17 16:21:53 +00:00
return redirect(
url_for(
2023-05-30 07:02:00 +00:00
"book.collection_view",
2023-05-17 16:21:53 +00:00
book_id=book_id,
)
)
@bp.route(
2023-05-30 07:02:00 +00:00
"/<int:book_id>/<int:interpretation_id>/comment_edit",
2023-05-17 16:21:53 +00:00
methods=["POST"],
)
@register_book_verify_route(bp.name)
@login_required
def comment_edit(
book_id: int,
interpretation_id: int,
):
2023-05-30 07:24:25 +00:00
redirect_url = url_for(
"book.qa_view", book_id=book_id, interpretation_id=interpretation_id
)
2023-05-17 16:21:53 +00:00
form = f.EditCommentForm()
if form.validate_on_submit():
2023-05-22 11:44:12 +00:00
text = form.text.data
comment_id = form.comment_id.data
comment: m.Comment = db.session.get(m.Comment, comment_id)
2023-05-22 11:44:12 +00:00
comment.text = text
2023-05-17 16:21:53 +00:00
comment.edited = True
log(log.INFO, "Edit comment [%s]", comment)
2023-05-22 11:44:12 +00:00
tags = current_app.config["TAG_REGEX"].findall(text)
2023-05-26 07:14:21 +00:00
set_comment_tags(comment, tags)
2023-05-22 11:44:12 +00:00
2023-05-17 16:21:53 +00:00
comment.save()
flash("Success!", "success")
2023-05-30 07:24:25 +00:00
return redirect(redirect_url)
2023-05-30 07:02:00 +00:00
flash("Invalid id!", "danger")
2023-05-17 16:21:53 +00:00
return redirect(
url_for(
2023-05-30 07:02:00 +00:00
"book.collection_view",
2023-05-17 16:21:53 +00:00
book_id=book_id,
)
)