mirror of https://github.com/logos-co/open-law.git
Merge pull request #86 from Simple2B/svyat/routes_section
refactor section routes
This commit is contained in:
commit
59ea4046c9
|
@ -70,17 +70,10 @@ def book_validator() -> Response | None:
|
||||||
section_id = request_args.get("section_id")
|
section_id = request_args.get("section_id")
|
||||||
if section_id:
|
if section_id:
|
||||||
section: m.Section = db.session.get(m.Section, 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)
|
log(log.WARNING, "Section with id [%s] not found", section)
|
||||||
flash("Section not found", "danger")
|
flash("Section not found", "danger")
|
||||||
return redirect(
|
return redirect(url_for("book.collection_view", book_id=book_id))
|
||||||
url_for(
|
|
||||||
"book.section_view",
|
|
||||||
book_id=book_id,
|
|
||||||
collection_id=collection_id,
|
|
||||||
sub_collection_id=sub_collection_id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
interpretation_id = request_args.get("interpretation_id")
|
interpretation_id = request_args.get("interpretation_id")
|
||||||
if interpretation_id:
|
if interpretation_id:
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from wtforms import StringField, SubmitField, ValidationError
|
from wtforms import StringField, SubmitField, ValidationError
|
||||||
from wtforms.validators import DataRequired, Length
|
from wtforms.validators import DataRequired, Length
|
||||||
|
from flask import request
|
||||||
|
|
||||||
from app import models as m, db
|
from app import models as m, db
|
||||||
from app.logger import log
|
from app.logger import log
|
||||||
|
@ -12,26 +13,23 @@ class BaseSectionForm(FlaskForm):
|
||||||
|
|
||||||
|
|
||||||
class CreateSectionForm(BaseSectionForm):
|
class CreateSectionForm(BaseSectionForm):
|
||||||
collection_id = StringField("Collection ID", [DataRequired()])
|
|
||||||
sub_collection_id = StringField("Sub collection ID")
|
|
||||||
submit = SubmitField("Create")
|
submit = SubmitField("Create")
|
||||||
|
|
||||||
def validate_collection_id(self, field):
|
def validate_label(self, field):
|
||||||
collection_id = field.data
|
request_args = (
|
||||||
collection: m.Collection = db.session.get(m.Collection, collection_id)
|
{**request.view_args, **request.args}
|
||||||
if self.sub_collection_id.data and self.sub_collection_id.data != "_":
|
if request.view_args
|
||||||
collection: m.Collection = db.session.get(
|
else {**request.args}
|
||||||
m.Collection, self.sub_collection_id.data
|
|
||||||
)
|
)
|
||||||
|
collection_id = request_args["collection_id"]
|
||||||
|
collection: m.Collection = db.session.get(m.Collection, collection_id)
|
||||||
|
|
||||||
if not collection or collection.sub_collections:
|
if not collection or collection.sub_collections:
|
||||||
log(log.WARNING, "Collection [%s] it not leaf", collection)
|
log(log.WARNING, "Collection [%s] it not leaf", collection)
|
||||||
|
|
||||||
raise ValidationError("You can't create section for this collection")
|
raise ValidationError("You can't create section for this collection")
|
||||||
|
|
||||||
def validate_label(self, field):
|
|
||||||
label = field.data
|
label = field.data
|
||||||
collection_id = self.collection_id.data
|
|
||||||
|
|
||||||
section: m.Section = m.Section.query.filter_by(
|
section: m.Section = m.Section.query.filter_by(
|
||||||
is_deleted=False, label=label, collection_id=collection_id
|
is_deleted=False, label=label, collection_id=collection_id
|
||||||
|
|
File diff suppressed because one or more lines are too long
144729
app/static/js/main.js
144729
app/static/js/main.js
File diff suppressed because one or more lines are too long
|
@ -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 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">
|
<div class="relative w-full max-w-2xl max-h-full">
|
||||||
<!-- Modal content -->
|
<!-- 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() }}
|
{{ 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" />
|
<input type="hidden" name="about" id="new-section-input" />
|
||||||
<!-- Modal header -->
|
<!-- Modal header -->
|
||||||
<div class="flex items-start justify-between p-4 border-b rounded-t dark:border-gray-600">
|
<div class="flex items-start justify-between p-4 border-b rounded-t dark:border-gray-600">
|
||||||
|
|
|
@ -4,11 +4,9 @@
|
||||||
<!-- Modal content -->
|
<!-- Modal content -->
|
||||||
<form
|
<form
|
||||||
id="delete_section_modal_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">
|
method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700">
|
||||||
{{ form_hidden_tag() }}
|
{{ 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="" />
|
<input type="hidden" name="section_id" id="delete_section_modal_section_id" value="" />
|
||||||
|
|
||||||
<!-- Modal header -->
|
<!-- Modal header -->
|
||||||
|
|
|
@ -3,11 +3,7 @@
|
||||||
<div class="relative w-full max-w-2xl max-h-full">
|
<div class="relative w-full max-w-2xl max-h-full">
|
||||||
<!-- Modal content -->
|
<!-- Modal content -->
|
||||||
<form
|
<form
|
||||||
{% if sub_collection %}
|
action="{{ url_for('book.section_edit', book_id=book.id, section_id=section.id) }}"
|
||||||
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"
|
method="post"
|
||||||
class="relative bg-white rounded-lg shadow dark:bg-gray-700"
|
class="relative bg-white rounded-lg shadow dark:bg-gray-700"
|
||||||
>
|
>
|
||||||
|
|
|
@ -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>
|
|
|
@ -62,10 +62,8 @@ def interpretation_view(
|
||||||
flash("Section not found", "danger")
|
flash("Section not found", "danger")
|
||||||
return redirect(
|
return redirect(
|
||||||
url_for(
|
url_for(
|
||||||
"book.section_view",
|
"book.collection_view",
|
||||||
book_id=book_id,
|
book_id=book_id,
|
||||||
collection_id=collection_id,
|
|
||||||
sub_collection_id=sub_collection_id,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
from flask import (
|
from flask import (
|
||||||
render_template,
|
|
||||||
flash,
|
flash,
|
||||||
redirect,
|
redirect,
|
||||||
url_for,
|
url_for,
|
||||||
|
@ -18,78 +17,12 @@ from app.logger import log
|
||||||
from .bp import bp
|
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>/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)
|
@register_book_verify_route(bp.name)
|
||||||
@login_required
|
@login_required
|
||||||
def section_create(
|
def section_create(book_id: int, collection_id: int):
|
||||||
book_id: int, collection_id: int, sub_collection_id: int | None = None
|
|
||||||
):
|
|
||||||
book: m.Book = db.session.get(m.Book, book_id)
|
book: m.Book = db.session.get(m.Book, book_id)
|
||||||
collection: m.Collection = db.session.get(m.Collection, collection_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)
|
redirect_url = url_for("book.collection_view", book_id=book_id)
|
||||||
if collection_id:
|
if collection_id:
|
||||||
|
@ -103,12 +36,9 @@ def section_create(
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
section: m.Section = m.Section(
|
section: m.Section = m.Section(
|
||||||
label=form.label.data,
|
label=form.label.data,
|
||||||
collection_id=sub_collection_id or collection_id,
|
collection_id=collection_id,
|
||||||
version_id=book.last_version.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)
|
log(log.INFO, "Create section [%s]. Collection: [%s]", section, collection_id)
|
||||||
section.save()
|
section.save()
|
||||||
|
@ -124,31 +54,14 @@ def section_create(
|
||||||
return redirect(redirect_url)
|
return redirect(redirect_url)
|
||||||
|
|
||||||
|
|
||||||
@bp.route(
|
@bp.route("/<int:book_id>/<int:section_id>/edit_section", methods=["POST"])
|
||||||
"/<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"],
|
|
||||||
)
|
|
||||||
@register_book_verify_route(bp.name)
|
@register_book_verify_route(bp.name)
|
||||||
@login_required
|
@login_required
|
||||||
def section_edit(
|
def section_edit(book_id: int, section_id: int):
|
||||||
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,
|
|
||||||
)
|
|
||||||
section: m.Section = db.session.get(m.Section, section_id)
|
section: m.Section = db.session.get(m.Section, section_id)
|
||||||
|
|
||||||
form = f.EditSectionForm()
|
form = f.EditSectionForm()
|
||||||
|
redirect_url = url_for("book.collection_view", book_id=book_id)
|
||||||
|
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
label = form.label.data
|
label = form.label.data
|
||||||
|
@ -169,44 +82,27 @@ def section_edit(
|
||||||
return redirect(redirect_url)
|
return redirect(redirect_url)
|
||||||
|
|
||||||
|
|
||||||
@bp.route(
|
@bp.route("/<int:book_id>/<int:section_id>/delete_section", methods=["POST"])
|
||||||
"/<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"],
|
|
||||||
)
|
|
||||||
@register_book_verify_route(bp.name)
|
@register_book_verify_route(bp.name)
|
||||||
@login_required
|
@login_required
|
||||||
def section_delete(
|
def section_delete(
|
||||||
book_id: int,
|
book_id: int,
|
||||||
collection_id: int,
|
|
||||||
section_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: m.Section = db.session.get(m.Section, section_id)
|
||||||
|
|
||||||
section.is_deleted = True
|
section.is_deleted = True
|
||||||
delete_nested_section_entities(section)
|
delete_nested_section_entities(section)
|
||||||
if not collection.active_sections:
|
if not section.collection.active_sections:
|
||||||
log(
|
log(
|
||||||
log.INFO,
|
log.INFO,
|
||||||
"Section [%s] has no active section. Set is_leaf = False",
|
"Section [%s] has no active section. Set is_leaf = False",
|
||||||
section.id,
|
section.id,
|
||||||
)
|
)
|
||||||
collection.is_leaf = False
|
section.collection.is_leaf = False
|
||||||
|
|
||||||
log(log.INFO, "Delete section [%s]", section.id)
|
log(log.INFO, "Delete section [%s]", section.id)
|
||||||
section.save()
|
section.save()
|
||||||
|
|
||||||
flash("Success!", "success")
|
flash("Success!", "success")
|
||||||
return redirect(
|
return redirect(url_for("book.collection_view", book_id=book_id))
|
||||||
url_for(
|
|
||||||
"book.collection_view",
|
|
||||||
book_id=book_id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
|
@ -5,21 +5,10 @@ export function addSection() {
|
||||||
document.querySelector('#add-section-modal');
|
document.querySelector('#add-section-modal');
|
||||||
|
|
||||||
const addSectionModalBtns = document.querySelectorAll('#callAddSectionModal');
|
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(
|
const addSectionForm: HTMLFormElement = document.querySelector(
|
||||||
'#add_section_modal_form',
|
'#add_section_modal_form',
|
||||||
);
|
);
|
||||||
if (
|
if (addSectionModal && addSectionModalBtns && addSectionForm) {
|
||||||
addSectionModal &&
|
|
||||||
addSectionModalBtns &&
|
|
||||||
collectionIdInAddSectionModal &&
|
|
||||||
subCollectionIdInAddSectionModal &&
|
|
||||||
addSectionForm
|
|
||||||
) {
|
|
||||||
const defaultActionPath = addSectionForm.getAttribute('action');
|
const defaultActionPath = addSectionForm.getAttribute('action');
|
||||||
|
|
||||||
const addModalCloseBtn = document.querySelector('#modalSectionCloseButton');
|
const addModalCloseBtn = document.querySelector('#modalSectionCloseButton');
|
||||||
|
@ -32,18 +21,16 @@ export function addSection() {
|
||||||
btn.addEventListener('click', () => {
|
btn.addEventListener('click', () => {
|
||||||
const collectionId = btn.getAttribute('data-collection-id');
|
const collectionId = btn.getAttribute('data-collection-id');
|
||||||
const subCollectionId = btn.getAttribute('data-sub-collection-id');
|
const subCollectionId = btn.getAttribute('data-sub-collection-id');
|
||||||
collectionIdInAddSectionModal.value = collectionId;
|
|
||||||
subCollectionIdInAddSectionModal.value = subCollectionId;
|
|
||||||
let newActionPath: string = '';
|
let newActionPath: string = '';
|
||||||
if (subCollectionId === '_') {
|
if (subCollectionId === '_') {
|
||||||
newActionPath = defaultActionPath.replace(
|
newActionPath = defaultActionPath.replace(
|
||||||
'0/0/create_section',
|
'0/create_section',
|
||||||
`${collectionId}/create_section`,
|
`${collectionId}/create_section`,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
newActionPath = defaultActionPath.replace(
|
newActionPath = defaultActionPath.replace(
|
||||||
'0/0/create_section',
|
'0/create_section',
|
||||||
`${collectionId}/${subCollectionId}/create_section`,
|
`${subCollectionId}/create_section`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (newActionPath.includes('/0')) {
|
if (newActionPath.includes('/0')) {
|
||||||
|
|
|
@ -8,10 +8,6 @@ export function deleteSection() {
|
||||||
const deleteSectionModalBtns = document.querySelectorAll(
|
const deleteSectionModalBtns = document.querySelectorAll(
|
||||||
'#callDeleteSectionModal',
|
'#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 =
|
const sectionIdInDeleteSectionModal: HTMLInputElement =
|
||||||
document.querySelector('#delete_section_modal_section_id');
|
document.querySelector('#delete_section_modal_section_id');
|
||||||
|
|
||||||
|
@ -22,8 +18,6 @@ export function deleteSection() {
|
||||||
if (
|
if (
|
||||||
deleteSectionModal &&
|
deleteSectionModal &&
|
||||||
deleteSectionModalBtns &&
|
deleteSectionModalBtns &&
|
||||||
collectionIdInDeleteSectionModal &&
|
|
||||||
subCollectionIdInDeleteSectionModal &&
|
|
||||||
sectionIdInDeleteSectionModal &&
|
sectionIdInDeleteSectionModal &&
|
||||||
deleteSectionForm
|
deleteSectionForm
|
||||||
) {
|
) {
|
||||||
|
@ -39,24 +33,13 @@ export function deleteSection() {
|
||||||
}
|
}
|
||||||
deleteSectionModalBtns.forEach(btn =>
|
deleteSectionModalBtns.forEach(btn =>
|
||||||
btn.addEventListener('click', () => {
|
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');
|
const sectionId = btn.getAttribute('data-section-id');
|
||||||
collectionIdInDeleteSectionModal.value = collectionId;
|
|
||||||
subCollectionIdInDeleteSectionModal.value = subCollectionId;
|
|
||||||
sectionIdInDeleteSectionModal.value = sectionId;
|
sectionIdInDeleteSectionModal.value = sectionId;
|
||||||
let newActionPath: string = '';
|
let newActionPath: string = '';
|
||||||
if (subCollectionId === '_') {
|
|
||||||
newActionPath = defaultActionPath.replace(
|
newActionPath = defaultActionPath.replace(
|
||||||
'0/0/0/delete_section',
|
'0/delete_section',
|
||||||
`${collectionId}/${sectionId}/delete_section`,
|
`${sectionId}/delete_section`,
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
newActionPath = defaultActionPath.replace(
|
|
||||||
'0/0/0/delete_section',
|
|
||||||
`${collectionId}/${subCollectionId}/${sectionId}/delete_section`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
deleteSectionForm.setAttribute('action', `${newActionPath}`);
|
deleteSectionForm.setAttribute('action', `${newActionPath}`);
|
||||||
sectionDeleteModal.show();
|
sectionDeleteModal.show();
|
||||||
|
|
|
@ -19,22 +19,13 @@ export function renameSection() {
|
||||||
sectionRenameForms[index].addEventListener('submit', async e => {
|
sectionRenameForms[index].addEventListener('submit', async e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const bookId = sectionRenameForms[index].getAttribute('data-book-id');
|
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 =
|
const sectionId =
|
||||||
sectionRenameForms[index].getAttribute('data-section-id');
|
sectionRenameForms[index].getAttribute('data-section-id');
|
||||||
const newLabel = inputsForRename[index].value;
|
const newLabel = inputsForRename[index].value;
|
||||||
inputsForRename[index].readOnly = true;
|
inputsForRename[index].readOnly = true;
|
||||||
|
|
||||||
let url = '';
|
let url = '';
|
||||||
if (subCollectionId === '_') {
|
url = `/book/${bookId}/${sectionId}/edit_section`;
|
||||||
url = `/book/${bookId}/${collectionId}/${sectionId}/edit_section_label`;
|
|
||||||
} else {
|
|
||||||
url = `/book/${bookId}/${collectionId}/${subCollectionId}/${sectionId}/edit_section_label`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|
|
@ -606,7 +606,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
assert b"Section label must be unique!" in response.data
|
assert b"Section label must be unique!" in response.data
|
||||||
|
|
||||||
response: Response = client.post(
|
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(
|
data=dict(
|
||||||
collection_id=sub_collection.id,
|
collection_id=sub_collection.id,
|
||||||
label=label_1,
|
label=label_1,
|
||||||
|
@ -625,7 +625,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
assert not section.interpretations
|
assert not section.interpretations
|
||||||
|
|
||||||
response: Response = client.post(
|
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(
|
data=dict(
|
||||||
collection_id=sub_collection.id,
|
collection_id=sub_collection.id,
|
||||||
label=label_1,
|
label=label_1,
|
||||||
|
@ -651,13 +651,13 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
assert b"Collection not found" in response.data
|
assert b"Collection not found" in response.data
|
||||||
|
|
||||||
response: Response = client.post(
|
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"),
|
data=dict(collection_id=999, label=label_1, about="Test Section #1 About"),
|
||||||
follow_redirects=True,
|
follow_redirects=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert b"Subcollection not found" in response.data
|
assert b"Collection not found" in response.data
|
||||||
|
|
||||||
# edit
|
# edit
|
||||||
|
|
||||||
|
@ -678,7 +678,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
).first()
|
).first()
|
||||||
|
|
||||||
response: Response = client.post(
|
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(
|
data=dict(
|
||||||
section_id=section.id,
|
section_id=section.id,
|
||||||
label="Test",
|
label="Test",
|
||||||
|
@ -693,7 +693,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
new_about = "Test Section #1 About(edited)"
|
new_about = "Test Section #1 About(edited)"
|
||||||
|
|
||||||
response: Response = client.post(
|
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),
|
data=dict(section_id=section.id, label=new_label, about=new_about),
|
||||||
follow_redirects=True,
|
follow_redirects=True,
|
||||||
)
|
)
|
||||||
|
@ -710,7 +710,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
label=label_1, collection_id=sub_collection.id
|
label=label_1, collection_id=sub_collection.id
|
||||||
).first()
|
).first()
|
||||||
response: Response = client.post(
|
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(
|
data=dict(
|
||||||
section_id=section_2.id,
|
section_id=section_2.id,
|
||||||
label="Test",
|
label="Test",
|
||||||
|
@ -722,7 +722,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
assert b"Section label must be unique!" in response.data
|
assert b"Section label must be unique!" in response.data
|
||||||
|
|
||||||
response: Response = client.post(
|
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),
|
data=dict(section_id=section_2.id, label=new_label, about=new_about),
|
||||||
follow_redirects=True,
|
follow_redirects=True,
|
||||||
)
|
)
|
||||||
|
@ -735,7 +735,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
assert edited_section
|
assert edited_section
|
||||||
|
|
||||||
response: Response = client.post(
|
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),
|
data=dict(section_id=section_2.id, label=new_label, about=new_about),
|
||||||
follow_redirects=True,
|
follow_redirects=True,
|
||||||
)
|
)
|
||||||
|
@ -744,7 +744,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
assert b"Section not found" in response.data
|
assert b"Section not found" in response.data
|
||||||
|
|
||||||
response: Response = client.post(
|
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,
|
follow_redirects=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -756,7 +756,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
check_if_nested_section_entities_is_deleted(deleted_section)
|
check_if_nested_section_entities_is_deleted(deleted_section)
|
||||||
|
|
||||||
response: Response = client.post(
|
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,
|
follow_redirects=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -768,7 +768,7 @@ def test_crud_sections(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
check_if_nested_section_entities_is_deleted(deleted_section)
|
check_if_nested_section_entities_is_deleted(deleted_section)
|
||||||
|
|
||||||
response: Response = client.post(
|
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,
|
follow_redirects=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -845,13 +845,13 @@ def test_crud_interpretation(client: FlaskClient, runner: FlaskCliRunner):
|
||||||
assert not interpretation.comments
|
assert not interpretation.comments
|
||||||
|
|
||||||
response: Response = client.post(
|
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),
|
data=dict(collection_id=999, text=text_1),
|
||||||
follow_redirects=True,
|
follow_redirects=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert response.status_code == 200
|
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(
|
response: Response = client.post(
|
||||||
f"/book/{book.id}/{leaf_collection.id}/999/create_interpretation",
|
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
|
assert not interpretation.comments
|
||||||
|
|
||||||
response: Response = client.post(
|
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),
|
data=dict(collection_id=999, label=label_1, text=text_1),
|
||||||
follow_redirects=True,
|
follow_redirects=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert response.status_code == 200
|
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(
|
response: Response = client.post(
|
||||||
f"/book/{book.id}/{leaf_collection.id}/999/create_interpretation",
|
f"/book/{book.id}/{leaf_collection.id}/999/create_interpretation",
|
||||||
|
|
Loading…
Reference in New Issue