Skip to content
Myr

Basic module support is working, but does not look very nice.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 444e642 · Browse files · 2 files+66 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+66 added−14 removed

  • tests:+25 added−0 removed
  • other:+41 added−14 removed

2 files+66 added−14 removed

modified:tests/flask_tests.py+25 added
296 unmodified lines
297 macro = flask.get_template_attribute('_macro.html', 'hello')298 assert macro('World') == 'Hello World!'299300301class ModuleTestCase(unittest.TestCase):302303 def test_basic_module(self):304 app = flask.Flask(__name__)305 admin = flask.Module('admin', url_prefix='/admin')306 @admin.route('/')307 def index():308 return 'admin index'309 @admin.route('/login')310 def login():311 return 'admin login'312 @admin.route('/logout')313 def logout():314 return 'admin logout'315 @app.route('/')316 def index():317 return 'the index'318 app.register_module('admin', admin)319 c = app.test_client()320 assert c.get('/').data == 'the index'321 assert c.get('/admin/').data == 'admin index'322 assert c.get('/admin/login').data == 'admin login'323 assert c.get('/admin/logout').data == 'admin logout'324325326def suite():327 from minitwit_tests import MiniTwitTestCase328 from flaskr_tests import FlaskrTestCase329 suite = unittest.TestSuite()
12 unmodified lines
modified:flask.py+41 added-14 removed
11 unmodified lines
12from __future__ import with_statement13import os14import sys1516from itertools import chain17from jinja2 import Environment, PackageLoader, FileSystemLoader18from werkzeug import Request as RequestBase, Response as ResponseBase, \19 LocalStack, LocalProxy, create_environ, SharedDataMiddleware, \20 ImmutableDict, cached_property
37 unmodified lines
58 """5960 endpoint = view_args = routing_exception = None6162 @property63 def module(self):64 """The name of the current module"""65 if self.endpoint and '.' in self.endpoint:66 return self.endpoint.rsplit('.', 1)[0]6768 @cached_property69 def json(self):70 """If the mimetype is `application/json` this will contain the71 parsed JSON data.
75 unmodified lines
147 :param endpoint: the endpoint of the URL (name of the function)148 :param values: the variable arguments of the URL rule149 """150 ctx = _request_ctx_stack.top144 if '.' not in endpoint and \145 ctx.request.endpoint is not None \146 and '.' in ctx.request.endpoint:147 endpoint = ctx.request.endpoint.rsplit('.', 1)[0] + '.' + endpoint148 return ctx.url_adapter.build(endpoint.lstrip('.'), values)151 if '.' not in endpoint:152 mod = ctx.request.module153 if mod is not None:154 endpoint = mod + '.' + endpoint155 elif endpoint.startswith('.'):156 endpoint = endpoint[1:]157 return ctx.url_adapter.build(endpoint, values)158159160def get_template_attribute(template_name, attribute):161 """Loads a macro (or variable) a template exports. This can be used to
157 unmodified lines
319320 def add_url_rule(self, rule, endpoint, view_func=None, **options):321 self._record(self._register_rule, (rule, endpoint, view_func, options))322323 def before_request(self, f):324 self._record(lambda s: s.app.before_request_funcs325 .setdefault(self.name, []).append(f), ())326 return f327328 def after_request(self, f):329 self._record(lambda s: s.app.after_request_funcs330 .setdefault(self.name, []).append(f), ())331 return f332333 def _record(self, func, args):334 self._register_events.append((func, args))335336 def _register_rule(self, state, rule, endpoint, view_func, options):
83 unmodified lines
420 #: can for example be used to open database connections or421 #: getting hold of the currently logged in user.422 #: To register a function here, use the :meth:`before_request`423 #: decorator.405 self.before_request_funcs = []424 self.before_request_funcs = {}425426 #: a list of functions that are called at the end of the427 #: request. The function is passed the current response428 #: object and modify it in place or replace it.429 #: To register a function here use the :meth:`after_request`430 #: decorator.412 self.after_request_funcs = []431 self.after_request_funcs = {}432433 #: a list of functions that are called without arguments434 #: to populate the template context. Each returns a dictionary435 #: that the template context is updated with.
280 unmodified lines
716 return decorator717718 def before_request(self, f):719 """Registers a function to run before each request."""701 self.before_request_funcs.append(f)720 self.before_request_funcs.setdefault(None, []).append(f)721 return f722723 def after_request(self, f):724 """Register a function to be run after each request."""706 self.after_request_funcs.append(f)725 self.after_request_funcs.setdefault(None, []).append(f)726 return f727728 def context_processor(self, f):729 """Registers a template context processor function."""
56 unmodified lines
786 If any of these function returns a value it's handled as787 if it was the return value from the view and further788 request handling is stopped.789 """790 funcs = self.before_request_funcs.get(None, ())791 mod = request.module771 for func in self.before_request_funcs:792 if mod and mod in self.before_request_funcs:793 funcs = chain(funcs, self.before_request_funcs[mod])794 for func in funcs:795 rv = func()796 if rv is not None:797 return rv798
5 unmodified lines
804 :param response: a :attr:`response_class` object.805 :return: a new response object or the same, has to be an806 instance of :attr:`response_class`.807 """785 session = _request_ctx_stack.top.session786 if not isinstance(session, _NullSession):787 self.save_session(session, response)788 for handler in self.after_request_funcs:808 ctx = _request_ctx_stack.top809 mod = ctx.request.module810 if not isinstance(ctx.session, _NullSession):811 self.save_session(ctx.session, response)812 funcs = self.after_request_funcs.get(None, ())813 if mod and mod in self.after_request_funcs:814 funcs = chain(funcs, self.after_request_funcs[mod])815 for handler in funcs:816 response = handler(response)817 return response818819 def wsgi_app(self, environ, start_response):
59 unmodified lines