edit interpretation ui

This commit is contained in:
SvyatoslavArtymovych 2023-05-04 16:32:56 +03:00
parent d50bf52e49
commit 6fce2cd53b
4 changed files with 167 additions and 100 deletions

View File

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

View File

@ -2,7 +2,7 @@ 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 import models as m, db
from app.logger import log
@ -33,28 +33,30 @@ class CreateInterpretationForm(BaseInterpretationForm):
raise ValidationError("Interpretation label must be unique!")
# class EditSectionForm(BaseSectionForm):
# section_id = StringField("Section ID", [DataRequired()])
# submit = SubmitField("Edit")
class EditInterpretationForm(BaseInterpretationForm):
interpretation_id = StringField("Interpretation ID", [DataRequired()])
text = StringField("Text")
submit = SubmitField("Edit")
# def validate_label(self, field):
# label = field.data
# section_id = self.section_id.data
def validate_label(self, field):
label = field.data
interpretation_id = self.interpretation_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()
# )
section_id = db.session.get(m.Interpretation, interpretation_id).section_id
# if section:
# log(
# log.WARNING,
# "Section with label [%s] already exists: [%s]",
# label,
# section,
# )
# raise ValidationError("Section label must be unique!")
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

@ -949,3 +949,91 @@ def interpretation_create(
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)

View File

@ -814,91 +814,68 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
assert response.status_code == 200
assert b"Section not found" in response.data
# # edit
# edit
# m.Section(
# label="Test",
# about="Test",
# collection_id=leaf_collection.id,
# version_id=book.last_version.id,
# ).save()
m.Interpretation(
label="Test",
text="Test",
section_id=section_in_collection.id,
).save()
# m.Section(
# label="Test",
# about="Test",
# collection_id=sub_collection.id,
# version_id=book.last_version.id,
# ).save()
m.Interpretation(
label="Test",
text="Test",
section_id=section_in_subcollection.id,
).save()
# section: m.Section = m.Section.query.filter_by(
# label=label_1, collection_id=leaf_collection.id
# ).first()
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.id}/edit_section",
# data=dict(
# section_id=section.id,
# label="Test",
# ),
# follow_redirects=True,
# )
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"Section label must be unique!" in response.data
assert response.status_code == 200
assert b"Interpretation label must be unique!" in response.data
# new_label = "Test Section #1 Label(edited)"
# new_about = "Test Section #1 About(edited)"
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.id}/edit_section",
# data=dict(section_id=section.id, label=new_label, about=new_about),
# follow_redirects=True,
# )
# assert response.status_code == 200
# assert b"Success!" in response.data
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_section: m.Section = m.Section.query.filter_by(
# label=new_label, about=new_about, id=section.id
# ).first()
# assert edited_section
edited_interpretation: m.Interpretation = m.Interpretation.query.filter_by(
label=new_label, text=new_text, id=interpretation.id
).first()
assert edited_interpretation
# #
# section_2: m.Section = m.Section.query.filter_by(
# label=label_1, collection_id=sub_collection.id
# ).first()
# response: Response = client.post(
# f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_2.id}/edit_section",
# data=dict(
# section_id=section_2.id,
# label="Test",
# ),
# follow_redirects=True,
# )
# assert response.status_code == 200
# assert b"Section label must be unique!" in response.data
# response: Response = client.post(
# f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_2.id}/edit_section",
# data=dict(section_id=section_2.id, label=new_label, about=new_about),
# follow_redirects=True,
# )
# assert response.status_code == 200
# assert b"Success!" in response.data
# edited_section: m.Section = m.Section.query.filter_by(
# label=new_label, about=new_about, id=section_2.id
# ).first()
# assert edited_section
# response: Response = client.post(
# f"/book/{book.id}/{collection.id}/{sub_collection.id}/999/edit_section",
# data=dict(section_id=section_2.id, label=new_label, about=new_about),
# follow_redirects=True,
# )
# assert response.status_code == 200
# assert b"Section not found" in response.data
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}/{collection.id}/{leaf_collection.id}/{section.id}/delete_section",