mirror of
https://github.com/logos-co/open-law.git
synced 2025-01-09 22:35:50 +00:00
InterpretationTag backend
This commit is contained in:
parent
6fab600d56
commit
131e77592d
@ -2,6 +2,24 @@ from app import models as m, db
|
||||
from app.logger import log
|
||||
|
||||
|
||||
def get_or_create_tag(tag_name: str):
|
||||
if len(tag_name) > 32:
|
||||
log(
|
||||
log.ERROR,
|
||||
"Cannot create Tag [%s]. Exceeded name length. Current length: [%s]",
|
||||
tag_name,
|
||||
len(tag_name),
|
||||
)
|
||||
raise ValueError("Exceeded name length")
|
||||
|
||||
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()
|
||||
|
||||
return tag
|
||||
|
||||
|
||||
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:
|
||||
@ -10,19 +28,17 @@ def set_book_tags(book: m.Book, tags: str):
|
||||
tags_names = [tag.title() for tag in tags.split(",") if len(tag)]
|
||||
|
||||
for tag_name in tags_names:
|
||||
if len(tag_name) > 32:
|
||||
try:
|
||||
tag = get_or_create_tag(tag_name)
|
||||
except ValueError as e:
|
||||
if str(e) == "Exceeded name length":
|
||||
continue
|
||||
log(
|
||||
log.ERROR,
|
||||
"Cannot create Tag [%s]. Exceeded name length. Current length: [%s]",
|
||||
tag_name,
|
||||
len(tag_name),
|
||||
log.CRITICAL,
|
||||
"Unexpected error [%s]",
|
||||
str(e),
|
||||
)
|
||||
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()
|
||||
raise e
|
||||
|
||||
book_tag = m.BookTags(tag_id=tag.id, book_id=book.id)
|
||||
log(log.INFO, "Create BookTag: [%s]", book_tag)
|
||||
@ -37,21 +53,48 @@ def set_comment_tags(comment: m.Comment, tags: str):
|
||||
tags_names = [tag.title() for tag in tags.split(",") if len(tag)]
|
||||
|
||||
for tag_name in tags_names:
|
||||
if len(tag_name) > 32:
|
||||
try:
|
||||
tag = get_or_create_tag(tag_name)
|
||||
except ValueError as e:
|
||||
if str(e) == "Exceeded name length":
|
||||
continue
|
||||
log(
|
||||
log.ERROR,
|
||||
"Cannot create Tag [%s]. Exceeded name length. Current length: [%s]",
|
||||
tag_name,
|
||||
len(tag_name),
|
||||
log.CRITICAL,
|
||||
"Unexpected error [%s]",
|
||||
str(e),
|
||||
)
|
||||
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()
|
||||
raise e
|
||||
|
||||
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()
|
||||
|
||||
|
||||
def set_interpretation_tags(interpretation: m.InterpretationTag, tags: str):
|
||||
interpretation_tags = m.InterpretationTag.query.filter_by(
|
||||
interpretation_id=interpretation.id
|
||||
).all()
|
||||
for tag in interpretation_tags:
|
||||
db.session.delete(tag)
|
||||
tags_names = [tag.title() for tag in tags.split(",") if len(tag)]
|
||||
|
||||
for tag_name in tags_names:
|
||||
try:
|
||||
tag = get_or_create_tag(tag_name)
|
||||
except ValueError as e:
|
||||
if str(e) == "Exceeded name length":
|
||||
continue
|
||||
log(
|
||||
log.CRITICAL,
|
||||
"Unexpected error [%s]",
|
||||
str(e),
|
||||
)
|
||||
raise e
|
||||
|
||||
interpretation_tag = m.InterpretationTag(
|
||||
tag_id=tag.id, interpretation_id=interpretation.id
|
||||
)
|
||||
log(log.INFO, "Create InterpretationTag: [%s]", interpretation_tag)
|
||||
interpretation_tag.save(False)
|
||||
db.session.commit()
|
||||
|
@ -6,6 +6,7 @@ from wtforms.validators import DataRequired, Length
|
||||
class BaseInterpretationForm(FlaskForm):
|
||||
label = StringField("Label", [DataRequired(), Length(3, 256)])
|
||||
about = StringField("About")
|
||||
tags = StringField("Tags")
|
||||
|
||||
|
||||
class CreateInterpretationForm(BaseInterpretationForm):
|
||||
|
@ -14,7 +14,11 @@ from app.controllers import (
|
||||
register_book_verify_route,
|
||||
book_validator,
|
||||
)
|
||||
from app.controllers.tags import set_book_tags, set_comment_tags
|
||||
from app.controllers.tags import (
|
||||
set_book_tags,
|
||||
set_comment_tags,
|
||||
set_interpretation_tags,
|
||||
)
|
||||
from app import models as m, db, forms as f
|
||||
from app.logger import log
|
||||
|
||||
@ -819,6 +823,10 @@ def interpretation_create(
|
||||
)
|
||||
interpretation.save()
|
||||
|
||||
tags = form.tags.data
|
||||
if tags:
|
||||
set_interpretation_tags(interpretation, tags)
|
||||
|
||||
flash("Success!", "success")
|
||||
return redirect(redirect_url)
|
||||
else:
|
||||
@ -869,6 +877,9 @@ def interpretation_edit(
|
||||
interpretation.label = label
|
||||
|
||||
interpretation.text = form.text.data
|
||||
tags = form.tags.data
|
||||
if tags:
|
||||
set_interpretation_tags(interpretation, tags)
|
||||
|
||||
log(log.INFO, "Edit interpretation [%s]", interpretation.id)
|
||||
interpretation.save()
|
||||
|
@ -121,3 +121,62 @@ def test_create_tags_on_comment_create(client: FlaskClient):
|
||||
|
||||
tags_from_db: m.Tag = m.Tag.query.all()
|
||||
assert len(tags_from_db) == 3
|
||||
|
||||
|
||||
def test_create_tags_on_interpretation_create_and_edit(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)
|
||||
|
||||
tags = "tag1,tag2,tag3"
|
||||
label_1 = "Test Interpretation #1 Label"
|
||||
text_1 = "Test Interpretation #1 Text"
|
||||
|
||||
response: Response = client.post(
|
||||
f"/book/{book.id}/{collection.id}/{section.id}/create_interpretation",
|
||||
data=dict(section_id=section.id, label=label_1, text=text_1, tags=tags),
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
|
||||
label=label_1, section_id=section.id
|
||||
).first()
|
||||
assert interpretation
|
||||
assert interpretation.tags
|
||||
|
||||
splitted_tags = [tag.title() for tag in tags.split(",")]
|
||||
assert len(interpretation.tags) == 3
|
||||
for tag in interpretation.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
|
||||
|
||||
tags = "tag-4,tag5,tag3"
|
||||
response: Response = client.post(
|
||||
f"/book/{book.id}/{collection.id}/{section.id}/{interpretation.id}/edit_interpretation",
|
||||
data=dict(
|
||||
interpretation_id=interpretation.id, label=label_1, text=text_1, tags=tags
|
||||
),
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
|
||||
label=label_1, section_id=section.id
|
||||
).first()
|
||||
assert interpretation
|
||||
|
||||
splitted_tags = [tag.title() for tag in tags.split(",")]
|
||||
assert len(interpretation.tags) == 3
|
||||
for tag in interpretation.tags:
|
||||
tag: m.Tag
|
||||
assert tag.name in splitted_tags
|
||||
|
||||
tags_from_db: m.Tag = m.Tag.query.all()
|
||||
assert len(tags_from_db) == 5
|
||||
|
Loading…
x
Reference in New Issue
Block a user