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()