From 9afcc981c24b5ff500f079c08fa5acdb15caf73e Mon Sep 17 00:00:00 2001 From: SvyatoslavArtymovych Date: Fri, 28 Apr 2023 14:41:29 +0300 Subject: [PATCH 1/2] add {{ flask_form.hidden_tag() }} --- app/__init__.py | 7 +++++++ app/templates/book/add_book_modal.html | 2 +- app/templates/book/add_collection_modal.html | 1 + app/templates/book/add_contributor_modal.html | 2 +- app/templates/book/delete_collection_modal.html | 1 + app/templates/book/edit_collection_modal.html | 1 + app/templates/book/settings.html | 2 ++ app/templates/user/add.html | 1 + 8 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index c02a6d8..463108c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,6 +5,7 @@ from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager from werkzeug.exceptions import HTTPException from flask_migrate import Migrate +from flask_wtf import FlaskForm from app.logger import log @@ -60,6 +61,12 @@ def create_app(environment="development"): login_manager.login_message_category = "info" login_manager.anonymous_user = AnonymousUser + # Jinja globals + + # Using: {{ flask_form.hidden_tag() }} + with app.app_context(): + app.jinja_env.globals["flask_form"] = FlaskForm() + # Error handlers. @app.errorhandler(HTTPException) def handle_http_error(exc): diff --git a/app/templates/book/add_book_modal.html b/app/templates/book/add_book_modal.html index 68e2b71..1dfb3aa 100644 --- a/app/templates/book/add_book_modal.html +++ b/app/templates/book/add_book_modal.html @@ -4,7 +4,7 @@
- + {{ flask_form.hidden_tag() }} diff --git a/app/templates/book/add_collection_modal.html b/app/templates/book/add_collection_modal.html index 20b5f61..9046427 100644 --- a/app/templates/book/add_collection_modal.html +++ b/app/templates/book/add_collection_modal.html @@ -10,6 +10,7 @@ action="{{ url_for('book.collection_create', book_id=book.id) }}" {% endif %} method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700"> + {{ flask_form.hidden_tag() }}

Add {% if collection %}Sub {% endif %}Collection

diff --git a/app/templates/book/add_contributor_modal.html b/app/templates/book/add_contributor_modal.html index 7a7b11c..e353e72 100644 --- a/app/templates/book/add_contributor_modal.html +++ b/app/templates/book/add_contributor_modal.html @@ -4,7 +4,7 @@
- + {{ flask_form.hidden_tag() }} diff --git a/app/templates/book/delete_collection_modal.html b/app/templates/book/delete_collection_modal.html index d246c2e..6324569 100644 --- a/app/templates/book/delete_collection_modal.html +++ b/app/templates/book/delete_collection_modal.html @@ -10,6 +10,7 @@ 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"> + {{ flask_form.hidden_tag() }}

Delete Collection

diff --git a/app/templates/book/edit_collection_modal.html b/app/templates/book/edit_collection_modal.html index c934b20..b713d85 100644 --- a/app/templates/book/edit_collection_modal.html +++ b/app/templates/book/edit_collection_modal.html @@ -10,6 +10,7 @@ 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"> + {{ flask_form.hidden_tag() }}

Edit Collection

diff --git a/app/templates/book/settings.html b/app/templates/book/settings.html index 7837b57..709e91c 100644 --- a/app/templates/book/settings.html +++ b/app/templates/book/settings.html @@ -35,6 +35,7 @@ {{ contributor.user.username }} + {{ flask_form.hidden_tag() }} diff --git a/app/templates/user/add.html b/app/templates/user/add.html index 6290010..991fc09 100644 --- a/app/templates/user/add.html +++ b/app/templates/user/add.html @@ -4,6 +4,7 @@
+ {{ flask_form.hidden_tag() }}

Add new user

