forked from orbit-oss/flask
Removed deprecated module functionality
This commit is contained in:
parent
84ad89ffa4
commit
817b72d484
17 changed files with 2 additions and 453 deletions
|
|
@ -27,7 +27,6 @@ from .globals import current_app, g, request, session, _request_ctx_stack, \
|
|||
_app_ctx_stack
|
||||
from .ctx import has_request_context, has_app_context, \
|
||||
after_this_request, copy_current_request_context
|
||||
from .module import Module
|
||||
from .blueprints import Blueprint
|
||||
from .templating import render_template, render_template_string
|
||||
|
||||
|
|
|
|||
32
flask/app.py
32
flask/app.py
|
|
@ -29,7 +29,6 @@ from .config import ConfigAttribute, Config
|
|||
from .ctx import RequestContext, AppContext, _AppCtxGlobals
|
||||
from .globals import _request_ctx_stack, request, session, g
|
||||
from .sessions import SecureCookieSessionInterface
|
||||
from .module import blueprint_is_module
|
||||
from .templating import DispatchingJinjaLoader, Environment, \
|
||||
_default_template_ctx_processor
|
||||
from .signals import request_started, request_finished, got_request_exception, \
|
||||
|
|
@ -255,11 +254,6 @@ class Flask(_PackageBoundObject):
|
|||
#: .. versionadded:: 0.4
|
||||
logger_name = ConfigAttribute('LOGGER_NAME')
|
||||
|
||||
#: Enable the deprecated module support? This is active by default
|
||||
#: in 0.7 but will be changed to False in 0.8. With Flask 1.0 modules
|
||||
#: will be removed in favor of Blueprints
|
||||
enable_modules = True
|
||||
|
||||
#: The JSON encoder class to use. Defaults to :class:`~flask.json.JSONEncoder`.
|
||||
#:
|
||||
#: .. versionadded:: 0.10
|
||||
|
|
@ -893,32 +887,6 @@ class Flask(_PackageBoundObject):
|
|||
"""
|
||||
return self.session_interface.make_null_session(self)
|
||||
|
||||
def register_module(self, module, **options):
|
||||
"""Registers a module with this application. The keyword arguments
|
||||
of this function are the same as the ones for the constructor of the
|
||||
:class:`Module` class and will override the values of the module if
|
||||
provided.
|
||||
|
||||
.. versionchanged:: 0.7
|
||||
The module system was deprecated in favor for the blueprint
|
||||
system.
|
||||
"""
|
||||
assert blueprint_is_module(module), 'register_module requires ' \
|
||||
'actual module objects. Please upgrade to blueprints though.'
|
||||
if not self.enable_modules:
|
||||
raise RuntimeError('Module support was disabled but code '
|
||||
'attempted to register a module named %r' % module)
|
||||
else:
|
||||
from warnings import warn
|
||||
warn(DeprecationWarning('Modules are deprecated. Upgrade to '
|
||||
'using blueprints. Have a look into the documentation for '
|
||||
'more information. If this module was registered by a '
|
||||
'Flask-Extension upgrade the extension or contact the author '
|
||||
'of that extension instead. (Registered %r)' % module),
|
||||
stacklevel=2)
|
||||
|
||||
self.register_blueprint(module, **options)
|
||||
|
||||
@setupmethod
|
||||
def register_blueprint(self, blueprint, **options):
|
||||
"""Registers a blueprint on the application.
|
||||
|
|
|
|||
11
flask/ctx.py
11
flask/ctx.py
|
|
@ -17,7 +17,6 @@ from functools import update_wrapper
|
|||
from werkzeug.exceptions import HTTPException
|
||||
|
||||
from .globals import _request_ctx_stack, _app_ctx_stack
|
||||
from .module import blueprint_is_module
|
||||
from .signals import appcontext_pushed, appcontext_popped
|
||||
from ._compat import BROKEN_PYPY_CTXMGR_EXIT, reraise
|
||||
|
||||
|
|
@ -252,16 +251,6 @@ class RequestContext(object):
|
|||
|
||||
self.match_request()
|
||||
|
||||
# XXX: Support for deprecated functionality. This is going away with
|
||||
# Flask 1.0
|
||||
blueprint = self.request.blueprint
|
||||
if blueprint is not None:
|
||||
# better safe than sorry, we don't want to break code that
|
||||
# already worked
|
||||
bp = app.blueprints.get(blueprint)
|
||||
if bp is not None and blueprint_is_module(bp):
|
||||
self.request._is_old_module = True
|
||||
|
||||
def _get_g(self):
|
||||
return _app_ctx_stack.top.g
|
||||
def _set_g(self, value):
|
||||
|
|
|
|||
|
|
@ -1,42 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
flask.module
|
||||
~~~~~~~~~~~~
|
||||
|
||||
Implements a class that represents module blueprints.
|
||||
|
||||
:copyright: (c) 2014 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from .blueprints import Blueprint
|
||||
|
||||
|
||||
def blueprint_is_module(bp):
|
||||
"""Used to figure out if something is actually a module"""
|
||||
return isinstance(bp, Module)
|
||||
|
||||
|
||||
class Module(Blueprint):
|
||||
"""Deprecated module support. Until Flask 0.6 modules were a different
|
||||
name of the concept now available as blueprints in Flask. They are
|
||||
essentially doing the same but have some bad semantics for templates and
|
||||
static files that were fixed with blueprints.
|
||||
|
||||
.. versionchanged:: 0.7
|
||||
Modules were deprecated in favor for blueprints.
|
||||
"""
|
||||
|
||||
def __init__(self, import_name, name=None, url_prefix=None,
|
||||
static_path=None, subdomain=None):
|
||||
if name is None:
|
||||
assert '.' in import_name, 'name required if package name ' \
|
||||
'does not point to a submodule'
|
||||
name = import_name.rsplit('.', 1)[1]
|
||||
Blueprint.__init__(self, name, import_name, url_prefix=url_prefix,
|
||||
subdomain=subdomain, template_folder='templates')
|
||||
|
||||
if os.path.isdir(os.path.join(self.root_path, 'static')):
|
||||
self._static_folder = 'static'
|
||||
|
|
@ -14,7 +14,6 @@ from jinja2 import BaseLoader, Environment as BaseEnvironment, \
|
|||
|
||||
from .globals import _request_ctx_stack, _app_ctx_stack
|
||||
from .signals import template_rendered
|
||||
from .module import blueprint_is_module
|
||||
from ._compat import itervalues, iteritems
|
||||
|
||||
|
||||
|
|
@ -73,16 +72,10 @@ class DispatchingJinjaLoader(BaseLoader):
|
|||
try:
|
||||
module, local_name = posixpath.normpath(template).split('/', 1)
|
||||
blueprint = self.app.blueprints[module]
|
||||
if blueprint_is_module(blueprint):
|
||||
loader = blueprint.jinja_loader
|
||||
if loader is not None:
|
||||
yield loader, local_name
|
||||
except (ValueError, KeyError):
|
||||
pass
|
||||
|
||||
for blueprint in itervalues(self.app.blueprints):
|
||||
if blueprint_is_module(blueprint):
|
||||
continue
|
||||
loader = blueprint.jinja_loader
|
||||
if loader is not None:
|
||||
yield loader, template
|
||||
|
|
@ -97,10 +90,7 @@ class DispatchingJinjaLoader(BaseLoader):
|
|||
loader = blueprint.jinja_loader
|
||||
if loader is not None:
|
||||
for template in loader.list_templates():
|
||||
prefix = ''
|
||||
if blueprint_is_module(blueprint):
|
||||
prefix = name + '/'
|
||||
result.add(prefix + template)
|
||||
result.add(template)
|
||||
|
||||
return list(result)
|
||||
|
||||
|
|
|
|||
|
|
@ -1305,18 +1305,6 @@ class SubdomainTestCase(FlaskTestCase):
|
|||
rv = c.get('/', 'http://test.localhost/')
|
||||
self.assert_equal(rv.data, b'test index')
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_module_static_path_subdomain(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.config['SERVER_NAME'] = 'example.com'
|
||||
from subdomaintestmodule import mod
|
||||
app.register_module(mod)
|
||||
c = app.test_client()
|
||||
rv = c.get('/static/hello.txt', 'http://foo.example.com/')
|
||||
rv.direct_passthrough = False
|
||||
self.assert_equal(rv.data.strip(), b'Hello Subdomain')
|
||||
rv.close()
|
||||
|
||||
def test_subdomain_matching(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.config['SERVER_NAME'] = 'localhost'
|
||||
|
|
@ -1339,28 +1327,6 @@ class SubdomainTestCase(FlaskTestCase):
|
|||
rv = c.get('/', 'http://mitsuhiko.localhost:3000/')
|
||||
self.assert_equal(rv.data, b'index for mitsuhiko')
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_module_subdomain_support(self):
|
||||
app = flask.Flask(__name__)
|
||||
mod = flask.Module(__name__, 'test', subdomain='testing')
|
||||
app.config['SERVER_NAME'] = 'localhost'
|
||||
|
||||
@mod.route('/test')
|
||||
def test():
|
||||
return 'Test'
|
||||
|
||||
@mod.route('/outside', subdomain='xtesting')
|
||||
def bar():
|
||||
return 'Outside'
|
||||
|
||||
app.register_module(mod)
|
||||
|
||||
c = app.test_client()
|
||||
rv = c.get('/test', 'http://testing.localhost/')
|
||||
self.assert_equal(rv.data, b'Test')
|
||||
rv = c.get('/outside', 'http://xtesting.localhost/')
|
||||
self.assert_equal(rv.data, b'Outside')
|
||||
|
||||
def test_multi_route_rules(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
|
|
|
|||
|
|
@ -11,250 +11,12 @@
|
|||
|
||||
import flask
|
||||
import unittest
|
||||
import warnings
|
||||
from flask.testsuite import FlaskTestCase, emits_module_deprecation_warning
|
||||
from flask.testsuite import FlaskTestCase
|
||||
from flask._compat import text_type
|
||||
from werkzeug.exceptions import NotFound
|
||||
from werkzeug.http import parse_cache_control_header
|
||||
from jinja2 import TemplateNotFound
|
||||
|
||||
|
||||
# import moduleapp here because it uses deprecated features and we don't
|
||||
# want to see the warnings
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
from moduleapp import app as moduleapp
|
||||
warnings.simplefilter('default', DeprecationWarning)
|
||||
|
||||
|
||||
class ModuleTestCase(FlaskTestCase):
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_basic_module(self):
|
||||
app = flask.Flask(__name__)
|
||||
admin = flask.Module(__name__, 'admin', url_prefix='/admin')
|
||||
@admin.route('/')
|
||||
def admin_index():
|
||||
return 'admin index'
|
||||
@admin.route('/login')
|
||||
def admin_login():
|
||||
return 'admin login'
|
||||
@admin.route('/logout')
|
||||
def admin_logout():
|
||||
return 'admin logout'
|
||||
@app.route('/')
|
||||
def index():
|
||||
return 'the index'
|
||||
app.register_module(admin)
|
||||
c = app.test_client()
|
||||
self.assert_equal(c.get('/').data, b'the index')
|
||||
self.assert_equal(c.get('/admin/').data, b'admin index')
|
||||
self.assert_equal(c.get('/admin/login').data, b'admin login')
|
||||
self.assert_equal(c.get('/admin/logout').data, b'admin logout')
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_default_endpoint_name(self):
|
||||
app = flask.Flask(__name__)
|
||||
mod = flask.Module(__name__, 'frontend')
|
||||
def index():
|
||||
return 'Awesome'
|
||||
mod.add_url_rule('/', view_func=index)
|
||||
app.register_module(mod)
|
||||
rv = app.test_client().get('/')
|
||||
self.assert_equal(rv.data, b'Awesome')
|
||||
with app.test_request_context():
|
||||
self.assert_equal(flask.url_for('frontend.index'), '/')
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_request_processing(self):
|
||||
catched = []
|
||||
app = flask.Flask(__name__)
|
||||
admin = flask.Module(__name__, 'admin', url_prefix='/admin')
|
||||
@admin.before_request
|
||||
def before_admin_request():
|
||||
catched.append('before-admin')
|
||||
@admin.after_request
|
||||
def after_admin_request(response):
|
||||
catched.append('after-admin')
|
||||
return response
|
||||
@admin.route('/')
|
||||
def admin_index():
|
||||
return 'the admin'
|
||||
@app.before_request
|
||||
def before_request():
|
||||
catched.append('before-app')
|
||||
@app.after_request
|
||||
def after_request(response):
|
||||
catched.append('after-app')
|
||||
return response
|
||||
@app.route('/')
|
||||
def index():
|
||||
return 'the index'
|
||||
app.register_module(admin)
|
||||
c = app.test_client()
|
||||
|
||||
self.assert_equal(c.get('/').data, b'the index')
|
||||
self.assert_equal(catched, ['before-app', 'after-app'])
|
||||
del catched[:]
|
||||
|
||||
self.assert_equal(c.get('/admin/').data, b'the admin')
|
||||
self.assert_equal(catched, ['before-app', 'before-admin',
|
||||
'after-admin', 'after-app'])
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_context_processors(self):
|
||||
app = flask.Flask(__name__)
|
||||
admin = flask.Module(__name__, 'admin', url_prefix='/admin')
|
||||
@app.context_processor
|
||||
def inject_all_regular():
|
||||
return {'a': 1}
|
||||
@admin.context_processor
|
||||
def inject_admin():
|
||||
return {'b': 2}
|
||||
@admin.app_context_processor
|
||||
def inject_all_module():
|
||||
return {'c': 3}
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template_string('{{ a }}{{ b }}{{ c }}')
|
||||
@admin.route('/')
|
||||
def admin_index():
|
||||
return flask.render_template_string('{{ a }}{{ b }}{{ c }}')
|
||||
app.register_module(admin)
|
||||
c = app.test_client()
|
||||
self.assert_equal(c.get('/').data, b'13')
|
||||
self.assert_equal(c.get('/admin/').data, b'123')
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_late_binding(self):
|
||||
app = flask.Flask(__name__)
|
||||
admin = flask.Module(__name__, 'admin')
|
||||
@admin.route('/')
|
||||
def index():
|
||||
return '42'
|
||||
app.register_module(admin, url_prefix='/admin')
|
||||
self.assert_equal(app.test_client().get('/admin/').data, b'42')
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_error_handling(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||
admin = flask.Module(__name__, 'admin')
|
||||
@admin.app_errorhandler(404)
|
||||
def not_found(e):
|
||||
return 'not found', 404
|
||||
@admin.app_errorhandler(500)
|
||||
def internal_server_error(e):
|
||||
return 'internal server error', 500
|
||||
@admin.route('/')
|
||||
def index():
|
||||
flask.abort(404)
|
||||
@admin.route('/error')
|
||||
def error():
|
||||
1 // 0
|
||||
app.register_module(admin)
|
||||
c = app.test_client()
|
||||
rv = c.get('/')
|
||||
self.assert_equal(rv.status_code, 404)
|
||||
self.assert_equal(rv.data, b'not found')
|
||||
rv = c.get('/error')
|
||||
self.assert_equal(rv.status_code, 500)
|
||||
self.assert_equal(b'internal server error', rv.data)
|
||||
|
||||
def test_templates_and_static(self):
|
||||
app = moduleapp
|
||||
app.testing = True
|
||||
c = app.test_client()
|
||||
|
||||
rv = c.get('/')
|
||||
self.assert_equal(rv.data, b'Hello from the Frontend')
|
||||
rv = c.get('/admin/')
|
||||
self.assert_equal(rv.data, b'Hello from the Admin')
|
||||
rv = c.get('/admin/index2')
|
||||
self.assert_equal(rv.data, b'Hello from the Admin')
|
||||
rv = c.get('/admin/static/test.txt')
|
||||
self.assert_equal(rv.data.strip(), b'Admin File')
|
||||
rv.close()
|
||||
rv = c.get('/admin/static/css/test.css')
|
||||
self.assert_equal(rv.data.strip(), b'/* nested file */')
|
||||
rv.close()
|
||||
|
||||
with app.test_request_context():
|
||||
self.assert_equal(flask.url_for('admin.static', filename='test.txt'),
|
||||
'/admin/static/test.txt')
|
||||
|
||||
with app.test_request_context():
|
||||
try:
|
||||
flask.render_template('missing.html')
|
||||
except TemplateNotFound as e:
|
||||
self.assert_equal(e.name, 'missing.html')
|
||||
else:
|
||||
self.assert_true(0, 'expected exception')
|
||||
|
||||
with flask.Flask(__name__).test_request_context():
|
||||
self.assert_equal(flask.render_template('nested/nested.txt'), 'I\'m nested')
|
||||
|
||||
def test_safe_access(self):
|
||||
app = moduleapp
|
||||
|
||||
with app.test_request_context():
|
||||
f = app.view_functions['admin.static']
|
||||
|
||||
try:
|
||||
f('/etc/passwd')
|
||||
except NotFound:
|
||||
pass
|
||||
else:
|
||||
self.assert_true(0, 'expected exception')
|
||||
try:
|
||||
f('../__init__.py')
|
||||
except NotFound:
|
||||
pass
|
||||
else:
|
||||
self.assert_true(0, 'expected exception')
|
||||
|
||||
# testcase for a security issue that may exist on windows systems
|
||||
import os
|
||||
import ntpath
|
||||
old_path = os.path
|
||||
os.path = ntpath
|
||||
try:
|
||||
try:
|
||||
f('..\\__init__.py')
|
||||
except NotFound:
|
||||
pass
|
||||
else:
|
||||
self.assert_true(0, 'expected exception')
|
||||
finally:
|
||||
os.path = old_path
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_endpoint_decorator(self):
|
||||
from werkzeug.routing import Submount, Rule
|
||||
from flask import Module
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
app.url_map.add(Submount('/foo', [
|
||||
Rule('/bar', endpoint='bar'),
|
||||
Rule('/', endpoint='index')
|
||||
]))
|
||||
module = Module(__name__, __name__)
|
||||
|
||||
@module.endpoint('bar')
|
||||
def bar():
|
||||
return 'bar'
|
||||
|
||||
@module.endpoint('index')
|
||||
def index():
|
||||
return 'index'
|
||||
|
||||
app.register_module(module)
|
||||
|
||||
c = app.test_client()
|
||||
self.assert_equal(c.get('/foo/').data, b'index')
|
||||
self.assert_equal(c.get('/foo/bar').data, b'bar')
|
||||
|
||||
|
||||
class BlueprintTestCase(FlaskTestCase):
|
||||
|
||||
def test_blueprint_specific_error_handling(self):
|
||||
|
|
@ -820,5 +582,4 @@ class BlueprintTestCase(FlaskTestCase):
|
|||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(unittest.makeSuite(BlueprintTestCase))
|
||||
suite.addTest(unittest.makeSuite(ModuleTestCase))
|
||||
return suite
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
from flask import Flask
|
||||
|
||||
app = Flask(__name__)
|
||||
from moduleapp.apps.admin import admin
|
||||
from moduleapp.apps.frontend import frontend
|
||||
app.register_module(admin)
|
||||
app.register_module(frontend)
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
from flask import Module, render_template
|
||||
|
||||
|
||||
admin = Module(__name__, url_prefix='/admin')
|
||||
|
||||
|
||||
@admin.route('/')
|
||||
def index():
|
||||
return render_template('admin/index.html')
|
||||
|
||||
|
||||
@admin.route('/index2')
|
||||
def index2():
|
||||
return render_template('./admin/index.html')
|
||||
|
|
@ -1 +0,0 @@
|
|||
/* nested file */
|
||||
|
|
@ -1 +0,0 @@
|
|||
Admin File
|
||||
|
|
@ -1 +0,0 @@
|
|||
Hello from the Admin
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
from flask import Module, render_template
|
||||
|
||||
|
||||
frontend = Module(__name__)
|
||||
|
||||
|
||||
@frontend.route('/')
|
||||
def index():
|
||||
return render_template('frontend/index.html')
|
||||
|
|
@ -1 +0,0 @@
|
|||
Hello from the Frontend
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
{{ url_for('static', filename='test.css') }}
|
||||
{{ url_for('foo.static', filename='test.css') }}
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
from flask import Flask, Module, render_template, url_for
|
||||
|
||||
|
||||
mod = Module(__name__)
|
||||
mod2 = Module(__name__, 'testmod2')
|
||||
mod3 = Module(__name__, name='somemod', subdomain='meh')
|
||||
|
||||
|
||||
@app.after_request
|
||||
def after_request(response):
|
||||
g.db.close()
|
||||
return response
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index_foo():
|
||||
x1 = url_for('somemod.index')
|
||||
x2 = url_for('.index')
|
||||
return render_template('test/index.html')
|
||||
|
||||
|
||||
@mod.route('/')
|
||||
def index():
|
||||
x1 = url_for('somemod.index')
|
||||
x2 = url_for('.index')
|
||||
return render_template('test/index.html')
|
||||
|
||||
|
||||
@mod2.route('/')
|
||||
def mod2_index():
|
||||
return render_template('testmod2/index.html')
|
||||
|
||||
|
||||
@mod3.route('/')
|
||||
def mod3_index():
|
||||
return render_template('something-else/index.html')
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
app.register_module(mod)
|
||||
app.register_module(mod2)
|
||||
|
||||
|
||||
def handle_404(error):
|
||||
return 'Testing', 404
|
||||
app.error_handlers[404] = handle_404
|
||||
Loading…
Add table
Add a link
Reference in a new issue