Merge branch 'develop' into kostia/feature/paginations

This commit is contained in:
Kostiantyn Stoliarskyi 2023-04-28 11:48:39 +03:00
commit c2b4785006
15 changed files with 342 additions and 73 deletions

View File

@ -3,15 +3,14 @@ from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, Length
class CreateCollectionForm(FlaskForm):
label = StringField("Label", [DataRequired(), Length(6, 256)])
class BaseCollectionForm(FlaskForm):
label = StringField("Label", [DataRequired(), Length(3, 256)])
about = StringField("About")
class CreateCollectionForm(BaseCollectionForm):
submit = SubmitField("Create")
class EditCollectionForm(FlaskForm):
label = StringField("Label", [Length(6, 256)])
about = StringField("About")
class EditCollectionForm(BaseCollectionForm):
submit = SubmitField("Edit")

View File

@ -25,3 +25,13 @@ class BookVersion(BaseModel):
def __repr__(self):
return f"<{self.id}: {self.semver}>"
@property
def root_collection(self):
for collection in self.collections:
if collection.is_root:
return collection
@property
def children_collections(self):
return self.root_collection.children

File diff suppressed because one or more lines are too long

View File

@ -58,7 +58,7 @@
</div>
<!-- prettier-ignore -->
{% endif %}
<div class="ml-3 text-sm font-normal">{{message}}</div>
<div class="ml-3 text-sm font-normal">{{message|safe}}</div>
<!-- prettier-ignore -->
<button type="button" class="ml-auto -mx-1.5 -my-1.5 bg-white text-gray-400 hover:text-gray-900 rounded-lg focus:ring-2 focus:ring-gray-300 p-1.5 hover:bg-gray-100 inline-flex h-8 w-8 dark:text-gray-500 dark:hover:text-white dark:bg-gray-800 dark:hover:bg-gray-700" data-dismiss-target="#toast-{{category}}" aria-label="Close">
<span class="sr-only">Close</span>

View File

@ -3,10 +3,16 @@
<div id="add-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 action="{{ url_for('book.collection_create', book_id=book.id) }}" method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
<form
{% if collection %}
action="{{ url_for('book.collection_create', book_id=book.id, collection_id=collection.id) }}"
{% else %}
action="{{ url_for('book.collection_create', book_id=book.id) }}"
{% endif %}
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
<!-- 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 Collection </h3>
<h3 class="text-xl font-semibold text-gray-900 dark:text-white"> Add {% if collection %}Sub {% endif %}Collection </h3>
<button id="modalAddCloseButton" data-modal-hide="add-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 -->

View File

@ -18,10 +18,7 @@
<div class="col-span-6 sm:col-span-3">
<label for="username" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Username</label >
<div class="relative">
<div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
<svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path></svg>
</div>
<input id="username" data-book-id="{{ book.id }}" type="search" id="default-search" class="block w-full p-4 pl-10 text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Search users..." required>
<input id="username" data-book-id="{{ book.id }}" type="search" id="default-search" class="block w-full p-4 text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Search users..." required>
<button id="search-btn" class="cursor-pointer select-none text-white absolute right-2.5 bottom-2.5 bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-4 py-2 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Search</button>
</div>
</div>

View File

@ -48,8 +48,7 @@
<dl
class="w-md md:w-full text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700">
<!-- prettier-ignore -->
{% if book.versions %}
{% for collection in book.versions[-1].collections if not collection.is_root and not collection.is_deleted %}
{% for collection in book.versions[-1].children_collections if not collection.is_root and not collection.is_deleted %}
<!-- prettier-ignore -->
<a href="{{url_for('book.sub_collection_view',book_id=book.id,collection_id=collection.id)}}" >
@ -74,8 +73,6 @@
</a >
{% endfor %}
<!-- prettier-ignore -->
{% endif %}
</dl>
</div>
<!-- prettier-ignore -->

View File

@ -3,7 +3,13 @@
<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 action="{{ url_for('book.collection_delete', book_id=book.id, collection_id=collection.id) }}" method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
<form
{% if sub_collection %}
action="{{ url_for('book.collection_delete', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id) }}"
{% else %}
action="{{ url_for('book.collection_delete', book_id=book.id, collection_id=collection.id) }}"
{% endif %}
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
<!-- 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 Collection </h3>

View File

