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..82aecf1 --- /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 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") + + 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/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/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/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/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 741b734..ede4f2e 100644 --- a/app/templates/book/interpretation_view.html +++ b/app/templates/book/interpretation_view.html @@ -1,5 +1,18 @@ {% 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 %} + {% 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 0bbfdf3..884b1c9 100644 --- a/app/templates/book/right_sidebar.html +++ b/app/templates/book/right_sidebar.html @@ -22,6 +22,15 @@ {% endif %} + {% if show_create_section %} +
  • + + +
  • + {% endif %} {% if show_edit_collection %}
  • @@ -33,6 +42,16 @@
  • {% endif %} + {% if show_edit_section %} +
  • + + +
  • + {% endif %} + {% if show_delete_collection %}
  • @@ -42,6 +61,16 @@
  • {% endif %} + + {% if show_delete_section %} +
  • + + +
  • + {% endif %}
    diff --git a/app/templates/book/section_view.html b/app/templates/book/section_view.html index c8eea25..fec5c9b 100644 --- a/app/templates/book/section_view.html +++ b/app/templates/book/section_view.html @@ -1,14 +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 40f3452..13c4308 100644 --- a/app/templates/book/sub_collection_view.html +++ b/app/templates/book/sub_collection_view.html @@ -2,17 +2,16 @@ {% 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 = not collection.sub_collections or 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 %} @@ -53,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 80b080f..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, @@ -636,3 +636,237 @@ 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.section_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, + ) + 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() + + 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)) + + 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: + 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, + ) + ) + collection_to_edit = sub_collection + + 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 + 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() + + 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..d1b7bbc 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}/{collection.id}/create_section", + data=dict( + collection_id=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