extended URL generation
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 88f671a · Browse files · 1 file·+24 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+24 added−16 removed
- other:+24 added−16 removed
Provenance
- Commit
444e642- Tree
da4b2f8- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
88f671a
1 file changed+24 added−16 removed
modified:flask.py+24 added-16 removed
54 unmodified lines55 the request object used you can subclass this and set56 :attr:`~flask.Flask.request_class` to your subclass.57 """58 59- endpoint = view_args = None59+ endpoint = view_args = routing_exception = None60 61 @cached_property62 def json(self):63 """If the mimetype is `application/json` this will contain the52 unmodified lines116 self.session = _NullSession()117 self.g = _RequestGlobals()118 self.flashes = None119 120+ try:121+ self.request.endpoint, self.request.view_args = \122+ self.url_adapter.match()123+ except HTTPException, e:124+ self.request.routing_exception = e125+126 def __enter__(self):127 _request_ctx_stack.push(self)128 129 def __exit__(self, exc_type, exc_value, tb):9 unmodified lines139 140 :param endpoint: the endpoint of the URL (name of the function)141 :param values: the variable arguments of the URL rule142 """137- return _request_ctx_stack.top.url_adapter.build(endpoint, values)143+ 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)149 150 151 def get_template_attribute(template_name, attribute):152 """Loads a macro (or variable) a template exports. This can be used to405 unmodified lines558 state = _ModuleSetupState(app, options)559 for func, args in module._register_events:560 func(state, *args)561 551- def add_url_rule(self, rule, endpoint, view_func=None, **options):562+ def add_url_rule(self, rule, endpoint=None, view_func=None, **options):563 """Connects a URL rule. Works exactly like the :meth:`route`564 decorator. If a view_func is provided it will be registered with the565 endpoint.566 14 unmodified lines581 582 app.view_functions['index'] = index583 584 .. versionchanged:: 0.2574- `view_func` parameter added585+ `view_func` parameter added.586 587 :param rule: the URL rule as string588 :param endpoint: the endpoint for the registered URL rule. Flask589 itself assumes the name of the view function as2 unmodified lines592 provided endpoint593 :param options: the options to be forwarded to the underlying594 :class:`~werkzeug.routing.Rule` object595 """596+ if endpoint is None:597+ assert view_func is not None, 'expected view func if endpoint ' \598+ 'is not provided.'599+ endpoint = view_func.__name__600 options['endpoint'] = endpoint601 options.setdefault('methods', ('GET',))602 self.url_map.add(Rule(rule, **options))603 if view_func is not None:64 unmodified lines668 :param options: other options to be forwarded to the underlying669 :class:`~werkzeug.routing.Rule` object.670 """671 def decorator(f):657- self.add_url_rule(rule, f.__name__, f, **options)672+ self.add_url_rule(rule, None, f, **options)673 return f674 return decorator675 676 def errorhandler(self, code):33 unmodified lines710 """Registers a template context processor function."""711 self.template_context_processors.append(f)712 return f713 699- def match_request(self):700- """Matches the current request against the URL map and also701- stores the endpoint and view arguments on the request object702- is successful, otherwise the exception is stored.703- """704- rv = _request_ctx_stack.top.url_adapter.match()705- request.endpoint, request.view_args = rv706- return rv707-714 def dispatch_request(self):715 """Does the request dispatching. Matches the URL and returns the716 return value of the view or error handler. This does not have to717 be a response object. In order to convert the return value to a718 proper response object, call :func:`make_response`.719 """720+ req = _request_ctx_stack.top.request721 try:715- endpoint, values = self.match_request()716- return self.view_functions[endpoint](**values)722+ if req.routing_exception is not None:723+ raise req.routing_exception724+ return self.view_functions[req.endpoint](**req.view_args)725 except HTTPException, e:726 handler = self.error_handlers.get(e.code)727 if handler is None:728 return e123 unmodified lines