set comment tags on comment create (backend)

This commit is contained in:
SvyatoslavArtymovych 2023-05-16 13:38:51 +03:00
parent 063594c7f9
commit 5afb843292
5 changed files with 74 additions and 5 deletions

View File

@ -6,6 +6,7 @@ def set_book_tags(book: m.Book, tags: str):
book_tags = m.BookTags.query.filter_by(book_id=book.id).all()
for book_tag in book_tags:
db.session.delete(book_tag)
tags_names = [tag.title() for tag in tags.split(",") if len(tag)]
for tag_name in tags_names:
@ -24,6 +25,33 @@ def set_book_tags(book: m.Book, tags: str):
tag = m.Tag(name=tag_name).save()
book_tag = m.BookTags(tag_id=tag.id, book_id=book.id)
log(log.INFO, "Create BookTag: [%s]", tag)
log(log.INFO, "Create BookTag: [%s]", book_tag)
book_tag.save(False)
db.session.commit()
def set_comment_tags(comment: m.Comment, tags: str):
comment_tags = m.CommentTags.query.filter_by(comment_id=comment.id).all()
for tag in comment_tags:
db.session.delete(tag)
tags_names = [tag.title() for tag in tags.split(",") if len(tag)]
for tag_name in tags_names:
if len(tag_name) > 32:
log(
log.ERROR,
"Cannot create Tag [%s]. Exceeded name length. Current length: [%s]",
tag_name,
len(tag_name),
)
continue
tag = m.Tag.query.filter_by(name=tag_name).first()
if not tag:
log(log.INFO, "Create Tag: [%s]", tag)
tag = m.Tag(name=tag_name).save()
comment_tag = m.CommentTags(tag_id=tag.id, comment_id=comment.id)
log(log.INFO, "Create CommentTags: [%s]", comment_tag)
comment_tag.save(False)
db.session.commit()

View File

@ -9,6 +9,7 @@ class BaseCommentForm(FlaskForm):
class CreateCommentForm(BaseCommentForm):
parent_id = StringField("Text")
tags = StringField("Tags")
submit = SubmitField("Create")

View File

@ -14,6 +14,7 @@ from app.controllers import (
register_book_verify_route,
book_validator,
set_book_tags,
set_comment_tags
)
from app import models as m, db, forms as f
from app.logger import log
@ -1007,13 +1008,13 @@ def qa_view(
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/preview/create_comment",
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/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"
"<int:section_id>/<int:interpretation_id>/create_comment"
),
methods=["POST"],
)
@ -1103,6 +1104,9 @@ def create_comment(
section,
)
comment.save()
tags = form.tags.data
if tags:
set_comment_tags(comment, tags)
flash("Success!", "success")
return redirect(redirect_url)

View File

@ -1015,7 +1015,7 @@ def test_crud_comment(client: FlaskClient, runner: FlaskCliRunner):
comment_text = "Some comment text"
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_in_subcollection.id}/{interpretation.id}/preview/create_comment",
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_in_subcollection.id}/{interpretation.id}/create_comment",
data=dict(
section_id=section_in_subcollection.id,
text=comment_text,

View File

@ -2,7 +2,7 @@ from flask import current_app as Response
from flask.testing import FlaskClient
from app import models as m
from tests.utils import login
from tests.utils import login, create_test_book
def test_create_tags_on_book_create(client: FlaskClient):
@ -85,3 +85,39 @@ def test_create_tags_on_book_edit(client: FlaskClient):
assert len(tags_from_db) == 4
book: m.Book = m.Book.query.first()
assert len(book.tags) == 0
def test_create_tags_on_comment_create(client: FlaskClient):
_, user = login(client)
create_test_book(user.id, 1)
book = m.Book.query.get(1)
collection = m.Collection.query.get(1)
section = m.Section.query.get(1)
interpretation = m.Interpretation.query.get(1)
tags = "tag1,tag2,tag3"
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{section.id}/{interpretation.id}/create_comment",
data=dict(
section_id=section.id,
text="some text",
interpretation_id=interpretation.id,
tags=tags,
),
follow_redirects=True,
)
assert response.status_code == 200
comment: m.Comment = m.Comment.query.filter_by(text="some text").first()
assert comment
assert comment.tags
splitted_tags = [tag.title() for tag in tags.split(",")]
assert len(comment.tags) == 3
for tag in comment.tags:
tag: m.Tag
assert tag.name in splitted_tags
tags_from_db: m.Tag = m.Tag.query.all()
assert len(tags_from_db) == 3