create interpretation backend

This commit is contained in:
SvyatoslavArtymovych 2023-05-04 15:14:17 +03:00
parent a91c993c79
commit d50bf52e49
4 changed files with 157 additions and 27 deletions

View File

@ -9,3 +9,4 @@ from .contributor import (
) )
from .collection import CreateCollectionForm, EditCollectionForm from .collection import CreateCollectionForm, EditCollectionForm
from .section import CreateSectionForm, EditSectionForm from .section import CreateSectionForm, EditSectionForm
from .interpretation import CreateInterpretationForm

View File

@ -0,0 +1,60 @@
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, ValidationError
from wtforms.validators import DataRequired, Length
from app import models as m
from app.logger import log
class BaseInterpretationForm(FlaskForm):
label = StringField("Label", [DataRequired(), Length(3, 256)])
about = StringField("About")
class CreateInterpretationForm(BaseInterpretationForm):
section_id = StringField("Interpretation ID", [DataRequired()])
text = StringField("Text")
submit = SubmitField("Create")
def validate_label(self, field):
label = field.data
section_id = self.section_id.data
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
is_deleted=False, label=label, section_id=section_id
).first()
if interpretation:
log(
log.WARNING,
"Interpretation with label [%s] already exists: [%s]",
label,
interpretation,
)
raise ValidationError("Interpretation label must be unique!")
# class EditSectionForm(BaseSectionForm):
# section_id = StringField("Section ID", [DataRequired()])
# submit = SubmitField("Edit")
# def validate_label(self, field):
# label = field.data
# section_id = self.section_id.data
# collection_id = db.session.get(m.Section, section_id).collection_id
# section: m.Section = (
# m.Section.query.filter_by(
# is_deleted=False, label=label, collection_id=collection_id
# )
# .filter(m.Section.id != section_id)
# .first()
# )
# if section:
# log(
# log.WARNING,
# "Section with label [%s] already exists: [%s]",
# label,
# section,
# )
# raise ValidationError("Section label must be unique!")

View File

@ -864,3 +864,88 @@ def section_delete(
book_id=book_id, book_id=book_id,
) )
) )
###############
# Interpretation CRUD
###############
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/create_interpretation",
methods=["POST"],
)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>/create_interpretation",
methods=["POST"],
)
@login_required
def interpretation_create(
book_id: int,
collection_id: int,
section_id: int,
sub_collection_id: int | None = None,
):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.is_deleted:
log(log.WARNING, "Book with id [%s] not found", book_id)
flash("Book not found", "danger")
return redirect(url_for("book.my_books"))
collection: m.Collection = db.session.get(m.Collection, collection_id)
if not collection or collection.is_deleted:
log(log.WARNING, "Collection with id [%s] not found", collection_id)
flash("Collection not found", "danger")
return redirect(url_for("book.collection_view", book_id=book_id))
sub_collection = None
if sub_collection_id:
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection or sub_collection.is_deleted:
log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id)
flash("Subcollection not found", "danger")
return redirect(
url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
)
)
redirect_url = url_for(
"book.section_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
)
section: m.Section = db.session.get(m.Section, section_id)
if not section or collection.is_deleted:
log(log.WARNING, "Section with id [%s] not found", section)
flash("Section not found", "danger")
return redirect(redirect_url)
form = f.CreateInterpretationForm()
if form.validate_on_submit():
interpretation: m.Interpretation = m.Interpretation(
label=form.label.data,
text=form.text.data,
section_id=section_id,
)
log(
log.INFO,
"Create interpretation [%s]. Section: [%s]",
interpretation,
section,
)
interpretation.save()
flash("Success!", "success")
return redirect(redirect_url)
else:
log(log.ERROR, "Interpretation 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(redirect_url)

View File

@ -741,13 +741,11 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
).save() ).save()
label_1 = "Test Interpretation #1 Label" label_1 = "Test Interpretation #1 Label"
text_1 = "Test Interpretation #1 Text"
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_in_subcollection.id}/create_interpretation", f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_in_subcollection.id}/create_interpretation",
data=dict( data=dict(section_id=section_in_subcollection.id, label=label_1, text=text_1),
section_id=section_in_subcollection.id,
label=label_1,
text="Test Interpretation #1 Label",
),
follow_redirects=True, follow_redirects=True,
) )
@ -756,17 +754,12 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
label=label_1, section_id=section_in_subcollection.id label=label_1, section_id=section_in_subcollection.id
).first() ).first()
assert interpretation assert interpretation
assert interpretation.collection_id == section_in_subcollection.id assert interpretation.section_id == section_in_subcollection.id
assert interpretation.version_id == book.last_version.id
assert not interpretation.comments assert not interpretation.comments
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_in_subcollection.id}/create_interpretation", f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_in_subcollection.id}/create_interpretation",
data=dict( data=dict(section_id=section_in_subcollection.id, label=label_1, text=text_1),
section_id=section_in_subcollection.id,
label=label_1,
text="Test Interpretation #1 Label",
),
follow_redirects=True, follow_redirects=True,
) )
@ -774,11 +767,7 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/create_interpretation", f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/create_interpretation",
data=dict( data=dict(section_id=section_in_collection.id, label=label_1, text=text_1),
section_id=section_in_collection.id,
label=label_1,
text="Test Interpretation #1 Label",
),
follow_redirects=True, follow_redirects=True,
) )
@ -787,17 +776,12 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
label=label_1, section_id=section_in_collection.id label=label_1, section_id=section_in_collection.id
).first() ).first()
assert interpretation assert interpretation
assert interpretation.collection_id == section_in_subcollection.id assert interpretation.section_id == section_in_collection.id
assert interpretation.version_id == book.last_version.id
assert not interpretation.comments assert not interpretation.comments
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/create_interpretation", f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/create_interpretation",
data=dict( data=dict(section_id=section_in_collection.id, label=label_1, text=text_1),
section_id=section_in_collection.id,
label=label_1,
text="Test Interpretation #1 Label",
),
follow_redirects=True, follow_redirects=True,
) )
@ -805,7 +789,7 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{collection.id}/999/create_section", f"/book/{book.id}/{collection.id}/999/create_section",
data=dict(collection_id=999, label=label_1, about="Test Section #1 About"), data=dict(collection_id=999, label=label_1, text=text_1),
follow_redirects=True, follow_redirects=True,
) )
@ -814,7 +798,7 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/999/create_interpretation", f"/book/{book.id}/{leaf_collection.id}/999/create_interpretation",
data=dict(collection_id=999, label=label_1, about="Test Section #1 About"), data=dict(collection_id=999, label=label_1, text=text_1),
follow_redirects=True, follow_redirects=True,
) )
@ -823,7 +807,7 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
response: Response = client.post( response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/888/create_interpretation", f"/book/{book.id}/{collection.id}/{sub_collection.id}/888/create_interpretation",
data=dict(collection_id=999, label=label_1, about="Test Section #1 About"), data=dict(collection_id=999, label=label_1, text=text_1),
follow_redirects=True, follow_redirects=True,
) )