Browse Source

Merge branch 'master' of github.com:mitsuhiko/flask

pull/309/merge
Armin Ronacher 14 years ago
parent
commit
f92a275d54
  1. 6
      flask/app.py
  2. 5
      flask/blueprints.py
  3. 2
      flask/testing.py
  4. 3
      flask/testsuite/__init__.py
  5. 29
      flask/testsuite/basic.py
  6. 89
      flask/testsuite/blueprints.py
  7. 3
      flask/testsuite/deprecations.py
  8. 3
      flask/testsuite/helpers.py
  9. 3
      flask/testsuite/templating.py
  10. 3
      flask/testsuite/testing.py
  11. 4
      setup.py

6
flask/app.py

@ -945,11 +945,15 @@ class Flask(_PackageBoundObject):
subdomain matching is in use.
:param strict_slashes: can be used to disable the strict slashes
setting for this rule. See above.
:param endpoint: Since version 0.8 you can also pass the enpoint,
it will be used instead of generating the endpoint
from the function name.
:param options: other options to be forwarded to the underlying
:class:`~werkzeug.routing.Rule` object.
"""
def decorator(f):
self.add_url_rule(rule, None, f, **options)
endpoint = options.pop("endpoint", None)
self.add_url_rule(rule, endpoint, f, **options)
return f
return decorator

5
flask/blueprints.py

@ -157,7 +157,8 @@ class Blueprint(_PackageBoundObject):
:func:`url_for` function is prefixed with the name of the blueprint.
"""
def decorator(f):
self.add_url_rule(rule, f.__name__, f, **options)
endpoint = options.pop("endpoint", f.__name__)
self.add_url_rule(rule, endpoint, f, **options)
return f
return decorator
@ -165,6 +166,8 @@ class Blueprint(_PackageBoundObject):
"""Like :meth:`Flask.add_url_rule` but for a blueprint. The endpoint for
the :func:`url_for` function is prefixed with the name of the blueprint.
"""
if endpoint:
assert '.' not in endpoint, "Blueprint endpoint's should not contain dot's"
self.record(lambda s:
s.add_url_rule(rule, endpoint, view_func, **options))

2
flask/testing.py

@ -10,6 +10,8 @@
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
from contextlib import contextmanager
from werkzeug.test import Client, EnvironBuilder
from flask import _request_ctx_stack

3
flask/testsuite/__init__.py

@ -9,6 +9,9 @@
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
import os
import sys
import flask

29
flask/testsuite/basic.py

@ -8,6 +8,9 @@
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
import re
import flask
import unittest
@ -895,6 +898,32 @@ class BasicFunctionalityTestCase(FlaskTestCase):
rv = c.post('/foo', data={}, follow_redirects=True)
self.assert_equal(rv.data, 'success')
def test_route_decorator_custom_endpoint(self):
app = flask.Flask(__name__)
app.debug = True
@app.route('/foo/')
def foo():
return flask.request.endpoint
@app.route('/bar/', endpoint='bar')
def for_bar():
return flask.request.endpoint
@app.route('/bar/123', endpoint='123')
def for_bar_foo():
return flask.request.endpoint
with app.test_request_context():
assert flask.url_for('foo') == '/foo/'
assert flask.url_for('bar') == '/bar/'
assert flask.url_for('123') == '/bar/123'
c = app.test_client()
self.assertEqual(c.get('/foo/').data, 'foo')
self.assertEqual(c.get('/bar/').data, 'bar')
self.assertEqual(c.get('/bar/123').data, '123')
class ContextTestCase(FlaskTestCase):

89
flask/testsuite/blueprints.py

@ -8,6 +8,9 @@
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
import flask
import unittest
import warnings
@ -415,6 +418,92 @@ class BlueprintTestCase(FlaskTestCase):
self.assert_equal(c.get('/').data, '1')
self.assert_equal(c.get('/page/2').data, '2')
def test_route_decorator_custom_endpoint(self):
bp = flask.Blueprint('bp', __name__)
@bp.route('/foo')
def foo():
return flask.request.endpoint
@bp.route('/bar', endpoint='bar')
def foo_bar():
return flask.request.endpoint
@bp.route('/bar/123', endpoint='123')
def foo_bar_foo():
return flask.request.endpoint
@bp.route('/bar/foo')
def bar_foo():
return flask.request.endpoint
app = flask.Flask(__name__)
app.register_blueprint(bp, url_prefix='/py')
@app.route('/')
def index():
return flask.request.endpoint
c = app.test_client()
self.assertEqual(c.get('/').data, 'index')
self.assertEqual(c.get('/py/foo').data, 'bp.foo')
self.assertEqual(c.get('/py/bar').data, 'bp.bar')
self.assertEqual(c.get('/py/bar/123').data, 'bp.123')
self.assertEqual(c.get('/py/bar/foo').data, 'bp.bar_foo')
def test_route_decorator_custom_endpoint_with_dots(self):
bp = flask.Blueprint('bp', __name__)
@bp.route('/foo')
def foo():
return flask.request.endpoint
try:
@bp.route('/bar', endpoint='bar.bar')
def foo_bar():
return flask.request.endpoint
except AssertionError:
pass
else:
raise AssertionError('expected AssertionError not raised')
try:
@bp.route('/bar/123', endpoint='bar.123')
def foo_bar_foo():
return flask.request.endpoint
except AssertionError:
pass
else:
raise AssertionError('expected AssertionError not raised')
def foo_foo_foo():
pass
self.assertRaises(
AssertionError,
lambda: bp.add_url_rule(
'/bar/123', endpoint='bar.123', view_func=foo_foo_foo
)
)
self.assertRaises(
AssertionError,
bp.route('/bar/123', endpoint='bar.123'),
lambda: None
)
app = flask.Flask(__name__)
app.register_blueprint(bp, url_prefix='/py')
c = app.test_client()
self.assertEqual(c.get('/py/foo').data, 'bp.foo')
# The rule's din't actually made it through
rv = c.get('/py/bar')
assert rv.status_code == 404
rv = c.get('/py/bar/123')
assert rv.status_code == 404
def suite():
suite = unittest.TestSuite()

3
flask/testsuite/deprecations.py

@ -8,6 +8,9 @@
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
import flask
import unittest
from flask.testsuite import FlaskTestCase, catch_warnings

3
flask/testsuite/helpers.py

@ -8,6 +8,9 @@
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
import os
import flask
import unittest

3
flask/testsuite/templating.py

@ -8,6 +8,9 @@
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
import flask
import unittest
from flask.testsuite import FlaskTestCase

3
flask/testsuite/testing.py

@ -8,6 +8,9 @@
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
import flask
import unittest
from flask.testsuite import FlaskTestCase

4
setup.py

@ -89,9 +89,7 @@ setup(
'and good intentions',
long_description=__doc__,
packages=['flask', 'flask.testsuite'],
package_data={
'flask.testsuite': ['test_apps/*', 'static/*', 'templates/*']
},
include_package_data = True,
zip_safe=False,
platforms='any',
install_requires=[

Loading…
Cancel
Save