Merge pull request #20 from Simple2B/svyat/feat/sections_crud

Svyat/feat/sections crud
This commit is contained in:
Svyatoslav Artymovych 2023-05-04 14:24:48 +03:00 committed by GitHub
commit ddc621dbc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 731 additions and 11 deletions

View File

@ -8,3 +8,4 @@ from .contributor import (
EditContributorRoleForm, EditContributorRoleForm,
) )
from .collection import CreateCollectionForm, EditCollectionForm 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 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!")

View File

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

View File

@ -25,3 +25,15 @@ class Collection(BaseModel):
def __repr__(self): def __repr__(self):
return f"<{self.id}: {self.label}>" 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
]

View File

@ -0,0 +1,43 @@
<!-- Add collection modal -->
<!-- prettier-ignore-->
<div id="add-section-modal" tabindex="-1" aria-hidden="true" class="fixed top-0 left-0 right-0 z-50 hidden w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-[calc(100%-1rem)] max-h-full">
<div class="relative w-full max-w-2xl max-h-full">
<!-- Modal content -->
<form
{% if sub_collection %}
action="{{ url_for('book.section_create', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id) }}"
{% else %}
action="{{ url_for('book.section_create', book_id=book.id, collection_id=collection.id) }}"
{% endif %}
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
{{ form_hidden_tag() }}
<input type="hidden" name="collection_id" id="collection_id" value="{{sub_collection.id if sub_collection else collection.id}}" />
<!-- Modal header -->
<div class="flex items-start justify-between p-4 border-b rounded-t dark:border-gray-600">
<h3 class="text-xl font-semibold text-gray-900 dark:text-white"> Add Section </h3>
<button id="modalAddCloseButton" data-modal-hide="add-section-modal" type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white"> <svg aria-hidden="true" class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path> </svg> </button>
</div>
<!-- Modal body -->
<div class="p-6 space-y-6">
<div class="grid gap-6">
<div class="col-span-6 sm:col-span-3">
<label for="label" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white" >Label</label >
<input type="text" name="label" id="label" class="shadow-sm bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-600 focus:border-blue-600 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Collection label" required />
</div>
</div>
</div>
<div class="p-6 pt-0 space-y-6">
<div class="grid gap-6">
<div class="col-span-6 sm:col-span-3">
<label for="about-collection" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white" >About</label >
<textarea name="about" id="about-collection" rows="4" class="shadow-sm bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-600 focus:border-blue-600 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="About collection..."></textarea>
</div>
</div>
</div>
<!-- Modal footer -->
<div class="flex items-center p-6 space-x-2 border-t border-gray-200 rounded-b dark:border-gray-600">
<button name="submit" type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Create</button>
</div>
</form>
</div>
</div>

View File

@ -0,0 +1,26 @@
<!-- prettier-ignore-->
<div id="delete-collection-modal" tabindex="-1" aria-hidden="true" class="fixed top-0 left-0 right-0 z-50 hidden w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-[calc(100%-1rem)] max-h-full">
<div class="relative w-full max-w-2xl max-h-full">
<!-- Modal content -->
<form
{% if sub_collection %}
action="{{ url_for('book.section_delete', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id, section_id=section.id) }}"
{% else %}
action="{{ url_for('book.section_delete', book_id=book.id, collection_id=collection.id, section_id=section.id) }}"
{% endif %}
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
{{ form_hidden_tag() }}
<!-- Modal header -->
<div class="flex items-start justify-between p-4 border-b rounded-t dark:border-gray-600">
<h3 class="text-xl font-semibold text-gray-900 dark:text-white"> Delete Section </h3>
<button id="modalAddCloseButton" data-modal-hide="delete-collection-modal" type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white"> <svg aria-hidden="true" class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path> </svg> </button>
</div>
<!-- Modal body -->
<!-- Modal footer -->
<div class="flex items-center p-6 space-x-2 border-t border-gray-200 rounded-b dark:border-gray-600">
<button name="submit" type="submit" class="text-white bg-red-700 hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-800">Confirm Deletion</button>
</div>
</form>
</div>
</div>

View File

