Merge branch 'deep_refactor' into kostia/routes_comments

This commit is contained in:
Kostiantyn Stoliarskyi 2023-05-30 10:07:47 +03:00
commit 50a63f8f9e
18 changed files with 78 additions and 381 deletions

View File

@ -70,17 +70,10 @@ def book_validator() -> Response | None:
section_id = request_args.get("section_id")
if section_id:
section: m.Section = db.session.get(m.Section, section_id)
if not section or collection.is_deleted:
if not section:
log(log.WARNING, "Section with id [%s] not found", section)
flash("Section not found", "danger")
return redirect(
url_for(
"book.section_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
)
)
return redirect(url_for("book.collection_view", book_id=book_id))
interpretation_id = request_args.get("interpretation_id")
if interpretation_id:

View File

@ -1,6 +1,7 @@
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, ValidationError
from wtforms.validators import DataRequired, Length
from flask import request
from app import models as m, db
from app.logger import log
@ -12,26 +13,23 @@ class BaseSectionForm(FlaskForm):
class CreateSectionForm(BaseSectionForm):
collection_id = StringField("Collection ID", [DataRequired()])
sub_collection_id = StringField("Sub collection ID")
submit = SubmitField("Create")
def validate_collection_id(self, field):
collection_id = field.data
def validate_label(self, field):
request_args = (
{**request.view_args, **request.args}
if request.view_args
else {**request.args}
)
collection_id = request_args["collection_id"]
collection: m.Collection = db.session.get(m.Collection, collection_id)
if self.sub_collection_id.data and self.sub_collection_id.data != "_":
collection: m.Collection = db.session.get(
m.Collection, self.sub_collection_id.data
)
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

File diff suppressed because one or more lines are too long

View File

@ -3,10 +3,8 @@
<div id="add-section-modal" tabindex="-1" aria-hidden="true" class="fixed top-0 left-0 right-0 z-[150] 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 id="add_section_modal_form" action="{{ url_for('book.section_create', book_id=book.id, collection_id=0, sub_collection_id=0) }}" method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
<form id="add_section_modal_form" action="{{ url_for('book.section_create', book_id=book.id, collection_id=0) }}" method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
{{ form_hidden_tag() }}
<input type="hidden" name="collection_id" id="add_section_modal_collection_id" value="" />
<input type="hidden" name="sub_collection_id" id="add_section_modal_sub_collection_id" value="" />
<input type="hidden" name="about" id="new-section-input" />
<!-- Modal header -->
<div class="flex items-start justify-between p-4 border-b rounded-t dark:border-gray-600">

View File

@ -4,11 +4,9 @@
<!-- Modal content -->
<form
id="delete_section_modal_form"
action="{{ url_for('book.section_delete', book_id=book.id, collection_id=0, sub_collection_id=0, section_id=0) }}"
action="{{ url_for('book.section_delete', book_id=book.id, section_id=0) }}"
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
{{ form_hidden_tag() }}
<input type="hidden" name="collection_id" id="delete_section_modal_collection_id" value="" />
<input type="hidden" name="sub_collection_id" id="delete_section_modal_sub_collection_id" value="" />
<input type="hidden" name="section_id" id="delete_section_modal_section_id" value="" />
<!-- Modal header -->

View File

@ -5,7 +5,7 @@
<!-- Modal content -->
<form
id="delete_sub_collection_modal_form"
action="{{ url_for('book.collection_delete', book_id=book.id, collection_id=0, sub_collection_id=0) }}"
action="{{ url_for('book.collection_delete', book_id=book.id, collection_id=0) }}"
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
{{ form_hidden_tag() }}
<input type="hidden" name="collection_id" id="delete_sub_collection_modal_collection_id" value="" />

View File

@ -4,12 +4,9 @@
<div class="relative w-full max-w-2xl max-h-full">
<!-- Modal content -->
<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">
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_hidden_tag() }}
<!-- Modal header -->
<div class="flex items-start justify-between p-4 border-b rounded-t dark:border-gray-600">

View File

@ -3,11 +3,7 @@
<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 %}
action="{{ url_for('book.section_edit', book_id=book.id, section_id=section.id) }}"
method="post"
class="relative bg-white rounded-lg shadow dark:bg-gray-700"
>

View File

