Last active
May 23, 2016 14:26
-
-
Save justinfay/a0c4a23a960770c3296e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Idea shamelessly stolen from pyramid. | |
| # utils.py | |
| from collections import Mapping | |
| from functools import wraps | |
| from flask import has_request_context, render_template | |
| def render_dict_template(template): | |
| def decorator(func): | |
| @wraps(func) | |
| def _decorator(*args, **kwargs): | |
| result = func(*args, **kwargs) | |
| # Allow for other return values such as redirect. | |
| if isinstance(result, Mapping) and has_request_context(): | |
| return render_template(template, **result) | |
| return result | |
| return _decorator | |
| return decorator | |
| # views.py | |
| from subshop import shop, storage, utils | |
| @shop.route('/') | |
| @shop.route('/plans') | |
| @utils.render_dict_template('plan_list.html') | |
| def plan_list(): | |
| return {'plans': storage.get_plans()} | |
| # In terminal | |
| >>> from subshop import views | |
| >>> assert views.plan_list() == {'plans': [...]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment