Merge pull request #23 from Simple2B/svyat/feat/interpretation_crud

Svyat/feat/interpretation crud
This commit is contained in:
Svyatoslav Artymovych 2023-05-04 17:21:03 +03:00 committed by GitHub
commit a0590b946a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 532 additions and 0 deletions

View File

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

View File

@ -0,0 +1,62 @@
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, ValidationError
from wtforms.validators import DataRequired, Length
from app import models as m, db
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 EditInterpretationForm(BaseInterpretationForm):
interpretation_id = StringField("Interpretation ID", [DataRequired()])
text = StringField("Text")
submit = SubmitField("Edit")
def validate_label(self, field):
label = field.data
interpretation_id = self.interpretation_id.data
section_id = db.session.get(m.Interpretation, interpretation_id).section_id
interpretation: m.Interpretation = (
m.Interpretation.query.filter_by(
is_deleted=False, label=label, section_id=section_id
)
.filter(m.Interpretation.id != interpretation_id)
.first()
)
if interpretation:
log(
log.WARNING,
"Interpretation with label [%s] already exists: [%s]",
label,
interpretation,
)
raise ValidationError("Interpretation label must be unique!")

View File

@ -864,3 +864,254 @@ def section_delete(
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)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/edit_interpretation",
methods=["POST"],
)
@bp.route(
(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/"
"<int:section_id>/<int:interpretation_id>/edit_interpretation"
),
methods=["POST"],
)
@login_required
def interpretation_edit(
book_id: int,
collection_id: int,
section_id: int,
interpretation_id: int,
sub_collection_id: int | None = None,
):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.owner != current_user or book.is_deleted:
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
flash("You are not owner of this book!", "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))
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.interpretation_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
section_id=section_id,
)
section: m.Section = db.session.get(m.Section, section_id)
if not section or section.is_deleted:
log(log.WARNING, "Section with id [%s] not found", section_id)
flash("Section not found", "danger")
return redirect(redirect_url)
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.EditInterpretationForm()
if form.validate_on_submit():
label = form.label.data
if label:
interpretation.label = label
interpretation.text = form.text.data
log(log.INFO, "Edit interpretation [%s]", interpretation.id)
interpretation.save()
flash("Success!", "success")
return redirect(redirect_url)
else:
log(log.ERROR, "Interpretation edit 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)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:section_id>/<int:interpretation_id>/delete_interpretation",
methods=["POST"],
)
@bp.route(
(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/"
"<int:section_id>/<int:interpretation_id>/delete_interpretation"
),
methods=["POST"],
)
@login_required
def interpretation_delete(
book_id: int,
collection_id: int,
section_id: int,
interpretation_id: int,
sub_collection_id: int | None = None,
):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.owner != current_user or book.is_deleted:
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
flash("You are not owner of this book!", "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))
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.interpretation_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
section_id=section_id,
)
section: m.Section = db.session.get(m.Section, section_id)
if not section or section.is_deleted:
log(log.WARNING, "Section with id [%s] not found", section_id)
flash("Section not found", "danger")
return redirect(redirect_url)
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)
interpretation.is_deleted = True
log(log.INFO, "Delete interpretation [%s]", interpretation)
interpretation.save()
flash("Success!", "success")
return redirect(
url_for(
"book.collection_view",
book_id=book_id,
)
)

View File

@ -698,3 +698,221 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
assert response.status_code == 200
assert b"Section not found" in response.data
def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
_, user = login(client)
user: m.User
# add dummmy data
runner.invoke(args=["db-populate"])
book: m.Book = db.session.get(m.Book, 1)
book.user_id = user.id
book.save()
leaf_collection: m.Collection = m.Collection(
label="Test Leaf Collection #1 Label",
version_id=book.last_version.id,
is_leaf=True,
parent_id=book.last_version.root_collection.id,
).save()
section_in_collection: m.Section = m.Section(
label="Test Section in Collection #1 Label",
about="Test Section in Collection #1 About",
collection_id=leaf_collection.id,
version_id=book.last_version.id,
).save()
collection: m.Collection = m.Collection(
label="Test Collection #1 Label", version_id=book.last_version.id
).save()
sub_collection: m.Collection = m.Collection(
label="Test SubCollection #1 Label",
version_id=book.last_version.id,
parent_id=collection.id,
is_leaf=True,
).save()
section_in_subcollection: m.Section = m.Section(
label="Test Section in Subcollection #1 Label",
about="Test Section in Subcollection #1 About",
collection_id=sub_collection.id,
version_id=book.last_version.id,
).save()
label_1 = "Test Interpretation #1 Label"
text_1 = "Test Interpretation #1 Text"
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_in_subcollection.id}/create_interpretation",
data=dict(section_id=section_in_subcollection.id, label=label_1, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
label=label_1, section_id=section_in_subcollection.id
).first()
assert interpretation
assert interpretation.section_id == section_in_subcollection.id
assert not interpretation.comments
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_in_subcollection.id}/create_interpretation",
data=dict(section_id=section_in_subcollection.id, label=label_1, text=text_1),
follow_redirects=True,
)
assert b"Interpretation label must be unique!" in response.data
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/create_interpretation",
data=dict(section_id=section_in_collection.id, label=label_1, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
label=label_1, section_id=section_in_collection.id
).first()
assert interpretation
assert interpretation.section_id == section_in_collection.id
assert not interpretation.comments
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/create_interpretation",
data=dict(section_id=section_in_collection.id, label=label_1, text=text_1),
follow_redirects=True,
)
assert b"Interpretation label must be unique!" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/999/create_section",
data=dict(collection_id=999, label=label_1, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Subcollection not found" in response.data
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/999/create_interpretation",
data=dict(collection_id=999, label=label_1, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Section not found" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/888/create_interpretation",
data=dict(collection_id=999, label=label_1, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Section not found" in response.data
# edit
m.Interpretation(
label="Test",
text="Test",
section_id=section_in_collection.id,
).save()
m.Interpretation(
label="Test",
text="Test",
section_id=section_in_subcollection.id,
).save()
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
label=label_1, section_id=section_in_collection.id
).first()
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/{interpretation.id}/edit_interpretation",
data=dict(
interpretation_id=interpretation.id,
label="Test",
text="Test",
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Interpretation label must be unique!" in response.data
new_label = "Test Interpretation #1 Label(edited)"
new_text = "Test Interpretation #1 Text(edited)"
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/{interpretation.id}/edit_interpretation",
data=dict(
label=new_label,
interpretation_id=interpretation.id,
text=new_text,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
edited_interpretation: m.Interpretation = m.Interpretation.query.filter_by(
label=new_label, text=new_text, id=interpretation.id
).first()
assert edited_interpretation
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/999/edit_interpretation",
data=dict(
interpretation_id=interpretation.id,
label=new_label,
text=new_text,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Interpretation not found" in response.data
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/999/delete_interpretation",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Interpretation not found" in response.data
response: Response = client.post(
(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/"
f"{section_in_subcollection.id}/{section_in_subcollection.interpretations[0].id}/delete_interpretation"
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
deleted_interpretation: m.Interpretation = db.session.get(
m.Interpretation, section_in_subcollection.interpretations[0].id
)
assert deleted_interpretation.is_deleted
response: Response = client.post(
(
f"/book/{book.id}/{leaf_collection.id}/{section_in_collection.id}/"
f"{section_in_collection.interpretations[0].id}/delete_interpretation"
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
deleted_interpretation: m.Interpretation = db.session.get(
m.Interpretation, section_in_collection.interpretations[0].id
)
assert deleted_interpretation.is_deleted