From fce4b9c56f2211b5cab8358ca3fbf7d5f7a3b392 Mon Sep 17 00:00:00 2001 From: SvyatoslavArtymovych Date: Tue, 2 May 2023 17:07:37 +0300 Subject: [PATCH 1/4] section CRUD backend --- app/forms/__init__.py | 1 + app/forms/section.py | 68 ++++++++++++ app/models/book.py | 4 + app/views/book.py | 220 ++++++++++++++++++++++++++++++++++++ tests/test_book.py | 251 +++++++++++++++++++++++++++++++++++++++++- 5 files changed, 541 insertions(+), 3 deletions(-) create mode 100644 app/forms/section.py diff --git a/app/forms/__init__.py b/app/forms/__init__.py index 060813d..74aa770 100644 --- a/app/forms/__init__.py +++ b/app/forms/__init__.py @@ -8,3 +8,4 @@ from .contributor import ( EditContributorRoleForm, ) from .collection import CreateCollectionForm, EditCollectionForm +from .section import CreateSectionForm, EditSectionForm diff --git a/app/forms/section.py b/app/forms/section.py new file mode 100644 index 0000000..ba6957f --- /dev/null +++ b/app/forms/section.py @@ -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!") diff --git a/app/models/book.py b/app/models/book.py index 1541c47..6d0d496 100644 --- a/app/models/book.py +++ b/app/models/book.py @@ -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] diff --git a/app/views/book.py b/app/views/book.py index 80b080f..66fe9df 100644 --- a/app/views/book.py +++ b/app/views/book.py @@ -636,3 +636,223 @@ def collection_delete( book_id=book_id, ) ) + + +############### +# Sections CRUD +############### + + +@bp.route("///create_section", methods=["POST"]) +@bp.route( + "////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( + "////edit_section", methods=["POST"] +) +@bp.route( + "/////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( + "////delete_section", + methods=["POST"], +) +@bp.route( + "/////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, + ) + ) diff --git a/tests/test_book.py b/tests/test_book.py index 3b27d29..9cee04c 100644 --- a/tests/test_book.py +++ b/tests/test_book.py @@ -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 From 00dc7dca7ea61a43f4219534c1708e500a1fa065 Mon Sep 17 00:00:00 2001 From: SvyatoslavArtymovych Date: Tue, 2 May 2023 17:38:27 +0300 Subject: [PATCH 2/4] create section UI --- app/forms/section.py | 4 +- app/templates/book/add_section_modal.html | 43 +++++++++++++++++++++ app/templates/book/right_sidebar.html | 10 +++++ app/templates/book/section_view.html | 4 ++ app/templates/book/sub_collection_view.html | 3 ++ app/views/book.py | 6 ++- tests/test_book.py | 4 +- 7 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 app/templates/book/add_section_modal.html diff --git a/app/forms/section.py b/app/forms/section.py index ba6957f..023e122 100644 --- a/app/forms/section.py +++ b/app/forms/section.py @@ -19,7 +19,9 @@ class CreateSectionForm(BaseSectionForm): collection_id = field.data collection: m.Collection = db.session.get(m.Collection, collection_id) - if collection and not collection.is_leaf: + if not collection or [ + child for child in collection.children if not child.is_deleted + ]: log(log.WARNING, "Collection [%s] it not leaf", collection) raise ValidationError("You can't create section for this collection") diff --git a/app/templates/book/add_section_modal.html b/app/templates/book/add_section_modal.html new file mode 100644 index 0000000..a811884 --- /dev/null +++ b/app/templates/book/add_section_modal.html @@ -0,0 +1,43 @@ + + + diff --git a/app/templates/book/right_sidebar.html b/app/templates/book/right_sidebar.html index 0bbfdf3..a23bf24 100644 --- a/app/templates/book/right_sidebar.html +++ b/app/templates/book/right_sidebar.html @@ -22,6 +22,16 @@ {% endif %} + {% if show_create_section %} +
  • + + +
  • + {% endif %} + {% if show_edit_collection %}
  • diff --git a/app/templates/book/section_view.html b/app/templates/book/section_view.html index c8eea25..bfdeeb8 100644 --- a/app/templates/book/section_view.html +++ b/app/templates/book/section_view.html @@ -5,10 +5,14 @@ {% set show_edit_collection = True %} {% set show_delete_collection = True %} + +{% set show_create_section = True %} + {% include 'book/edit_collection_modal.html' %} {% include 'book/delete_collection_modal.html' %} +{% include 'book/add_section_modal.html' %} {% endif %} diff --git a/app/templates/book/sub_collection_view.html b/app/templates/book/sub_collection_view.html index 40f3452..86e36a7 100644 --- a/app/templates/book/sub_collection_view.html +++ b/app/templates/book/sub_collection_view.html @@ -8,11 +8,14 @@ {% set show_delete_collection = True %} {% set show_create_collection = True %} + +{% set show_create_section = collection.is_leaf or not collection.children %} {% include 'book/edit_collection_modal.html' %} {% include 'book/delete_collection_modal.html' %} {% include 'book/add_collection_modal.html' %} +{% include 'book/add_section_modal.html' %} {% endif %} diff --git a/app/views/book.py b/app/views/book.py index 66fe9df..07f28ce 100644 --- a/app/views/book.py +++ b/app/views/book.py @@ -681,7 +681,7 @@ def section_create( redirect_url = url_for("book.collection_view", book_id=book_id) if collection_id: redirect_url = url_for( - "book.sub_collection_view", + "book.section_view", book_id=book_id, collection_id=collection_id, sub_collection_id=sub_collection_id, @@ -696,6 +696,10 @@ def section_create( collection_id=sub_collection_id or collection_id, version_id=book.last_version.id, ) + if sub_collection: + sub_collection.is_leaf = True + else: + collection.is_leaf = True log(log.INFO, "Create section [%s]. Collection: [%s]", section, collection_id) section.save() diff --git a/tests/test_book.py b/tests/test_book.py index 9cee04c..d1b7bbc 100644 --- a/tests/test_book.py +++ b/tests/test_book.py @@ -485,9 +485,9 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner): leaf_collection.is_leaf = False leaf_collection.save() response: Response = client.post( - f"/book/{book.id}/{leaf_collection.id}/create_section", + f"/book/{book.id}/{collection.id}/create_section", data=dict( - collection_id=leaf_collection.id, + collection_id=collection.id, label="Test Section", about="Test Section #1 About", ), From dc23dcfeca2ed235386c384582b59ae7ff1a18d9 Mon Sep 17 00:00:00 2001 From: SvyatoslavArtymovych Date: Wed, 3 May 2023 10:04:31 +0300 Subject: [PATCH 3/4] delete section UI, fix create section/subcollection --- app/forms/section.py | 4 +-- app/models/collection.py | 12 +++++++++ app/templates/book/delete_section_modal.html | 26 ++++++++++++++++++++ app/templates/book/interpretation_view.html | 11 +++++++++ app/templates/book/right_sidebar.html | 10 ++++++++ app/templates/book/section_view.html | 5 ++-- app/templates/book/sub_collection_view.html | 8 ++---- app/views/book.py | 14 +++++++++-- 8 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 app/templates/book/delete_section_modal.html diff --git a/app/forms/section.py b/app/forms/section.py index 023e122..82aecf1 100644 --- a/app/forms/section.py +++ b/app/forms/section.py @@ -19,9 +19,7 @@ class CreateSectionForm(BaseSectionForm): collection_id = field.data collection: m.Collection = db.session.get(m.Collection, collection_id) - if not collection or [ - child for child in collection.children if not child.is_deleted - ]: + if not collection or collection.sub_collections: log(log.WARNING, "Collection [%s] it not leaf", collection) raise ValidationError("You can't create section for this collection") diff --git a/app/models/collection.py b/app/models/collection.py index cec8a90..3053de8 100644 --- a/app/models/collection.py +++ b/app/models/collection.py @@ -25,3 +25,15 @@ class Collection(BaseModel): def __repr__(self): return f"<{self.id}: {self.label}>" + + @property + def active_sections(self): + return [section for section in self.sections if not section.is_deleted] + + @property + def sub_collections(self): + return [ + sub_collection + for sub_collection in self.children + if not sub_collection.is_deleted + ] diff --git a/app/templates/book/delete_section_modal.html b/app/templates/book/delete_section_modal.html new file mode 100644 index 0000000..209630b --- /dev/null +++ b/app/templates/book/delete_section_modal.html @@ -0,0 +1,26 @@ + + diff --git a/app/templates/book/interpretation_view.html b/app/templates/book/interpretation_view.html index 741b734..c49074e 100644 --- a/app/templates/book/interpretation_view.html +++ b/app/templates/book/interpretation_view.html @@ -1,5 +1,16 @@ {% extends 'base.html' %} + +{% include 'book/delete_section_modal.html' %} + + +{% set show_delete_section = True %} + + +{% block right_sidebar %} + {% include 'book/right_sidebar.html' %} +{% endblock %} + {% block content %}
    {% include 'book/breadcrumbs_navigation.html'%} diff --git a/app/templates/book/right_sidebar.html b/app/templates/book/right_sidebar.html index a23bf24..f180051 100644 --- a/app/templates/book/right_sidebar.html +++ b/app/templates/book/right_sidebar.html @@ -52,6 +52,16 @@
  • {% endif %} + + {% if show_delete_section %} +
  • + + +
  • + {% endif %} diff --git a/app/templates/book/section_view.html b/app/templates/book/section_view.html index bfdeeb8..fec5c9b 100644 --- a/app/templates/book/section_view.html +++ b/app/templates/book/section_view.html @@ -1,18 +1,17 @@ {% extends 'base.html' %} {% if book.owner.id == current_user.id %} - {% set show_edit_collection = True %} - {% set show_delete_collection = True %} - {% set show_create_section = True %} +{% set show_create_collection = not collection.sub_collections and not collection.is_leaf %} {% include 'book/edit_collection_modal.html' %} {% include 'book/delete_collection_modal.html' %} {% include 'book/add_section_modal.html' %} +{% include 'book/add_collection_modal.html' %} {% endif %} diff --git a/app/templates/book/sub_collection_view.html b/app/templates/book/sub_collection_view.html index 86e36a7..13c4308 100644 --- a/app/templates/book/sub_collection_view.html +++ b/app/templates/book/sub_collection_view.html @@ -2,14 +2,10 @@ {% extends 'base.html' %} {% if book.owner.id == current_user.id %} - {% set show_edit_collection = True %} - {% set show_delete_collection = True %} - {% set show_create_collection = True %} - -{% set show_create_section = collection.is_leaf or not collection.children %} +{% set show_create_section = not collection.sub_collections or collection.is_leaf or not collection.children %} {% include 'book/edit_collection_modal.html' %} @@ -56,7 +52,7 @@
    - {% for sub_collection in collection.children %} + {% for sub_collection in collection.sub_collections %}
    diff --git a/app/views/book.py b/app/views/book.py index 07f28ce..c323e15 100644 --- a/app/views/book.py +++ b/app/views/book.py @@ -173,9 +173,9 @@ def section_view( ) if sub_collection: - sections = sub_collection.sections + sections = sub_collection.active_sections else: - sections = collection.sections + sections = collection.active_sections breadcrumbs = create_breadcrumbs( book_id=book_id, @@ -823,6 +823,8 @@ def section_delete( 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)) + + collection_to_edit = collection 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: @@ -835,6 +837,7 @@ def section_delete( collection_id=collection_id, ) ) + collection_to_edit = sub_collection redirect_url = url_for( "book.section_view", @@ -849,6 +852,13 @@ def section_delete( return redirect(redirect_url) section.is_deleted = True + if not collection_to_edit.active_sections: + log( + log.INFO, + "Section [%s] has no active section. Set is_leaf = False", + section.id, + ) + collection_to_edit.is_leaf = False log(log.INFO, "Delete section [%s]", section.id) section.save() From 2b330342d60f47a36bc4211dd67f0d9b023582e7 Mon Sep 17 00:00:00 2001 From: SvyatoslavArtymovych Date: Wed, 3 May 2023 10:16:03 +0300 Subject: [PATCH 4/4] edit section UI --- app/templates/book/edit_section_modal.html | 43 +++++++++++++++++++++ app/templates/book/interpretation_view.html | 2 + app/templates/book/right_sidebar.html | 11 +++++- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 app/templates/book/edit_section_modal.html diff --git a/app/templates/book/edit_section_modal.html b/app/templates/book/edit_section_modal.html new file mode 100644 index 0000000..5500378 --- /dev/null +++ b/app/templates/book/edit_section_modal.html @@ -0,0 +1,43 @@ + + diff --git a/app/templates/book/interpretation_view.html b/app/templates/book/interpretation_view.html index c49074e..ede4f2e 100644 --- a/app/templates/book/interpretation_view.html +++ b/app/templates/book/interpretation_view.html @@ -2,9 +2,11 @@ {% extends 'base.html' %} {% include 'book/delete_section_modal.html' %} +{% include 'book/edit_section_modal.html' %} {% set show_delete_section = True %} +{% set show_edit_section = True %} {% block right_sidebar %} diff --git a/app/templates/book/right_sidebar.html b/app/templates/book/right_sidebar.html index f180051..884b1c9 100644 --- a/app/templates/book/right_sidebar.html +++ b/app/templates/book/right_sidebar.html @@ -32,7 +32,6 @@ {% endif %} - {% if show_edit_collection %}
  • @@ -43,6 +42,16 @@
  • {% endif %} + {% if show_edit_section %} +
  • + + +
  • + {% endif %} + {% if show_delete_collection %}