@ -3,7 +3,13 @@
<div id="edit-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 action="{{ url_for('book.collection_edit', book_id=book.id, collection_id=collection.id) }}" method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
<form
{% if sub_collection %}
action="{{ url_for('book.collection_edit', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id) }}"
{% else %}
action="{{ url_for('book.collection_edit', book_id=book.id, collection_id=collection.id) }}"
{% endif %}
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
<!-- 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 Collection </h3>
@ -14,7 +20,7 @@
<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="{{collection.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 />
<input value="{{sub_collection.label if sub_collection else collection.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>
@ -22,7 +28,7 @@
<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...">{{collection.about}}</textarea>
<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...">{{sub_collection.about if sub_collection else collection.about}}</textarea>
</div>
</div>
</div>

View File

@ -15,17 +15,18 @@
<!-- prettier-ignore -->
<button type="button" data-modal-target="add-collection-modal" data-modal-toggle="add-collection-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 Collection</p>
<p>New {% if collection %}Sub{% endif %}Collection</p>
</button>
</li>
{% endif %}
{% if show_edit_collection %}
<li>
<!-- prettier-ignore -->
<button type="button" data-modal-target="edit-collection-modal" data-modal-toggle="edit-collection-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 Collection</p>
<p>Edit {% if sub_collection %}Sub{% endif %}Collection</p>
</button>
</li>
{% endif %}
@ -35,7 +36,7 @@
<!-- prettier-ignore -->
<button type="button" data-modal-target="delete-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 Collection</p>
<p>Delete {% if sub_collection %}Sub{% endif %}Collection</p>
</button>
</li>
{% endif %}

View File

@ -1,5 +1,20 @@
<!-- prettier-ignore -->
{% extends 'base.html' %}
<!-- show edit collection btn on rightside bar -->
{% set show_edit_collection = True %}
<!-- show delete collection btn on rightside bar -->
{% set show_delete_collection = True %}
<!-- prettier-ignore -->
{% include 'book/edit_collection_modal.html' %}
{% include 'book/delete_collection_modal.html' %}
<!-- prettier-ignore -->
{% block right_sidebar %}
{% include 'book/right_sidebar.html' %}
{% endblock %}
{% block content %}
<div class="relative overflow-x-auto shadow-md sm:rounded-lg mt-5 md:mr-64">
<!-- prettier-ignore -->

View File

@ -5,10 +5,13 @@
{% set show_edit_collection = True %}
<!-- show delete collection btn on rightside bar -->
{% set show_delete_collection = True %}
<!-- show create collection btn on rightside bar -->
{% set show_create_collection = True %}
<!-- prettier-ignore -->
{% include 'book/edit_collection_modal.html' %}
{% include 'book/delete_collection_modal.html' %}
{% include 'book/add_collection_modal.html' %}
{% block right_sidebar %}

View File