From 754f93ae737539dadec311b8bc9326a0c9f303b6 Mon Sep 17 00:00:00 2001 From: SvyatoslavArtymovych Date: Fri, 28 Apr 2023 13:03:48 +0000 Subject: [PATCH 2/2] refactor jinja global flask_form --- app/__init__.py | 6 ++---- app/controllers/jinja_globals.py | 7 +++++++ app/templates/book/add_book_modal.html | 2 +- app/templates/book/add_collection_modal.html | 2 +- app/templates/book/add_contributor_modal.html | 2 +- app/templates/book/delete_collection_modal.html | 2 +- app/templates/book/edit_collection_modal.html | 2 +- app/templates/book/settings.html | 4 ++-- app/templates/user/add.html | 2 +- 9 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 app/controllers/jinja_globals.py diff --git a/app/__init__.py b/app/__init__.py index 463108c..7a5e1b3 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,7 +5,6 @@ from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager from werkzeug.exceptions import HTTPException from flask_migrate import Migrate -from flask_wtf import FlaskForm from app.logger import log @@ -62,10 +61,9 @@ def create_app(environment="development"): login_manager.anonymous_user = AnonymousUser # Jinja globals + from app.controllers.jinja_globals import form_hidden_tag - # Using: {{ flask_form.hidden_tag() }} - with app.app_context(): - app.jinja_env.globals["flask_form"] = FlaskForm() + app.jinja_env.globals["form_hidden_tag"] = form_hidden_tag # Error handlers. @app.errorhandler(HTTPException) diff --git a/app/controllers/jinja_globals.py b/app/controllers/jinja_globals.py new file mode 100644 index 0000000..80ca073 --- /dev/null +++ b/app/controllers/jinja_globals.py @@ -0,0 +1,7 @@ +from flask_wtf import FlaskForm + + +# Using: {{ form_hidden_tag() }} +def form_hidden_tag(): + form = FlaskForm() + return form.hidden_tag() diff --git a/app/templates/book/add_book_modal.html b/app/templates/book/add_book_modal.html index 1dfb3aa..9bf9e67 100644 --- a/app/templates/book/add_book_modal.html +++ b/app/templates/book/add_book_modal.html @@ -4,7 +4,7 @@
- {{ flask_form.hidden_tag() }} + {{ form_hidden_tag() }} diff --git a/app/templates/book/add_collection_modal.html b/app/templates/book/add_collection_modal.html index 9046427..fb450c9 100644 --- a/app/templates/book/add_collection_modal.html +++ b/app/templates/book/add_collection_modal.html @@ -10,7 +10,7 @@ action="{{ url_for('book.collection_create', book_id=book.id) }}" {% endif %} method="post" class="relative bg-white rounded-lg shadow dark:bg-gray-700"> - {{ flask_form.hidden_tag() }} + {{ form_hidden_tag() }}

Add {% if collection %}Sub {% endif %}Collection

diff --git a/app/templates/book/add_contributor_modal.html b/app/templates/book/add_contributor_modal.html index e353e72..11b555b 100644 --- a/app/templates/book/add_contributor_modal.html +++ b/app/templates/book/add_contributor_modal.html @@ -4,7 +4,7 @@
- {{ flask_form.hidden_tag() }} + {{ form_hidden_tag() }} diff --git a/app/templates/book/delete_collection_modal.html b/app/templates/book/delete_collection_modal.html index 6324569..ca654f3 100644 --- a/app/templates/book/delete_collection_modal.html +++ b/app/templates/book/delete_collection_modal.html @@ -10,7 +10,7 @@ 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"> - {{ flask_form.hidden_tag() }} + {{ form_hidden_tag() }}

Delete Collection

diff --git a/app/templates/book/edit_collection_modal.html b/app/templates/book/edit_collection_modal.html index b713d85..d2cda5d 100644 --- a/app/templates/book/edit_collection_modal.html +++ b/app/templates/book/edit_collection_modal.html @@ -10,7 +10,7 @@ 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"> - {{ flask_form.hidden_tag() }} + {{ form_hidden_tag() }}

Edit Collection

diff --git a/app/templates/book/settings.html b/app/templates/book/settings.html index 709e91c..735fbeb 100644 --- a/app/templates/book/settings.html +++ b/app/templates/book/settings.html @@ -35,7 +35,7 @@ {{ contributor.user.username }} - {{ flask_form.hidden_tag() }} + {{ form_hidden_tag() }} diff --git a/app/templates/user/add.html b/app/templates/user/add.html index 991fc09..3a4337c 100644 --- a/app/templates/user/add.html +++ b/app/templates/user/add.html @@ -4,7 +4,7 @@
- {{ flask_form.hidden_tag() }} + {{ form_hidden_tag() }}

Add new user