Replace Flask modules to blueprints #3

Merged
merged 2 commits into from Feb 24, 2012
View
@@ -6,16 +6,16 @@
.. seealso::
- Module :mod:`langdev.web.home`
+ Blueprint :mod:`langdev.web.home`
Website home.
- Module :mod:`langdev.web.user`
+ Blueprint :mod:`langdev.web.user`
User authentications, personal pages, and so on.
- Module :mod:`langdev.web.forum`
+ Blueprint :mod:`langdev.web.forum`
Forum post pages and comments.
- Module :mod:`langdev.web.thirdparty`
+ Blueprint :mod:`langdev.web.thirdparty`
Third-party applications.
Module :mod:`langdev.web.pager`
@@ -53,32 +53,32 @@
import langdev.orm
-#: The :class:`dict` of modules to be registered by default.
+#: The :class:`dict` of blueprints to be registered by default.
#: Keys are import names in string, and values are keyword arguments for
-#: :meth:`flask.Flask.register_module()` method. ::
+#: :meth:`flask.Flask.register_blueprint()` method. ::
#:
-#: modules = {'module.name:var': {'url_prefix': '/path'},
-#: 'module.name2:var2': {}}
+#: blueprints = {'module.name:blueprint': {'url_prefix': '/path'},
+#: 'module.name2:blueprint2': {}}
#:
-#: It can be extended by ``MODULES`` configuration also::
+#: It can be extended by ``BLUEPRINTS`` configuration also::
#:
-#: MODULES = {'module.name:var': {}}
+#: BLUEPRINTS = {'module.name:var': {}}
#:
#: .. seealso::
#:
#: Function :func:`werkzeug.utils.import_string()`
#: The function that imports an object based on a string, provided by
#: Werkzeug_.
#:
-#: Flask --- :ref:`working-with-modules`
-#: Flask_ provides 'module' facilities for large applications.
+#: Flask --- :ref:`flask:blueprints`
+#: Flask_ provides 'blueprint' facilities for large applications.
#:
#: .. _Werkzeug: http://werkzeug.pocoo.org/
#: .. _Flask: http://flask.pocoo.org/
-modules = {'langdev.web.home:home': {},
- 'langdev.web.user:user': {'url_prefix': '/users'},
- 'langdev.web.forum:forum': {'url_prefix': '/posts'},
- 'langdev.web.thirdparty:thirdparty': {'url_prefix': '/apps'}}
+blueprints = {'langdev.web.home:home': {},
+ 'langdev.web.user:user': {'url_prefix': '/users'},
+ 'langdev.web.forum:forum': {'url_prefix': '/posts'},
+ 'langdev.web.thirdparty:thirdparty': {'url_prefix': '/apps'}}
#: The list of WSGI middlewares to hook in. Its elements are import names in
#: string. ::
@@ -189,11 +189,11 @@ def app(app):
else:
raise TypeError('modifier must be a callable object, not ' +
repr(modifier))
- mods = dict(modules)
- mods.update(app.config.get('MODULES', {}))
- for import_name, kwargs in modules.iteritems():
- modobj = werkzeug.utils.import_string(import_name)
- app.register_module(modobj, **(kwargs or {}))
+ mods = dict(blueprints)
+ mods.update(app.config.get('BLUEPRINTS', {}))
+ for import_name, kwargs in mods.iteritems():
+ bp = werkzeug.utils.import_string(import_name)
+ app.register_blueprint(bp, **(kwargs or {}))
app.before_request_funcs.setdefault(None, []).extend(before_request_funcs)
app.after_request_funcs.setdefault(None, []).extend(after_request_funcs)
app.error_handlers.update(error_handlers)
@@ -290,12 +290,10 @@ def method_for(endpoint):
"""
ctx = flask.globals._request_ctx_stack.top
- if '.' not in endpoint:
- mod = ctx.request.module
- if mod is not None:
- endpoint = mod + '.' + endpoint
- elif endpoint.startswith('.'):
- endpoint = endpoint[1:]
+ if endpoint.startswith('.'):
+ bp = ctx.request.blueprint
+ if bp is not None:
+ endpoint = bp + endpoint
methods = ctx.app.url_map._rules_by_endpoint[endpoint][0].methods
try:
return iter(methods.difference(['HEAD', 'OPTIONS'])).next()
View
@@ -13,10 +13,10 @@
import langdev.web.pager
-#: Forum web pages module.
+#: Forum web pages blueprint.
#:
-#: .. seealso:: Flask --- :ref:`working-with-modules`
-forum = Module(__name__)
+#: .. seealso:: Flask --- :ref:`flask:blueprints`
+forum = Blueprint('forum', __name__)
def get_post(post_id):
@@ -117,7 +117,7 @@ def write():
form.populate_obj(post)
with g.session.begin():
g.session.add(post)
- return redirect(url_for('post', post_id=post.id), 302)
+ return redirect(url_for('.post', post_id=post.id), 302)
return write_form(form=form)
@@ -147,7 +147,7 @@ def delete(post_id):
langdev.web.user.ensure_signin(post.author)
with g.session.begin():
g.session.delete(post)
- return redirect(url_for('posts'), 302)
+ return redirect(url_for('.posts'), 302)
def get_comment(comment_id, post_id=None):
@@ -187,7 +187,7 @@ def comment(post_id, comment_id):
response = render('forum/base', comment, comment=comment)
if re.match(r'^(application/xhtml\+xml|text/html)\s*($|;)',
response.content_type):
- return redirect(url_for('post', post_id=post_id) +
+ return redirect(url_for('.post', post_id=post_id) +
'#comment-{0}'.format(comment.id))
return response
@@ -198,5 +198,5 @@ def delete_comment(post_id, comment_id):
langdev.web.user.ensure_signin(comment.author)
with g.session.begin():
g.session.delete(comment)
- return redirect(url_for('post', post_id=post_id), 302)
+ return redirect(url_for('.post', post_id=post_id), 302)
View
@@ -2,13 +2,13 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
-from flask import *
+from flask import Blueprint, redirect, url_for
-#: Home module.
+#: Home blueprint.
#:
-#: .. seealso:: Flask --- :ref:`working-with-modules`
-home = Module(__name__)
+#: .. seealso:: Flask --- :ref:`flask:blueprints`
+home = Blueprint('home', __name__)
@home.route('/')
@@ -1,6 +1,6 @@
{% from 'macro.html' import only_for_style %}
{%- macro static_url_for(filename) -%}
- {%- set url = url_for('.static', filename=filename) -%}
+ {%- set url = url_for('static', filename=filename) -%}
{%- if config['DEBUG'] -%} {{ url.replace('.min.', '.') }}
{%- else -%} {{ url }} {%- endif -%}
{%- endmacro -%}
@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
- <id>{{ url_for('posts', _external=True) }}</id>
+ <id>{{ url_for('.posts', _external=True) }}</id>
<title>LangDev</title>
- <link href="{{ url_for('posts', _external=True) }}" rel="alternate" />
+ <link href="{{ url_for('.posts', _external=True) }}" rel="alternate" />
<link href="{{ request.url }}" rel="self" />
{% for post in posts %}
{% if loop.first %}
<updated>{{ post.modified_at.isoformat() }}</updated>
{% endif %}
<entry>
- <id>{{ url_for('post', post_id=post.id, _external=True) }}</id>
+ <id>{{ url_for('.post', post_id=post.id, _external=True) }}</id>
<title>{{ post.title }}</title>
- <link href="{{ url_for('post', post_id=post.id, _external=True) }}"
+ <link href="{{ url_for('.post', post_id=post.id, _external=True) }}"
rel="alternate" />
<author>
<name>{{ post.author }}</name>
@@ -4,5 +4,5 @@
{% if form.errors %}
{{ form.errors }}
{% endif %}
- {{ render_form(form, 'edit', post_id=post.id) }}
+ {{ render_form(form, '.edit', post_id=post.id) }}
{% endblock %}
@@ -4,7 +4,7 @@
{{ post }} &#8212; {{ super() }}
{%- endblock %}
{% block head %}
- <link rel="canonical" href="{{ url_for('post', post_id=post.id,
+ <link rel="canonical" href="{{ url_for('.post', post_id=post.id,
_external=True) }}" />
{% endblock %}
{% block body %}
@@ -15,15 +15,15 @@
<time datetime="{{ post.created_at.isoformat() }}">
{{- post.created_at }}</time>
{% if post.author == current_user %}
- <a href="{{ url_for('edit_form', post_id=post.id) }}">Edit</a>
- {% call render_raw_form('delete', post_id=post.id) %}
+ <a href="{{ url_for('.edit_form', post_id=post.id) }}">Edit</a>
+ {% call render_raw_form('.delete', post_id=post.id) %}
<button type="submit">Delete</button>
{% endcall %}
{% endif %}
<div class="body">{{ post.body_html|safe }}</div>
{{ comments(post) }}
{% if current_user %}
- {{ render_form(comment_form, 'write_comment', post_id=post.id) }}
+ {{ render_form(comment_form, '.write_comment', post_id=post.id) }}
{% endif %}
</article>
{% endblock %}
@@ -41,7 +41,7 @@
<time datetime="{{ comment.created_at.isoformat() }}">
{{- comment.created_at }}</time>
{% if comment.author == current_user %}
- {% call render_raw_form('delete_comment', post_id=comment.post_id,
+ {% call render_raw_form('.delete_comment', post_id=comment.post_id,
comment_id=comment.id) %}
<button type="submit">Delete</button>
{% endcall %}
@@ -2,16 +2,16 @@
{% from 'pager.html' import render_pager %}
{% block head %}
<link rel="alternate"
- href="{{ config.get('FORUM_FEED_HREF', url_for('atom', _external=True)) }}"
+ href="{{ config.get('FORUM_FEED_HREF', url_for('.atom', _external=True)) }}"
type="{{ config.get('FORUM_FEED_TYPE', 'application/atom+xml') }}" />
{% if next %}
- <link rel="next" href="{{ url_for('posts', next=next.id,
+ <link rel="next" href="{{ url_for('.posts', next=next.id,
limit=limit, view=view) }}" />
{% endif %}
{% endblock %}
{% block body %}
{% if current_user %}
- <a href="{{ url_for('write_form') }}"
+ <a href="{{ url_for('.write_form') }}"
class="btn large">Write a post</a>
{% endif %}
{% if view == 'summary' %}
@@ -42,7 +42,7 @@
</a>
</td>
<td class="title">
- <a href="{{ url_for('post', post_id=post.id) }}">{{ post }}</a>
+ <a href="{{ url_for('.post', post_id=post.id) }}">{{ post }}</a>
</td>
<td class="created-at">
<time datetime="{{ post.created_at.isoformat() }}">
@@ -71,14 +71,14 @@
{{- post.created_at.date() -}}
</time></dd>
<dt class="title">Title:</dt>
- <dd class="title"><h1><a href="{{ url_for('post', post_id=post.id) }}">
+ <dd class="title"><h1><a href="{{ url_for('.post', post_id=post.id) }}">
{{- post -}}
</a></h1></dd>
</dl>
</header>
<section class="content">{{ post.body_html|safe }}</section>
<footer>
- <a href="{{ url_for('post', post_id=post.id) -}}
+ <a href="{{ url_for('.post', post_id=post.id) -}}
{% if post.replies.first() -%}
#comment-{{ post.replies.first().id }}
{%- else -%}
@@ -90,5 +90,5 @@
{% endfor %}
</div>
{% endif %}
- {{ render_pager(pager, limit, 'posts') }}
+ {{ render_pager(pager, limit, '.posts') }}
{% endblock %}
@@ -1,5 +1,5 @@
{% extends '/forum/base.html' %}
{% from 'form.html' import render_form %}
{% block body %}
- {{ render_form(form, 'write') }}
+ {{ render_form(form, '.write') }}
{% endblock %}
@@ -41,7 +41,7 @@
<h2><abbr title="Single Sign-on">SSO</abbr></h2>
<h3><code>POST
- {{ url_for('sso', app_key='yourappkey', user_login='userlogin')
+ {{ url_for('.sso', app_key='yourappkey', user_login='userlogin')
| replace('yourappkey', '<var>YourAppKey</var>')
| replace('userlogin', '<var>UserLoginOrEmail</var>')
| safe}}</code></h3>
@@ -65,7 +65,7 @@
<p>HMAC+SHA1 hashed (when secret key is <code>{{ app.secret_key }}</code>)
hexadecimal digest of <code>5d41402abc4b2a76b9719d911017c592</code> is:
<code>{{ app.hmac('5d41402abc4b2a76b9719d911017c592') }}</code>.</p>
- <pre>POST {{ url_for('sso', app_key=app.key,
+ <pre>POST {{ url_for('.sso', app_key=app.key,
user_login=current_user.login) }} HTTP/1.1
Host: {{ request.host }}
Accept: application/json
@@ -104,7 +104,7 @@
request.</p>
<h2>Delete</h2>
- {% call render_raw_form('delete_app', app_key=app.key) %}
+ {% call render_raw_form('.delete_app', app_key=app.key) %}
<input type="submit" value="Delete" />
{% endcall %}
<script>
@@ -1,12 +1,12 @@
{% extends 'thirdparty/base.html' %}
{% from 'form.html' import render_form %}
{% block body %}
- {{ render_form(form, 'register') }}
+ {{ render_form(form, '.register') }}
{% if current_user.applications %}
<h2>Your Applications</h2>
<ul class="applications">
{% for app in current_user.applications %}
- <li><a href="{{ url_for('app', app_key=app.key) }}">{{ app }}</a></li>
+ <li><a href="{{ url_for('.app', app_key=app.key) }}">{{ app }}</a></li>
{% endfor %}
</ul>
{% endif %}
@@ -21,5 +21,5 @@
{% endfor %}
</tbody>
</table>
- {{ render_pager(pager, limit, 'posts') }}
+ {{ render_pager(pager, limit, '.posts') }}
{% endblock %}
@@ -1,12 +1,12 @@
{% extends '/user/base.html' %}
{% from 'form.html' import render_form, render_raw_form %}
{% block head %}
- <link rel="canonical" href="{{ url_for('profile', user_login=user.login,
+ <link rel="canonical" href="{{ url_for('.profile', user_login=user.login,
_external=True) }}" />
{% endblock %}
{% block profile %}
{% if form %}
- {{ render_form(form, 'edit', user_login=user.login) }}
+ {{ render_form(form, '.edit', user_login=user.login) }}
{% else %}
<dl>
{% if user.url %}
@@ -19,8 +19,8 @@
</dl>
{% endif %}
{% if user == current_user %}
- {% call render_raw_form('leave', user_login=user.login,
- return_url=url_for('home.main')) %}
+ {% call render_raw_form('.leave', user_login=user.login,
+ return_url=url_for('home.main')) %}
<input type="submit" value="Leave" />
{% endcall %}
<script>
Oops, something went wrong.