end work on subcollection CRUD(front end)

This commit is contained in:
SvyatoslavArtymovych 2023-04-27 17:44:00 +03:00
parent 8c701d68b6
commit bd14929369
11 changed files with 261 additions and 40 deletions

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

@ -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

@ -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)}}" >
@ -76,8 +75,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"))
@ -389,7 +392,9 @@ def collection_create(book_id: int, collection_id: int | None = None):
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,
parrent_id=book.versions[-1].root_collection.id,
)
if collection_id:
collection.parrent_id = collection_id
@ -399,6 +404,10 @@ def collection_create(book_id: int, collection_id: int | None = None):
collection.save()
flash("Success!", "success")
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, "Collection/Subcollection create errors: [%s]", form.errors)
@ -428,6 +437,7 @@ def collection_edit(
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:
@ -440,6 +450,7 @@ def collection_edit(
collection_id=collection_id,
)
)
collection_to_edit = sub_collection
form = f.EditCollectionForm()
redirect_url = url_for(
@ -453,7 +464,7 @@ def collection_edit(
if (
m.Collection.query.filter_by(label=label, version_id=book.versions[-1].id)
.filter(m.Collection.id != collection_id)
.filter(m.Collection.id != collection_to_edit.id)
.first()
):
log(
@ -467,16 +478,23 @@ def collection_edit(
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, "Collection edit errors: [%s]", form.errors)
@ -507,6 +525,7 @@ def collection_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_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:
@ -519,11 +538,12 @@ def collection_delete(
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

@ -0,0 +1,36 @@
"""empty message
Revision ID: 02000d4f9eea
Revises: e96f96cb7d02
Create Date: 2023-04-27 17:17:27.670334
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '02000d4f9eea'
down_revision = 'e96f96cb7d02'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('collections', schema=None) as batch_op:
batch_op.alter_column('about',
existing_type=sa.TEXT(),
nullable=True)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('collections', schema=None) as batch_op:
batch_op.alter_column('about',
existing_type=sa.TEXT(),
nullable=False)
# ### end Alembic commands ###