From 141daff128e3043e4295f4a52aa88f9849640564 Mon Sep 17 00:00:00 2001 From: Kostiantyn Stoliarskyi Date: Fri, 28 Apr 2023 12:12:46 +0300 Subject: [PATCH 1/3] start --- app/__init__.py | 2 + app/models/section.py | 15 ++++ app/templates/home/index.html | 2 +- app/templates/section/all.html | 127 +++++++++++++++++++++++++++++++++ app/views/__init__.py | 1 + app/views/book.py | 23 +++++- app/views/section.py | 29 ++++++++ 7 files changed, 195 insertions(+), 4 deletions(-) create mode 100644 app/templates/section/all.html create mode 100644 app/views/section.py diff --git a/app/__init__.py b/app/__init__.py index c02a6d8..117c566 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -23,6 +23,7 @@ def create_app(environment="development"): user_blueprint, book_blueprint, home_blueprint, + section_blueprint, ) from app.models import ( User, @@ -50,6 +51,7 @@ def create_app(environment="development"): app.register_blueprint(user_blueprint) app.register_blueprint(book_blueprint) app.register_blueprint(home_blueprint) + app.register_blueprint(section_blueprint) # Set up flask login. @login_manager.user_loader diff --git a/app/models/section.py b/app/models/section.py index 29a500d..627d6fc 100644 --- a/app/models/section.py +++ b/app/models/section.py @@ -32,5 +32,20 @@ class Section(BaseModel): path += self.label return path + @property + def book_id(self): + _book_id = self.version.book_id + return _book_id + + @property + def sub_collection_id(self): + parent = self.collection + grand_parent = parent.parent + if grand_parent.is_root: + _sub_collection_id = parent.id + else: + _sub_collection_id = grand_parent.id + return _sub_collection_id + def __repr__(self): return f"<{self.id}: {self.label}>" diff --git a/app/templates/home/index.html b/app/templates/home/index.html index 34561b5..4142747 100644 --- a/app/templates/home/index.html +++ b/app/templates/home/index.html @@ -69,7 +69,7 @@

{{section.path}}

{% endfor %} - Explore all sections... + Explore all sections... {% endblock %} diff --git a/app/templates/section/all.html b/app/templates/section/all.html new file mode 100644 index 0000000..5704a75 --- /dev/null +++ b/app/templates/section/all.html @@ -0,0 +1,127 @@ + +{% extends 'base.html' %} +{% block content %} + +
+ +
+ +

Sections

+
+ +
+ {% for section in sections %} + + + + +
+
+
+ +

{{ section.path }}

+
+ + + + +

55

+
+ + + + + +

55

+
+
+
+
+
+
+ + {% endfor %} +
+ + {% if page.pages > 1 %} +
+ +
+ {% endif %} +
+ + +{% endblock %} + +{% block scripts %} +{% endblock %} diff --git a/app/views/__init__.py b/app/views/__init__.py index 7751f4f..9c6c52e 100644 --- a/app/views/__init__.py +++ b/app/views/__init__.py @@ -4,3 +4,4 @@ from .main import main_blueprint from .user import bp as user_blueprint from .book import bp as book_blueprint from .home import bp as home_blueprint +from .section import bp as section_blueprint diff --git a/app/views/book.py b/app/views/book.py index 5edb824..731f8ca 100644 --- a/app/views/book.py +++ b/app/views/book.py @@ -146,11 +146,18 @@ def section_view(book_id: int, collection_id: int, sub_collection_id: int): @bp.route( - "////", + "/////interpretation", + methods=["GET"], +) +@bp.route( + "////interpretation", methods=["GET"], ) def interpretation_view( - book_id: int, collection_id: int, sub_collection_id: int, section_id: int + book_id: int, + collection_id: int, + section_id: int, + sub_collection_id: int | None = None, ): book: m.Book = db.session.get(m.Book, book_id) if not book or book.is_deleted: @@ -164,6 +171,17 @@ def interpretation_view( flash("Collection not found", "danger") return redirect(url_for("book.collection_view", book_id=book_id)) + if sub_collection_id: + sub_collection: m.Collection = db.session.get(m.Collection, sub_collection_id) + if not sub_collection: + 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, + ) 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) @@ -172,7 +190,6 @@ def interpretation_view( url_for( "book.sub_collection_view", book_id=book_id, collection_id=collection_id ) - ) section: m.Section = db.session.get(m.Section, section_id) if not section: diff --git a/app/views/section.py b/app/views/section.py new file mode 100644 index 0000000..b4feaa1 --- /dev/null +++ b/app/views/section.py @@ -0,0 +1,29 @@ +from flask import ( + Blueprint, + render_template, + request, +) + +from app.controllers import create_pagination +from app import models as m + + +bp = Blueprint("section", __name__, url_prefix="/section") + + +@bp.route("/all", methods=["GET"]) +def get_all(): + q = request.args.get("q", type=str, default=None) + section: m.Section = m.Section.query.order_by(m.Section.id) + if q: + section = section.filter(m.Section.label.like(f"{q}")) + + pagination = create_pagination(total=section.count()) + + return render_template( + "section/all.html", + sections=section.paginate(page=pagination.page, per_page=pagination.per_page), + page=pagination, + search_query=q, + all_books=True, + ) From be0f63da861eab304673cf2465e80bccc031f4f9 Mon Sep 17 00:00:00 2001 From: Kostiantyn Stoliarskyi Date: Fri, 28 Apr 2023 16:14:36 +0300 Subject: [PATCH 2/3] fix book.py --- app/views/book.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/app/views/book.py b/app/views/book.py index 7a80cc2..dec07a4 100644 --- a/app/views/book.py +++ b/app/views/book.py @@ -176,20 +176,12 @@ def interpretation_view( if not sub_collection: 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, - ) + 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 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 - ) + return redirect(url_for("book.collection_view", book_id=book_id)) section: m.Section = db.session.get(m.Section, section_id) if not section: From 37f5291a2809513b25fa39ddf0901635b298d0bc Mon Sep 17 00:00:00 2001 From: SvyatoslavArtymovych Date: Mon, 1 May 2023 17:39:12 +0300 Subject: [PATCH 3/3] remove commented code --- app/templates/section/all.html | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/templates/section/all.html b/app/templates/section/all.html index 5704a75..d057694 100644 --- a/app/templates/section/all.html +++ b/app/templates/section/all.html @@ -13,8 +13,6 @@ class="w-md md:w-full text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700"> {% for section in sections %} - -