@ -0,0 +1,43 @@
<!-- prettier-ignore-->
<div id="edit-section-modal" tabindex="-1" aria-hidden="true" class="fixed top-0 left-0 right-0 z-50 hidden w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-[calc(100%-1rem)] max-h-full">
<div class="relative w-full max-w-2xl max-h-full">
<!-- Modal content -->
<form
{% if sub_collection %}
action="{{ url_for('book.section_edit', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id, section_id=section.id) }}"
{% else %}
action="{{ url_for('book.section_edit', book_id=book.id, collection_id=collection.id, section_id=section.id) }}"
{% endif %}
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
{{ form_hidden_tag() }}
<input type="hidden" name="section_id" id="section_id" value="{{section.id}}" />
<!-- Modal header -->
<div class="flex items-start justify-between p-4 border-b rounded-t dark:border-gray-600">
<h3 class="text-xl font-semibold text-gray-900 dark:text-white"> Edit Section </h3>
<button id="modalAddCloseButton" data-modal-hide="edit-section-modal" type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white"> <svg aria-hidden="true" class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path> </svg> </button>
</div>
<!-- Modal body -->
<div class="p-6 space-y-6">
<div class="grid gap-6">
<div class="col-span-6 sm:col-span-3">
<label for="label" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white" >Label</label >
<input value="{{section.label}}" type="text" name="label" id="label" class="shadow-sm bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-600 focus:border-blue-600 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Collection label" required />
</div>
</div>
</div>
<div class="p-6 pt-0 space-y-6">
<div class="grid gap-6">
<div class="col-span-6 sm:col-span-3">
<label for="about-collection" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white" >About</label >
<textarea name="about" id="about-collection" rows="4" class="shadow-sm bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-600 focus:border-blue-600 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="About collection...">{{section.about}}</textarea>
</div>
</div>
</div>
<!-- Modal footer -->
<div class="flex items-center p-6 space-x-2 border-t border-gray-200 rounded-b dark:border-gray-600">
<button name="submit" type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Save</button>
</div>
</form>
</div>
</div>

View File

@ -1,5 +1,18 @@
<!-- prettier-ignore --> <!-- prettier-ignore -->
{% extends 'base.html' %} {% extends 'base.html' %}
{% include 'book/delete_section_modal.html' %}
{% include 'book/edit_section_modal.html' %}
<!-- show delete section btn on rightside bar -->
{% set show_delete_section = True %}
{% set show_edit_section = True %}
<!-- prettier-ignore -->
{% block right_sidebar %}
{% include 'book/right_sidebar.html' %}
{% endblock %}
{% block content %} {% block content %}
<div class="relative overflow-x-auto shadow-md sm:rounded-lg mt-5 md:mr-64"> <div class="relative overflow-x-auto shadow-md sm:rounded-lg mt-5 md:mr-64">
{% include 'book/breadcrumbs_navigation.html'%} {% include 'book/breadcrumbs_navigation.html'%}

View File

@ -22,6 +22,15 @@
</li> </li>
{% endif %} {% endif %}
{% if show_create_section %}
<li>
<!-- prettier-ignore -->
<button type="button" data-modal-target="add-section-modal" data-modal-toggle="add-section-modal" class="space-x-3 text-white ml-2 w-11/12 bg-emerald-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-emerald-300 font-medium rounded-lg text-sm px-4 py-2.5 text-center inline-flex items-center dark:bg-emerald-600 dark:hover:bg-emerald-700 dark:focus:ring-emerald-800">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"> <path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" /> </svg>
<p>New Section</p>
</button>
</li>
{% endif %}
{% if show_edit_collection %} {% if show_edit_collection %}
<li> <li>
@ -33,6 +42,16 @@
</li> </li>
{% endif %} {% endif %}
{% if show_edit_section %}
<li>
<!-- prettier-ignore -->
<button type="button" data-modal-target="edit-section-modal" data-modal-toggle="edit-section-modal" class="space-x-3 text-white ml-2 w-11/12 bg-teal-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-teal-300 font-medium rounded-lg text-sm px-4 py-2.5 text-center inline-flex items-center dark:bg-teal-600 dark:hover:bg-teal-700 dark:focus:ring-teal-800">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"> <path stroke-linecap="round" stroke-linejoin="round" d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L10.582 16.07a4.5 4.5 0 01-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 011.13-1.897l8.932-8.931zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0115.75 21H5.25A2.25 2.25 0 013 18.75V8.25A2.25 2.25 0 015.25 6H10" /> </svg>
<p>Edit Section</p>
</button>
</li>
{% endif %}
{% if show_delete_collection %} {% if show_delete_collection %}
<li> <li>
<!-- prettier-ignore --> <!-- prettier-ignore -->
@ -42,6 +61,16 @@
</button> </button>
</li> </li>
{% endif %} {% endif %}
{% if show_delete_section %}
<li>
<!-- prettier-ignore -->
<button type="button" data-modal-target="collection-collection-modal" data-modal-toggle="delete-collection-modal" class="space-x-3 text-white ml-2 w-11/12 bg-red-700 hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 font-medium rounded-lg text-sm px-4 py-2.5 text-center inline-flex items-center dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-800">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"> <path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" /> </svg>
<p>Delete Section</p>
</button>
</li>
{% endif %}
</ul> </ul>
</div> </div>
</aside> </aside>

