From c5609f8c073a9ce9e72ff37c2ab541febc345942 Mon Sep 17 00:00:00 2001 From: Debashish Palit Date: Mon, 10 May 2021 15:05:45 +0530 Subject: [PATCH] Refactoring --- README.md | 14 +++++++------- flask_modals/modal.py | 13 ++++++++----- flask_modals/parser.py | 21 +++++++++------------ flask_modals/static/js/main.js | 4 +++- setup.py | 2 +- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index a2dd222..a14303a 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ pip install Flask-Modals app = Flask(__name__) modal = Modal(app) ``` - +
2. Alternatively if you are using the application factory pattern: @@ -42,14 +42,14 @@ pip install Flask-Modals app = Flask(__name__) modal.init_app(app) ``` - +
3. Include the following in the head tag of your base template. ```html {{ modals() }} ``` - +
4. Include the following in the modal body. @@ -59,10 +59,10 @@ pip install Flask-Modals
... ``` - +
5. Import the function `render_template_modal` in your `routes.py` file and use -it instead of `render_template` in the route handler for the pagewith the modal +it instead of `render_template` in the route handler for the page with the modal form. It takes the same arguments as `render_template`, apart from `modal` (the modal `id`), `turbo` (`False` if modal is not to be displayed) and `redirect` (`False` if you are not redirecting). See the next examples for use of `turbo` and @@ -90,7 +90,7 @@ modal `id`), `turbo` (`False` if modal is not to be displayed) and `redirect` return render_template_modal('index.html', form=form, modal='modal-form') ``` See the example folder in the repo for more details. - +
6. If you want to redirect to the same page outside the modal, use Flask's `session` proxy as follows: @@ -120,7 +120,7 @@ modal `id`), `turbo` (`False` if modal is not to be displayed) and `redirect` return render_template_modal('index.html', form=form, modal='modal-form', turbo=check) ``` - +
7. If you want to render a template and not redirect, then use the following pattern: diff --git a/flask_modals/modal.py b/flask_modals/modal.py index 0d9e59d..5fe442d 100644 --- a/flask_modals/modal.py +++ b/flask_modals/modal.py @@ -31,10 +31,11 @@ def render_template_modal(*args, **kwargs): ''' ctx = _app_ctx_stack.top + ctx._include = True # used in extension templates modal = kwargs.pop('modal', None) replace = kwargs.pop('turbo', True) + update = False redirect = kwargs.pop('redirect', True) - ctx._include = True # used in extension templates show_modal = False if turbo.can_stream(): @@ -43,20 +44,22 @@ def render_template_modal(*args, **kwargs): # prevent flash messages from showing both outside and # inside the modal ctx._modal = True + else: + update = False if redirect else True html, stream, target = add_turbo_stream_ids( render_template(*args, **kwargs), modal, redirect, + update, show_modal ) if show_modal: return turbo.stream(turbo.replace(stream, target=target)) - else: - if not replace and not redirect: - return turbo.stream(turbo.update(stream, - target='turbo-stream__body')) + + if update: + return turbo.stream(turbo.update(stream, target='turbo-stream__body')) return html diff --git a/flask_modals/parser.py b/flask_modals/parser.py index e29a001..f2b8a90 100644 --- a/flask_modals/parser.py +++ b/flask_modals/parser.py @@ -22,10 +22,7 @@ def __init__(self, html, modal, redirect, show_modal): html: The render_template output string. Will be modified with id attributes. modal: The id of the modal - redirect: If rendering a template instead of redirecting, this will - be False. - body: If True, parse the body out of the html string if redirect is - False. + body: If True, parse the body out of the html string. stream: The modal body element with the id set. target: The id of the stream element. ''' @@ -37,13 +34,12 @@ def __init__(self, html, modal, redirect, show_modal): self.found_body = False self.html = html self.modal = modal - self.redirect = redirect self.body = not show_modal self.stream = '' self.target = '' - self.insert_id() + self.insert_id(redirect) - def insert_id(self): + def insert_id(self, redirect): def repl(matchobj): @@ -52,13 +48,13 @@ def repl(matchobj): self.html = re.sub(r'class\s*=\s*"modal-body"', repl, self.html) - if not self.redirect: + if not redirect: self.html = self.html.replace('')) @@ -113,9 +109,10 @@ def get_stream(self, endtaglen=6): self.stream += end_line[:end_pos] -def add_turbo_stream_ids(html, modal, redirect, show_modal): +def add_turbo_stream_ids(html, modal, redirect, update, show_modal): '''Add turbo stream ids and parse the resulting html string.''' parser = ModalParser(html, modal, redirect, show_modal) - parser.feed(parser.html) + if update or show_modal: # parse only if streaming + parser.feed(parser.html) return parser.html, parser.stream, parser.target diff --git a/flask_modals/static/js/main.js b/flask_modals/static/js/main.js index c9cb4c6..61f0797 100644 --- a/flask_modals/static/js/main.js +++ b/flask_modals/static/js/main.js @@ -20,6 +20,8 @@ document.documentElement.addEventListener('turbo:submit-end', () => { document.documentElement.addEventListener('turbo:before-stream-render', (e) => { if (e.target.attributes.action.value === 'update') { - document.querySelector('body').classList.remove('modal-open') + const body = document.querySelector('body') + body.classList.remove('modal-open') + body.style.paddingRight = '0px' } }) diff --git a/setup.py b/setup.py index 47b283a..b1ed5e1 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name='Flask-Modals', - version='0.2.1', + version='0.2.3', author='Debashish Palit', author_email='dpalit17@outlook.com', description='Use forms in Bootstrap 4 modals with Flask.',