section CRUD backend

This commit is contained in:
SvyatoslavArtymovych 2023-05-02 17:07:37 +03:00
parent 9c4f8962f8
commit fce4b9c56f
5 changed files with 541 additions and 3 deletions

View File

@ -8,3 +8,4 @@ from .contributor import (
EditContributorRoleForm,
)
from .collection import CreateCollectionForm, EditCollectionForm
from .section import CreateSectionForm, EditSectionForm

68
app/forms/section.py Normal file
View File

@ -0,0 +1,68 @@
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 BaseSectionForm(FlaskForm):
label = StringField("Label", [DataRequired(), Length(3, 256)])
about = StringField("About")
class CreateSectionForm(BaseSectionForm):
collection_id = StringField("Collection ID", [DataRequired()])
submit = SubmitField("Create")
def validate_collection_id(self, field):
collection_id = field.data
collection: m.Collection = db.session.get(m.Collection, collection_id)
if collection and not collection.is_leaf:
log(log.WARNING, "Collection [%s] it not leaf", collection)
raise ValidationError("You can't create section for this collection")
def validate_label(self, field):
label = field.data
collection_id = self.collection_id.data
section: m.Section = m.Section.query.filter_by(
is_deleted=False, label=label, collection_id=collection_id
).first()
if section:
log(
log.WARNING,
"Section with label [%s] already exists: [%s]",
label,
section,
)
raise ValidationError("Section 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

@ -19,3 +19,7 @@ class Book(BaseModel):
def __repr__(self):
return f"<{self.id}: {self.label}>"
@property
def last_version(self):
return self.versions[-1]

View File

@ -636,3 +636,223 @@ def collection_delete(
book_id=book_id,
)
)
###############
# Sections CRUD
###############
@bp.route("/<int:book_id>/<int:collection_id>/create_section", methods=["POST"])
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/create_section",
methods=["POST"],
)
@login_required
def section_create(
book_id: int, collection_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.collection_view", book_id=book_id)
if collection_id:
redirect_url = url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
)
form = f.CreateSectionForm()
if form.validate_on_submit():
section: m.Section = m.Section(
label=form.label.data,
about=form.about.data,
collection_id=sub_collection_id or collection_id,
version_id=book.last_version.id,
)
log(log.INFO, "Create section [%s]. Collection: [%s]", section, collection_id)
section.save()
flash("Success!", "success")
return redirect(redirect_url)
else:
log(log.ERROR, "Section 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>/edit_section", methods=["POST"]
)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>/edit_section",
methods=["POST"],
)
@login_required
def section_edit(
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.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.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 section.is_deleted:
log(log.WARNING, "Section with id [%s] not found", section_id)
flash("Section not found", "danger")
return redirect(redirect_url)
form = f.EditSectionForm()
if form.validate_on_submit():
label = form.label.data
if label:
section.label = label
about = form.about.data
if about:
section.about = about
log(log.INFO, "Edit section [%s]", section.id)
section.save()
flash("Success!", "success")
if sub_collection_id:
redirect_url = url_for(
"book.section_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
)
return redirect(redirect_url)
else:
log(log.ERROR, "Section 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>/delete_section",
methods=["POST"],
)
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/<int:section_id>/delete_section",
methods=["POST"],
)
@login_required
def section_delete(
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.owner != current_user:
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.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 section.is_deleted:
log(log.WARNING, "Section with id [%s] not found", section_id)
flash("Section not found", "danger")
return redirect(redirect_url)
section.is_deleted = True
log(log.INFO, "Delete section [%s]", section.id)
section.save()
flash("Success!", "success")
return redirect(
url_for(
"book.collection_view",
book_id=book_id,
)
)

View File

@ -338,12 +338,12 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
leaf_collection: m.Collection = m.Collection(
label="Test Leaf Collection #1 Label",
version_id=book.versions[-1].id,
version_id=book.last_version.id,
is_leaf=True,
parent_id=book.versions[-1].root_collection.id,
parent_id=book.last_version.root_collection.id,
).save()
collection: m.Collection = m.Collection(
label="Test Collection #1 Label", version_id=book.versions[-1].id
label="Test Collection #1 Label", version_id=book.last_version.id
).save()
response: Response = client.post(
@ -453,3 +453,248 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
assert response.status_code == 200
assert b"Collection not found" in response.data
def test_crud_sections(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()
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()
leaf_collection.is_leaf = False
leaf_collection.save()
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/create_section",
data=dict(
collection_id=leaf_collection.id,
label="Test Section",
about="Test Section #1 About",
),
follow_redirects=True,
)
assert b"You can't create section for this collection" in response.data
leaf_collection.is_leaf = True
leaf_collection.save()
label_1 = "Test Section #1 Label"
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/create_section",
data=dict(
collection_id=leaf_collection.id,
label=label_1,
about="Test Section #1 About",
),
follow_redirects=True,
)
assert response.status_code == 200
section: m.Section = m.Section.query.filter_by(
label=label_1, collection_id=leaf_collection.id
).first()
assert section
assert section.collection_id == leaf_collection.id
assert section.version_id == book.last_version.id
assert not section.interpretations
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/create_section",
data=dict(
collection_id=leaf_collection.id,
label=label_1,
about="Test Section #1 About",
),
follow_redirects=True,
)
assert b"Section label must be unique!" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/create_section",
data=dict(
collection_id=sub_collection.id,
label=label_1,
about="Test Section #1 About",
),
follow_redirects=True,
)
assert response.status_code == 200
section: m.Section = m.Section.query.filter_by(
label=label_1, collection_id=sub_collection.id
).first()
assert section
assert section.collection_id == sub_collection.id
assert section.version_id == book.last_version.id
assert not section.interpretations
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/create_section",
data=dict(
collection_id=sub_collection.id,
label=label_1,
about="Test Section #1 About",
),
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}/999/create_section",
data=dict(
collection_id=999,
label=label_1,
about="Test Section #1 About",
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection not found" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/999/create_section",
data=dict(collection_id=999, label=label_1, about="Test Section #1 About"),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Subcollection not found" in response.data
# edit
m.Section(
label="Test",
about="Test",
collection_id=leaf_collection.id,
version_id=book.last_version.id,
).save()
m.Section(
label="Test",
about="Test",
collection_id=sub_collection.id,
version_id=book.last_version.id,
).save()
section: m.Section = m.Section.query.filter_by(
label=label_1, collection_id=leaf_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,
)
assert response.status_code == 200
assert b"Section label must be unique!" in response.data
new_label = "Test Section #1 Label(edited)"
new_about = "Test Section #1 About(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
edited_section: m.Section = m.Section.query.filter_by(
label=new_label, about=new_about, id=section.id
).first()
assert edited_section
#
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}/{collection.id}/{leaf_collection.id}/{section.id}/delete_section",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
deleted_section: m.Section = db.session.get(m.Section, section.id)
assert deleted_section.is_deleted
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_2.id}/delete_section",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
deleted_section: m.Section = db.session.get(m.Section, section_2.id)
assert deleted_section.is_deleted
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/999/delete_section",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Section not found" in response.data