View File

@ -1,14 +1,17 @@
<!-- prettier-ignore --> <!-- prettier-ignore -->
{% extends 'base.html' %} {% extends 'base.html' %}
{% if book.owner.id == current_user.id %} {% if book.owner.id == current_user.id %}
<!-- show edit collection btn on rightside bar -->
{% set show_edit_collection = True %} {% set show_edit_collection = True %}
<!-- show delete collection btn on rightside bar -->
{% set show_delete_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 %}
<!-- prettier-ignore --> <!-- prettier-ignore -->
{% include 'book/edit_collection_modal.html' %} {% include 'book/edit_collection_modal.html' %}
{% include 'book/delete_collection_modal.html' %} {% include 'book/delete_collection_modal.html' %}
{% include 'book/add_section_modal.html' %}
{% include 'book/add_collection_modal.html' %}
{% endif %} {% endif %}
<!-- prettier-ignore --> <!-- prettier-ignore -->

View File

@ -2,17 +2,16 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% if book.owner.id == current_user.id %} {% if book.owner.id == current_user.id %}
<!-- show edit collection btn on rightside bar -->
{% set show_edit_collection = True %} {% set show_edit_collection = True %}
<!-- show delete collection btn on rightside bar -->
{% set show_delete_collection = True %} {% set show_delete_collection = True %}
<!-- show create collection btn on rightside bar -->
{% set show_create_collection = True %} {% set show_create_collection = True %}
{% set show_create_section = not collection.sub_collections or collection.is_leaf or not collection.children %}
<!-- prettier-ignore --> <!-- prettier-ignore -->
{% include 'book/edit_collection_modal.html' %} {% include 'book/edit_collection_modal.html' %}
{% include 'book/delete_collection_modal.html' %} {% include 'book/delete_collection_modal.html' %}
{% include 'book/add_collection_modal.html' %} {% include 'book/add_collection_modal.html' %}
{% include 'book/add_section_modal.html' %}
{% endif %} {% endif %}
@ -53,7 +52,7 @@
<!-- prettier-ignore --> <!-- prettier-ignore -->
<dl class="w-md md:w-full text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700"> <dl class="w-md md:w-full text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700">
<!-- prettier-ignore --> <!-- prettier-ignore -->
{% for sub_collection in collection.children %} {% for sub_collection in collection.sub_collections %}
<!-- prettier-ignore --> <!-- prettier-ignore -->
<a href="{{url_for('book.section_view',book_id=book.id,collection_id=collection.id,sub_collection_id=sub_collection.id)}}"> <a href="{{url_for('book.section_view',book_id=book.id,collection_id=collection.id,sub_collection_id=sub_collection.id)}}">
<dl class="bg-white dark:bg-gray-900 max-w-full p-3 text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700 m-3 border-2 border-gray-200 border-solid rounded-lg dark:border-gray-700"> <dl class="bg-white dark:bg-gray-900 max-w-full p-3 text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700 m-3 border-2 border-gray-200 border-solid rounded-lg dark:border-gray-700">

View File

@ -173,9 +173,9 @@ def section_view(
) )
if sub_collection: if sub_collection:
sections = sub_collection.sections sections = sub_collection.active_sections
else: else:
sections = collection.sections sections = collection.active_sections
breadcrumbs = create_breadcrumbs( breadcrumbs = create_breadcrumbs(
book_id=book_id, book_id=book_id,
@ -636,3 +636,237 @@ def collection_delete(
book_id=book_id, 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.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(
"/<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))
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,
)
)

View File

@ -338,12 +338,12 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
leaf_collection: m.Collection = m.Collection( leaf_collection: m.Collection = m.Collection(
label="Test Leaf Collection #1 Label", label="Test Leaf Collection #1 Label",
version_id=book.versions[-1].id, version_id=book.last_version.id,
is_leaf=True, is_leaf=True,
parent_id=book.versions[-1].root_collection.id, parent_id=book.last_version.root_collection.id,
).save() ).save()
collection: m.Collection = m.Collection( 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() ).save()
response: Response = client.post( response: Response = client.post(
@ -453,3 +453,248 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
assert response.status_code == 200 assert response.status_code == 200
assert b"Collection not found" in response.data 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