Skip to content

Commit

Permalink
Update code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
deb17 committed May 24, 2021
1 parent 4e280e9 commit be0101a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 37 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ JavaScript. Normal form submission in modals has its own problems.

This Flask extension eases the process of using forms in (Bootstrap 4) modals.
No JavaScript coding is required on your part. The ajax calls are handled behind
the scenes with html-over-the-wire Turbo library. You can code in pure Python -
flashing messages and rendering templates.
the scenes with html-over-the-wire [Turbo](https://turbo.hotwire.dev/) library.
You can code in pure Python - flashing messages and rendering templates.

### Installation

Expand Down Expand Up @@ -158,7 +158,7 @@ arguments as Flask's `redirect` and `render_template` functions respectively.

### Note

1. See the example folder for more details.
1. See the example folder in the repo for more details.

2. The extension loads the NProgress js library to display a progress bar during
form submission.
28 changes: 17 additions & 11 deletions flask_modals/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
from flask import (Blueprint, render_template, get_flashed_messages,
_app_ctx_stack, session, redirect, request)
from jinja2 import Markup
from flask_modals.turbo import Turbo
from flask_modals import turbo

from flask_modals.parser import parse_html

turbo = Turbo()


def modal_messages():
'''This will be available in the app templates for use in the modal
Expand Down Expand Up @@ -66,25 +64,29 @@ def render_template_modal(*args, **kwargs):


def redirect_to(*args, **kwargs):
'''Use this function instead of Flask's `redirect` if you are
redirecting to a page without modal forms.
'''Use this function instead of Flask's `redirect` if you want to do
a full reload of the page on form submit. Turbo Drive normally does
an ajax load of the page.
'''

session['_keep_flashes'] = True
return redirect(*args, **kwargs)


def render_template_redirect(*args, **kwargs):
'''Reload the page to unload the Turbo library. See template
`turbo.html`. Flashes need to be saved so that they are again
available on reload.
'''Reload the page if session variable is set, i.e. the route is the
target of the `redirect_to` function.
'''

setup_for_reload()
return render_template(*args, **kwargs)


def setup_for_reload():
'''Setup for reload conditionally. App context variable `_reload`
causes the reload to happen - see template `turbo.html`. Flashes
need to be saved so that they are again available on reload.
'''

if '_keep_flashes' in session:
del session['_keep_flashes']
Expand All @@ -94,6 +96,9 @@ def setup_for_reload():


def response(template=None):
'''Use this decorator if coding `render_template_modal` in a number
of places in a view function looks verbose.
'''
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
Expand All @@ -118,8 +123,9 @@ def __init__(self, app=None):
self.init_app(app)

def init_app(self, app):
'''Initialize the Turbo-Flask extension and register template
globals for app.
'''Initialize the extension.
Call method for blueprint and register template globals for app.
'''

self.register_blueprint(app)
Expand All @@ -139,7 +145,7 @@ def register_blueprint(self, app):
@staticmethod
def show_flashed_messages(*args, **kwargs):
'''Delegate to get_flashed_messages if _modal is set on the
Flask g object. If it is not set, it means modal is not being
app context. If it is not set, it means modal is not being
displayed and so we do not flash messages in it.
'''

Expand Down
47 changes: 25 additions & 22 deletions flask_modals/turbo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,37 @@
_VER = 'v7.0.0-beta.5-LhgiwOUjafYu3bb8VbTv'


class Turbo:
def load(version=_VER, url=None):

def load(self, version=_VER, url=None):
if url is None:
url = f'{_CDN}/pin/{_PKG}@{version}/min/{_PKG}.js'
return Markup(f'<script type="module" src="{url}"></script>')

if url is None:
url = f'{_CDN}/pin/{_PKG}@{version}/min/{_PKG}.js'
return Markup(f'<script type="module" src="{url}"></script>')

def can_stream(self):
'''Returns `True` if the client accepts turbo streams.'''
def can_stream():
'''Returns `True` if the client accepts turbo streams.'''

stream_mimetype = 'text/vnd.turbo-stream.html'
best = request.accept_mimetypes.best_match([
stream_mimetype, 'text/html'])
return best == stream_mimetype
stream_mimetype = 'text/vnd.turbo-stream.html'
best = request.accept_mimetypes.best_match([
stream_mimetype, 'text/html'])
return best == stream_mimetype

def _make_stream(self, action, content, target):
return (f'<turbo-stream action="{action}" target="{target}">'
f'<template>{content}</template></turbo-stream>')

def replace(self, content, target):
return self._make_stream('replace', content, target)
def _make_stream(action, content, target):
return (f'<turbo-stream action="{action}" target="{target}">'
f'<template>{content}</template></turbo-stream>')

def update(self, content, target):
return self._make_stream('update', content, target)

def stream(self, response_stream):
'''Create a turbo stream response.'''
def replace(content, target):
return _make_stream('replace', content, target)

return current_app.response_class(
response_stream, mimetype='text/vnd.turbo-stream.html')

def update(content, target):
return _make_stream('update', content, target)


def stream(response_stream):
'''Create a turbo stream response.'''

return current_app.response_class(
response_stream, mimetype='text/vnd.turbo-stream.html')
8 changes: 7 additions & 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.7',
version='0.2.8',
author='Debashish Palit',
author_email='[email protected]',
description='Use forms in Bootstrap 4 modals with Flask.',
Expand All @@ -23,7 +23,13 @@
],
zip_safe=False,
classifiers=[
'Development Status :: 3 - Alpha',
'Environment :: Web Environment',
'Intended Audience :: Developers',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
Expand Down

0 comments on commit be0101a

Please sign in to comment.