@ -60,7 +60,10 @@ def create():
book: m.Book = m.Book(label=form.label.data, user_id=current_user.id)
log(log.INFO, "Form submitted. Book: [%s]", book)
book.save()
m.BookVersion(semver="1.0.0", book_id=book.id).save()
version = m.BookVersion(semver="1.0.0", book_id=book.id).save()
m.Collection(
label="Root Collection", version_id=version.id, is_root=True
).save()
flash("Book added!", "success")
return redirect(url_for("book.my_books"))
@ -76,7 +79,7 @@ def create():
@bp.route("/<int:book_id>", methods=["GET"])
def collection_view(book_id: int):
book = db.session.get(m.Book, book_id)
if not book:
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"))
@ -87,13 +90,13 @@ def collection_view(book_id: int):
@bp.route("/<int:book_id>/<int:collection_id>", methods=["GET"])
def sub_collection_view(book_id: int, collection_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if not book:
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:
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))
@ -113,19 +116,19 @@ def sub_collection_view(book_id: int, collection_id: int):
@bp.route("/<int:book_id>/<int:collection_id>/<int:sub_collection_id>", methods=["GET"])
def section_view(book_id: int, collection_id: int, sub_collection_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if not book:
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:
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: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection:
if not sub_collection or sub_collection.is_deleted:
log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id)
flash("Sub_collection not found", "danger")
return redirect(
@ -150,19 +153,19 @@ def interpretation_view(
book_id: int, collection_id: int, sub_collection_id: int, section_id: int
):
book: m.Book = db.session.get(m.Book, book_id)
if not book:
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:
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: m.Collection = db.session.get(m.Collection, sub_collection_id)
if not sub_collection:
if not sub_collection or sub_collection.is_deleted:
log(log.WARNING, "Sub_collection with id [%s] not found", sub_collection_id)
flash("Sub_collection not found", "danger")
return redirect(
@ -197,7 +200,7 @@ def interpretation_view(
@login_required
def settings(book_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if book.owner != current_user:
if not book or book.is_deleted 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"))
@ -211,7 +214,7 @@ def settings(book_id: int):
@login_required
def add_contributor(book_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.owner != current_user:
if not book or book.is_deleted 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"))
@ -249,7 +252,7 @@ def add_contributor(book_id: int):
@login_required
def delete_contributor(book_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.owner != current_user:
if not book or book.is_deleted 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"))
@ -289,7 +292,7 @@ def delete_contributor(book_id: int):
@login_required
def edit_contributor_role(book_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.owner != current_user:
if not book or book.is_deleted 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"))
@ -332,27 +335,58 @@ def edit_contributor_role(book_id: int):
return redirect(url_for("book.settings", book_id=book_id))
#################
# Collection CRUD
#################
###############################
# Collection/SubCollection CRUD
###############################
@bp.route("/<int:book_id>/create_collection", methods=["POST"])
@bp.route("/<int:book_id>/<int:collection_id>/create_sub_collection", methods=["POST"])
@login_required
def collection_create(book_id: int):
def collection_create(book_id: int, 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"))
if collection_id:
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))
elif collection.is_leaf:
log(log.WARNING, "Collection with id [%s] is leaf", collection_id)
flash("You can't create subcollection for this collection", "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
)
form = f.CreateCollectionForm()
if form.validate_on_submit():
label = form.label.data
collection: m.Collection = m.Collection.query.filter_by(
is_deleted=False, label=label, version_id=book.versions[-1].id
).first()
is_deleted=False,
label=label,
)
if collection_id:
collection = collection.filter_by(parent_id=collection_id)
else:
collection = collection.filter_by(
parent_id=book.versions[-1].root_collection.id
)
collection = collection.first()
if collection:
log(
@ -363,28 +397,43 @@ def collection_create(book_id: int):
label,
)
flash("Collection label must be unique!", "danger")
return redirect(url_for("book.collection_view", book_id=book_id))
return redirect(redirect_url)
collection: m.Collection = m.Collection(
label=label, about=form.about.data, version_id=book.versions[-1].id
label=label,
about=form.about.data,
parent_id=book.versions[-1].root_collection.id,
)
if collection_id:
collection.parent_id = collection_id
collection.is_leaf = True
log(log.INFO, "Create collection [%s]. Book: [%s]", collection, book.id)
collection.save()
flash("Success!", "success")
return redirect(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
)
return redirect(redirect_url)
else:
log(log.ERROR, "Book create errors: [%s]", form.errors)
log(log.ERROR, "Collection/Subcollection 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(url_for("book.settings", book_id=book_id))
return redirect(redirect_url)
@bp.route("/<int:book_id>/<int:collection_id>/edit", methods=["POST"])
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/edit", methods=["POST"]
)
@login_required
def collection_edit(book_id: int, collection_id: int):
def collection_edit(
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.owner != current_user or book.is_deleted:
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
@ -396,6 +445,20 @@ def collection_edit(book_id: int, collection_id: int):
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
form = f.EditCollectionForm()
redirect_url = url_for(
@ -406,12 +469,19 @@ def collection_edit(book_id: int, collection_id: int):
if form.validate_on_submit():
label = form.label.data
collection_query: m.Collection = m.Collection.query.filter_by(
is_deleted=False,
label=label,
).filter(m.Collection.id != collection_to_edit.id)
if (
m.Collection.query.filter_by(label=label, version_id=book.versions[-1].id)
.filter(m.Collection.id != collection_id)
.first()
):
if sub_collection_id:
collection_query = collection_query.filter_by(parent_id=collection_id)
else:
collection_query = collection_query.filter_by(
parent_id=collection_to_edit.parent.id
)
if collection_query.first():
log(
log.INFO,
"Collection with similar label already exists. Book: [%s], Collection: [%s], Label: [%s]",
@ -423,19 +493,26 @@ def collection_edit(book_id: int, collection_id: int):
return redirect(redirect_url)
if label:
collection.label = label
collection_to_edit.label = label
about = form.about.data
if about:
collection.about = about
collection_to_edit.about = about
log(log.INFO, "Edit collection [%s]", collection.id)
collection.save()
log(log.INFO, "Edit collection [%s]", collection_to_edit.id)
collection_to_edit.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, "Book create errors: [%s]", form.errors)
log(log.ERROR, "Collection edit errors: [%s]", form.errors)
for field, errors in form.errors.items():
field_label = form._fields[field].label.text
for error in errors:
@ -444,8 +521,14 @@ def collection_edit(book_id: int, collection_id: int):
@bp.route("/<int:book_id>/<int:collection_id>/delete", methods=["POST"])
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/delete",
methods=["POST"],
)
@login_required
def collection_delete(book_id: int, collection_id: int):
def collection_delete(
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.owner != current_user:
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
@ -457,11 +540,25 @@ def collection_delete(book_id: int, collection_id: int):
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_delete = 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_delete = sub_collection
collection.is_deleted = True
collection_to_delete.is_deleted = True
log(log.INFO, "Delete collection [%s]", collection.id)
collection.save()
log(log.INFO, "Delete collection [%s]", collection_to_delete.id)
collection_to_delete.save()
flash("Success!", "success")
return redirect(

View File

@ -1,8 +1,8 @@
"""empty message
"""init
Revision ID: febf8dfa148f
Revises:
Create Date: 2023-04-27 16:07:14.727110
Revision ID: bbc4b55246ba
Revises:
Create Date: 2023-04-28 10:13:52.011272
"""
from alembic import op
@ -10,7 +10,7 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "febf8dfa148f"
revision = "bbc4b55246ba"
down_revision = None
branch_labels = None
depends_on = None

View File

@ -219,7 +219,9 @@ def test_crud_collection(client: FlaskClient, runner: FlaskCliRunner):
label="Test Collection #1 Label"
).first()
m.Collection(
label="Test Collection #2 Label", version_id=collection.version_id
label="Test Collection #2 Label",
version_id=collection.version_id,
parent_id=collection.parent_id,
).save()
response: Response = client.post(
@ -254,7 +256,7 @@ def test_crud_collection(client: FlaskClient, runner: FlaskCliRunner):
assert edited_collection
response: Response = client.post(
f"/book/{book.id}/0/edit",
f"/book/{book.id}/999/edit",
data=dict(
label=new_label,
about=new_about,
@ -283,3 +285,133 @@ def test_crud_collection(client: FlaskClient, runner: FlaskCliRunner):
assert response.status_code == 200
assert b"Collection not found" in response.data
def test_crud_subcollection(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.versions[-1].id,
is_leaf=True,
parent_id=book.versions[-1].root_collection.id,
).save()
collection: m.Collection = m.Collection(
label="Test Collection #1 Label", version_id=book.versions[-1].id
).save()
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/create_sub_collection",
data=dict(
label="Test SubCollection #1 Label", about="Test SubCollection #1 About"
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"You can't create subcollection for this collection" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/create_sub_collection",
data=dict(
label="Test SubCollection #1 Label", about="Test SubCollection #1 About"
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/create_sub_collection",
data=dict(
label="Test SubCollection #1 Label", about="Test SubCollection #1 About"
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection label must be unique!" in response.data
sub_collection: m.Collection = m.Collection.query.filter_by(
label="Test SubCollection #1 Label"
).first()
assert sub_collection
assert sub_collection.is_leaf
assert sub_collection.parent_id == collection.id
m.Collection(
label="Test SubCollection #2 Label",
version_id=collection.version_id,
parent_id=collection.id,
).save()
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit",
data=dict(
label="Test SubCollection #2 Label",
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection label must be unique!" in response.data
new_label = "Test SubCollection #1 Label(edited)"
new_about = "Test SubCollection #1 About(edited)"
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit",
data=dict(
label=new_label,
about=new_about,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
edited_collection: m.Collection = m.Collection.query.filter_by(
label=new_label, about=new_about
).first()
assert edited_collection
response: Response = client.post(
f"/book/{book.id}/{collection.id}/9999/edit",
data=dict(
label=new_label,
about=new_about,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"SubCollection not found" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
deleted_collection: m.Collection = db.session.get(m.Collection, sub_collection.id)
assert deleted_collection.is_deleted
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection not found" in response.data