@ -1,92 +0,0 @@
<!-- prettier-ignore -->
{% 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 %}
<!-- prettier-ignore -->
{% 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 %}
<!-- prettier-ignore -->
{% block right_sidebar %}
{% include 'book/right_sidebar.html' %}
{% endblock %}
{% block content %}
<div class="overflow-x-auto shadow-md sm:rounded-lg md:mr-64">
<div class="fixed z-30 w-full top-32 pt-6 bg-white border-b border-gray-200 dark:bg-gray-800 dark:border-gray-700">
<h1 class="text-l font-extrabold dark:text-white ml-4">Sections page</h1>
<div class="mb-1">
<!-- prettier-ignore -->
<ul class="flex flex-wrap -mb-px text-sm font-medium text-center" id="myTab" data-tabs-toggle="#myTabContent" role="tablist">
<li class="mr-2" role="presentation">
<!-- prettier-ignore -->
<button class="flex items-center space-x-2 p-4 border-b-2 rounded-t-lg" id="files-tab" data-tabs-target="#files" type="button" role="tab" aria-controls="files" aria-selected="false">
<!-- prettier-ignore -->
<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="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m5.231 13.481L15 17.25m-4.5-15H5.625c-.621 0-1.125.504-1.125 1.125v16.5c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9zm3.75 11.625a2.625 2.625 0 11-5.25 0 2.625 2.625 0 015.25 0z" /> </svg>
<span>Files</span>
</button>
</li>
<li class="mr-2" role="presentation">
<!-- prettier-ignore -->
<button class="flex items-center space-x-2 p-4 border-b-2 border-transparent rounded-t-lg hover:text-gray-600 hover:border-gray-300 dark:hover:text-gray-300" id="about-tab" data-tabs-target="#about" type="button" role="tab" aria-controls="about" aria-selected="false">
<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="M8.625 12a.375.375 0 11-.75 0 .375.375 0 01.75 0zm0 0H8.25m4.125 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zm0 0H12m4.125 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zm0 0h-.375M21 12c0 4.556-4.03 8.25-9 8.25a9.764 9.764 0 01-2.555-.337A5.972 5.972 0 015.41 20.97a5.969 5.969 0 01-.474-.065 4.48 4.48 0 00.978-2.025c.09-.457-.133-.901-.467-1.226C3.93 16.178 3 14.189 3 12c0-4.556 4.03-8.25 9-8.25s9 3.694 9 8.25z" /> </svg>
<span>About</span>
</button>
</li>
</ul>
</div>
</div>
<div id="myTabContent" class="mt-20">
<!-- prettier-ignore -->
<div class="hidden p-4 rounded-lg bg-gray-50 dark:bg-gray-800" id="files" role="tabpanel" aria-labelledby="files-tab">
<dl class="w-md md:w-full text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700">
<!-- prettier-ignore -->
{% for section in sections %}
<!-- prettier-ignore -->
{% if sub_collection %}
<a href="{{url_for('book.interpretation_view', book_id=book.id, collection_id=collection.id, sub_collection_id=sub_collection.id, section_id=section.id)}}">
{% else %}
<a href="{{url_for('book.interpretation_view', book_id=book.id, collection_id=collection.id, section_id=section.id)}}">
{% endif %}
<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">
<div class="flex flex-col pb-3 p-3 w-full">
<dt class="flex w-full mb-1 text-gray-500 md:text-lg dark:text-gray-400 flex-col">
<!-- prettier-ignore -->
<p class="truncate">{{ section.label }}</p>
<div class="flex ml-auto align-center justify-center space-x-3">
<span class="space-x-0.5 flex items-center">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 22 22" stroke-width="1" stroke="currentColor" class="w-4 h-4 inline-flex mr-1"> <path stroke-linecap="round" stroke-linejoin="round" d="M3.75 13.5l10.5-11.25L12 10.5h8.25L9.75 21.75 12 13.5H3.75z" /></svg>
<p>55</p>
</span>
<span class="space-x-0.5 flex items-center">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 22 22" stroke-width="1" stroke="currentColor" class="w-4 h-4 inline-flex mr-1"> <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9z" /></svg>
<p>55</p>
</span>
</div>
</dt>
</div>
</dl>
</a>
{% endfor %}
</dl>
</div>
<div class="hidden p-4 rounded-lg bg-gray-50 dark:bg-gray-800" id="about" role="tabpanel" aria-labelledby="about-tab">
<p class="text-sm text-gray-500 dark:text-gray-400">This is about</p>
</div>
</div>
<!-- prettier-ignore -->
{% endblock %}
<!-- prettier-ignore -->
{% block scripts %}
{% endblock %}
</div>

