Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
deb17 committed May 10, 2021
1 parent a6baf2e commit c5609f8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pip install Flask-Modals
app = Flask(__name__)
modal = Modal(app)
```

<br>

2. Alternatively if you are using the application factory pattern:

Expand All @@ -42,14 +42,14 @@ pip install Flask-Modals
app = Flask(__name__)
modal.init_app(app)
```

<br>

3. Include the following in the head tag of your base template.

```html
{{ modals() }}
```

<br>

4. Include the following in the modal body.

Expand All @@ -59,10 +59,10 @@ pip install Flask-Modals
<form method="post">
...
```

<br>

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
Expand Down Expand Up @@ -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.

<br>

6. If you want to redirect to the same page outside the modal, use Flask's
`session` proxy as follows:
Expand Down Expand Up @@ -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)
```

<br>

7. If you want to render a template and not redirect, then use the following
pattern:
Expand Down
13 changes: 8 additions & 5 deletions flask_modals/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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

Expand Down
21 changes: 9 additions & 12 deletions flask_modals/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
'''
Expand All @@ -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):

Expand All @@ -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('<body',
'<body id="turbo-stream__body"')

def handle_starttag(self, tag, attrs):

if self.body and not self.redirect:
if self.body:
if tag == 'body':
self.stream_start = self.getpos()
return
Expand All @@ -82,7 +78,7 @@ def handle_starttag(self, tag, attrs):

def handle_endtag(self, tag):

if self.body and not self.redirect:
if self.body:
if tag == 'body':
self.stream_end = self.getpos()
self.get_stream(endtaglen=len('</body>'))
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion flask_modals/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
})
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='Flask-Modals',
version='0.2.1',
version='0.2.3',
author='Debashish Palit',
author_email='[email protected]',
description='Use forms in Bootstrap 4 modals with Flask.',
Expand Down

0 comments on commit c5609f8

Please sign in to comment.