View File

@ -34,28 +34,8 @@ def collection_view(book_id: int):
@bp.route("/<int:book_id>/<int:collection_id>/subcollections", 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 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_library"))
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))
breadcrumbs = create_breadcrumbs(book_id=book_id, collection_path=(collection.id,))
if collection.is_leaf:
return redirect(
url_for("book.section_view", book_id=book.id, collection_id=collection.id)
)
else:
return render_template(
"book/sub_collection_view.html",
book=book,
collection=collection,
breadcrumbs=breadcrumbs,
)
# TODO REMOVE ME
return {"REMOVE ME": "REMOVE ME"}
@bp.route("/<int:book_id>/create_collection", methods=["POST"])
@ -73,16 +53,12 @@ def collection_create(book_id: int, collection_id: int | None = None):
flash("You can't create subcollection for this collection", "danger")
return redirect(
url_for(
"book.sub_collection_view",
"book.collection_view",
book_id=book_id,
collection_id=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():
@ -114,7 +90,7 @@ def collection_create(book_id: int, collection_id: int | None = None):
label=label,
about=form.about.data,
parent_id=book.versions[-1].root_collection.id,
version_id=book.versions[-1].id,
version_id=book.last_version.id,
)
if collection_id:
collection.parent_id = collection_id
@ -137,18 +113,11 @@ def collection_create(book_id: int, collection_id: int | None = None):
@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"]
)
@register_book_verify_route(bp.name)
@login_required
def collection_edit(
book_id: int, collection_id: int, sub_collection_id: int | None = None
):
def collection_edit(book_id: int, collection_id: int):
book: m.Book = db.session.get(m.Book, book_id)
collection: m.Collection = db.session.get(m.Collection, collection_id)
if sub_collection_id:
collection = db.session.get(m.Collection, sub_collection_id)
form = f.EditCollectionForm()
redirect_url = url_for(
@ -158,19 +127,15 @@ def collection_edit(
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.id)
if sub_collection_id:
collection_query = collection_query.filter_by(parent_id=collection_id)
else:
collection_query = collection_query.filter_by(
parent_id=collection.parent.id
existing_collection: m.Collection = (
m.Collection.query.filter_by(
is_deleted=False, label=label, parent_id=collection.parent.id
)
.filter(m.Collection.id != collection.id)
.first()
)
if collection_query.first():
if existing_collection:
log(
log.INFO,
"Collection with similar label already exists. Book: [%s], Collection: [%s], Label: [%s]",
@ -203,18 +168,10 @@ def collection_edit(
@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"],
)
@register_book_verify_route(bp.name)
@login_required
def collection_delete(
book_id: int, collection_id: int, sub_collection_id: int | None = None
):
def collection_delete(book_id: int, collection_id: int):
collection: m.Collection = db.session.get(m.Collection, collection_id)
if sub_collection_id:
collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
collection.is_deleted = True
if collection.children:

View File

@ -62,10 +62,8 @@ def interpretation_view(
flash("Section not found", "danger")
return redirect(
url_for(
"book.section_view",
"book.collection_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
)
)
else:

View File

@ -1,5 +1,4 @@
from flask import (
render_template,
flash,
redirect,
url_for,
@ -18,78 +17,12 @@ from app.logger import log
from .bp import bp
@bp.route("/<int:book_id>/<int:collection_id>/sections", methods=["GET"])
@bp.route(
"/<int:book_id>/<int:collection_id>/<int:sub_collection_id>/sections",
methods=["GET"],
)
def section_view(
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_library"))
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("Sub_collection not found", "danger")
return redirect(
url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
)
)
if sub_collection:
sections = sub_collection.active_sections
else:
sections = collection.active_sections
breadcrumbs = create_breadcrumbs(
book_id=book_id,
collection_path=(
collection_id,
sub_collection_id,
),
)
return render_template(
"book/section_view.html",
book=book,
collection=collection,
sections=sections,
sub_collection=sub_collection,
breadcrumbs=breadcrumbs,
)
@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"],
)
@register_book_verify_route(bp.name)
@login_required
def section_create(
book_id: int, collection_id: int, sub_collection_id: int | None = None
):
def section_create(book_id: int, collection_id: int):
book: m.Book = db.session.get(m.Book, book_id)
collection: m.Collection = db.session.get(m.Collection, collection_id)
sub_collection = None
if sub_collection_id:
sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id)
redirect_url = url_for("book.collection_view", book_id=book_id)
if collection_id:
@ -103,13 +36,10 @@ def section_create(
if form.validate_on_submit():
section: m.Section = m.Section(
label=form.label.data,
collection_id=sub_collection_id or collection_id,
collection_id=collection_id,
version_id=book.last_version.id,
)
if sub_collection:
sub_collection.is_leaf = True
else:
collection.is_leaf = True
collection.is_leaf = True
log(log.INFO, "Create section [%s]. Collection: [%s]", section, collection_id)
section.save()
@ -124,31 +54,14 @@ def section_create(
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"],
)
@bp.route("/<int:book_id>/<int:section_id>/edit_section", methods=["POST"])
@register_book_verify_route(bp.name)
@login_required
def section_edit(
book_id: int,
collection_id: int,
section_id: int,
sub_collection_id: int | None = None,
):
redirect_url = url_for(
"book.interpretation_view",
book_id=book_id,
collection_id=collection_id,
sub_collection_id=sub_collection_id,
section_id=section_id,
)
def section_edit(book_id: int, section_id: int):
section: m.Section = db.session.get(m.Section, section_id)
form = f.EditSectionForm()
redirect_url = url_for("book.collection_view", book_id=book_id)
if form.validate_on_submit():
label = form.label.data
@ -169,44 +82,27 @@ def section_edit(
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"],
)
@bp.route("/<int:book_id>/<int:section_id>/delete_section", methods=["POST"])
@register_book_verify_route(bp.name)
@login_required
def section_delete(
book_id: int,
collection_id: int,
section_id: int,
sub_collection_id: int | None = None,
):
collection: m.Collection = db.session.get(
m.Collection, sub_collection_id or collection_id
)
section: m.Section = db.session.get(m.Section, section_id)
section.is_deleted = True
delete_nested_section_entities(section)
if not collection.active_sections:
if not section.collection.active_sections:
log(
log.INFO,
"Section [%s] has no active section. Set is_leaf = False",
section.id,
)
collection.is_leaf = False
section.collection.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,
)
)
return redirect(url_for("book.collection_view", book_id=book_id))

View File

@ -5,21 +5,10 @@ export function addSection() {
document.querySelector('#add-section-modal');
const addSectionModalBtns = document.querySelectorAll('#callAddSectionModal');
const collectionIdInAddSectionModal: HTMLInputElement =
document.querySelector('#add_section_modal_collection_id');
const subCollectionIdInAddSectionModal: HTMLInputElement =
document.querySelector('#add_section_modal_sub_collection_id');
const addSectionForm: HTMLFormElement = document.querySelector(
'#add_section_modal_form',
);
if (
addSectionModal &&
addSectionModalBtns &&
collectionIdInAddSectionModal &&
subCollectionIdInAddSectionModal &&
addSectionForm
) {
if (addSectionModal && addSectionModalBtns && addSectionForm) {
const defaultActionPath = addSectionForm.getAttribute('action');
const addModalCloseBtn = document.querySelector('#modalSectionCloseButton');
@ -32,18 +21,16 @@ export function addSection() {
btn.addEventListener('click', () => {
const collectionId = btn.getAttribute('data-collection-id');
const subCollectionId = btn.getAttribute('data-sub-collection-id');
collectionIdInAddSectionModal.value = collectionId;
subCollectionIdInAddSectionModal.value = subCollectionId;
let newActionPath: string = '';
if (subCollectionId === '_') {
newActionPath = defaultActionPath.replace(
'0/0/create_section',
'0/create_section',
`${collectionId}/create_section`,
);
} else {
newActionPath = defaultActionPath.replace(
'0/0/create_section',
`${collectionId}/${subCollectionId}/create_section`,
'0/create_section',
`${subCollectionId}/create_section`,
);
}
if (newActionPath.includes('/0')) {

View File

@ -8,10 +8,6 @@ export function deleteSection() {
const deleteSectionModalBtns = document.querySelectorAll(
'#callDeleteSectionModal',
);
const collectionIdInDeleteSectionModal: HTMLInputElement =
document.querySelector('#delete_section_modal_collection_id');
const subCollectionIdInDeleteSectionModal: HTMLInputElement =
document.querySelector('#delete_section_modal_sub_collection_id');
const sectionIdInDeleteSectionModal: HTMLInputElement =
document.querySelector('#delete_section_modal_section_id');
@ -22,8 +18,6 @@ export function deleteSection() {
if (
deleteSectionModal &&
deleteSectionModalBtns &&
collectionIdInDeleteSectionModal &&
subCollectionIdInDeleteSectionModal &&
sectionIdInDeleteSectionModal &&
deleteSectionForm
) {
@ -39,24 +33,13 @@ export function deleteSection() {
}
deleteSectionModalBtns.forEach(btn =>
btn.addEventListener('click', () => {
const collectionId = btn.getAttribute('data-collection-id');
const subCollectionId = btn.getAttribute('data-sub-collection-id');
const sectionId = btn.getAttribute('data-section-id');
collectionIdInDeleteSectionModal.value = collectionId;
subCollectionIdInDeleteSectionModal.value = subCollectionId;
sectionIdInDeleteSectionModal.value = sectionId;
let newActionPath: string = '';
if (subCollectionId === '_') {
newActionPath = defaultActionPath.replace(
'0/0/0/delete_section',
`${collectionId}/${sectionId}/delete_section`,
);
} else {
newActionPath = defaultActionPath.replace(
'0/0/0/delete_section',
`${collectionId}/${subCollectionId}/${sectionId}/delete_section`,
);
}
newActionPath = defaultActionPath.replace(
'0/delete_section',
`${sectionId}/delete_section`,
);
deleteSectionForm.setAttribute('action', `${newActionPath}`);
sectionDeleteModal.show();

View File

@ -36,14 +36,13 @@ export function deleteSubCollection() {
}
deleteSubCollectionModalBtns.forEach(btn =>
btn.addEventListener('click', () => {
const collectionId = btn.getAttribute('data-collection-id');
const subCollectionId = btn.getAttribute('data-sub-collection-id');
collectionIdInDeleteSubCollectionModal.value = collectionId;
collectionIdInDeleteSubCollectionModal.value = subCollectionId;
let newActionPath: string = '';
newActionPath = defaultActionPath.replace(
'0/0/delete',
`${collectionId}/${subCollectionId}/delete`,
'0/delete',
`${subCollectionId}/delete`,
);
deleteSubCollectionForm.setAttribute('action', `${newActionPath}`);

View File

@ -19,22 +19,13 @@ export function renameSection() {
sectionRenameForms[index].addEventListener('submit', async e => {
e.preventDefault();
const bookId = sectionRenameForms[index].getAttribute('data-book-id');
const collectionId =
sectionRenameForms[index].getAttribute('data-collection-id');
const subCollectionId = sectionRenameForms[index].getAttribute(
'data-sub-collection-id',
);
const sectionId =
sectionRenameForms[index].getAttribute('data-section-id');
const newLabel = inputsForRename[index].value;
inputsForRename[index].readOnly = true;
let url = '';
if (subCollectionId === '_') {
url = `/book/${bookId}/${collectionId}/${sectionId}/edit_section_label`;
} else {
url = `/book/${bookId}/${collectionId}/${subCollectionId}/${sectionId}/edit_section_label`;
}
url = `/book/${bookId}/${sectionId}/edit_section`;
const response = await fetch(url, {
method: 'POST',

View File

@ -23,15 +23,13 @@ export function renameSubCollection() {
e.preventDefault();
const bookId =
subCollectionRenameForms[index].getAttribute('data-book-id');
const collectionId =
subCollectionRenameForms[index].getAttribute('data-collection-id');
const subCollectionId = subCollectionRenameForms[index].getAttribute(
'data-sub-collection-id',
);
const newLabel = inputsForRename[index].value;
inputsForRename[index].readOnly = true;
let url = `/book/${bookId}/${collectionId}/${subCollectionId}/edit`;
let url = `/book/${bookId}/${subCollectionId}/edit`;
const response = await fetch(url, {
method: 'POST',

View File

@ -468,7 +468,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
).save()
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit",
f"/book/{book.id}/{sub_collection.id}/edit",
data=dict(
label="Test SubCollection #2 Label",
),
@ -482,7 +482,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
new_about = "Test SubCollection #1 About(edited)"
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/edit",
f"/book/{book.id}/{sub_collection.id}/edit",
data=dict(
label=new_label,
about=new_about,
@ -499,7 +499,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
assert edited_collection
response: Response = client.post(
f"/book/{book.id}/{collection.id}/9999/edit",
f"/book/{book.id}/9999/edit",
data=dict(
label=new_label,
about=new_about,
@ -508,10 +508,10 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
)
assert response.status_code == 200
assert b"Subcollection not found" in response.data
assert b"Collection not found" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
f"/book/{book.id}/{sub_collection.id}/delete",
follow_redirects=True,
)
@ -523,12 +523,12 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
check_if_nested_collection_entities_is_deleted(deleted_collection)
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/delete",
f"/book/{book.id}/{sub_collection.id}/delete",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Subcollection not found" in response.data
assert b"Collection not found" in response.data
def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
@ -606,7 +606,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
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",
f"/book/{book.id}/{sub_collection.id}/create_section",
data=dict(
collection_id=sub_collection.id,
label=label_1,
@ -625,7 +625,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
assert not section.interpretations
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/create_section",
f"/book/{book.id}/{sub_collection.id}/create_section",
data=dict(
collection_id=sub_collection.id,
label=label_1,
@ -651,13 +651,13 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
assert b"Collection not found" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/999/create_section",
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"Subcollection not found" in response.data
assert b"Collection not found" in response.data
# edit
@ -678,7 +678,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
).first()
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section.id}/edit_section",
f"/book/{book.id}/{section.id}/edit_section",
data=dict(
section_id=section.id,
label="Test",
@ -693,7 +693,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
new_about = "Test Section #1 About(edited)"
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/{section.id}/edit_section",
f"/book/{book.id}/{section.id}/edit_section",
data=dict(section_id=section.id, label=new_label, about=new_about),
follow_redirects=True,
)
@ -710,7 +710,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
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",
f"/book/{book.id}/{section_2.id}/edit_section",
data=dict(
section_id=section_2.id,
label="Test",
@ -722,7 +722,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
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",
f"/book/{book.id}/{section_2.id}/edit_section",
data=dict(section_id=section_2.id, label=new_label, about=new_about),
follow_redirects=True,
)
@ -735,7 +735,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
assert edited_section
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/999/edit_section",
f"/book/{book.id}/999/edit_section",
data=dict(section_id=section_2.id, label=new_label, about=new_about),
follow_redirects=True,
)
@ -744,7 +744,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
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",
f"/book/{book.id}/{section.id}/delete_section",
follow_redirects=True,
)
@ -756,7 +756,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
check_if_nested_section_entities_is_deleted(deleted_section)
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/{section_2.id}/delete_section",
f"/book/{book.id}/{section_2.id}/delete_section",
follow_redirects=True,
)
@ -768,7 +768,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
check_if_nested_section_entities_is_deleted(deleted_section)
response: Response = client.post(
f"/book/{book.id}/{collection.id}/{sub_collection.id}/999/delete_section",
f"/book/{book.id}/999/delete_section",
follow_redirects=True,
)
@ -845,13 +845,13 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
assert not interpretation.comments
response: Response = client.post(
f"/book/{book.id}/{collection.id}/999/create_section",
f"/book/{book.id}/999/create_section",
data=dict(collection_id=999, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Subcollection not found" in response.data
assert b"Collection not found" in response.data
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/999/create_interpretation",
@ -1179,13 +1179,13 @@ def test_interpretation_in_home_last_inter_section(
assert not interpretation.comments
response: Response = client.post(
f"/book/{book.id}/{collection.id}/999/create_section",
f"/book/{book.id}/999/create_section",
data=dict(collection_id=999, label=label_1, text=text_1),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Subcollection not found" in response.data
assert b"Collection not found" in response.data
response: Response = client.post(
f"/book/{book.id}/{leaf_collection.id}/999/create_interpretation",