From: Ben Darnell Date: Sun, 2 Dec 2012 18:37:54 +0000 (-0500) Subject: Add path_{kw,}args attributes to RequestHandler. X-Git-Tag: v3.0.0~204 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4ca0a0f29451d690c34d35eb443c7cc65af32052;p=thirdparty%2Ftornado.git Add path_{kw,}args attributes to RequestHandler. --- diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index 39ba5ea9f..73f798775 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -1010,3 +1010,28 @@ class GzipTestCase(SimpleHandlerTestCase): response = self.fetch('/?vary=Accept-Language') self.assertEqual(response.headers['Vary'], 'Accept-Language, Accept-Encoding') + +class PathArgsInPrepareTest(WebTestCase): + class Handler(RequestHandler): + def prepare(self): + self.write(dict(args=self.path_args, kwargs=self.path_kwargs)) + + def get(self, path): + assert path == 'foo' + self.finish() + + def get_app(self): + return Application([('/pos/(.*)', self.Handler), + ('/kw/(?P.*)', self.Handler)]) + + def test_pos(self): + response = self.fetch('/pos/foo') + response.rethrow() + data = json_decode(response.body) + self.assertEqual(data, {'args': ['foo'], 'kwargs': {}}) + + def test_kw(self): + response = self.fetch('/kw/foo') + response.rethrow() + data = json_decode(response.body) + self.assertEqual(data, {'args': [], 'kwargs': {'path': 'foo'}}) diff --git a/tornado/web.py b/tornado/web.py index eb235488d..7d45ce538 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -113,6 +113,8 @@ class RequestHandler(object): self._finished = False self._auto_finish = True self._transforms = None # will be set in _execute + self.path_args = None + self.path_kwargs = None self.ui = ObjectDict((n, self._ui_method(m)) for n, m in application.ui_methods.iteritems()) # UIModules are available as both `modules` and `_modules` in the @@ -1051,6 +1053,9 @@ class RequestHandler(object): try: if self.request.method not in self.SUPPORTED_METHODS: raise HTTPError(405) + self.path_args = [self.decode_argument(arg) for arg in args] + self.path_kwargs = dict((k, self.decode_argument(v, name=k)) + for (k, v) in kwargs.iteritems()) # If XSRF cookies are turned on, reject form submissions without # the proper cookie if self.request.method not in ("GET", "HEAD", "OPTIONS") and \ @@ -1058,10 +1063,8 @@ class RequestHandler(object): self.check_xsrf_cookie() self.prepare() if not self._finished: - args = [self.decode_argument(arg) for arg in args] - kwargs = dict((k, self.decode_argument(v, name=k)) - for (k, v) in kwargs.iteritems()) - getattr(self, self.request.method.lower())(*args, **kwargs) + getattr(self, self.request.method.lower())( + *self.path_args, **self.path_kwargs) if self._auto_finish and not self._finished: self.finish() except Exception, e: diff --git a/website/sphinx/releases/next.rst b/website/sphinx/releases/next.rst index 1e3cf40d9..d784660e9 100644 --- a/website/sphinx/releases/next.rst +++ b/website/sphinx/releases/next.rst @@ -189,3 +189,8 @@ In progress * `tornado.options.options` (and `OptionParser` instances generally) now have a `mockable()` method that returns a wrapper object compatible with `mock.patch`. +* `tornado.web.RequestHandler` has new attributes ``path_args`` and + ``path_kwargs``, which contain the positional and keyword arguments + that are passed to the ``get``/``post``/etc method. These attributes + are set before those methods are called, so they are available during + ``prepare()`` diff --git a/website/sphinx/web.rst b/website/sphinx/web.rst index 4ae22e083..4fba98695 100644 --- a/website/sphinx/web.rst +++ b/website/sphinx/web.rst @@ -35,6 +35,14 @@ The `tornado.httpserver.HTTPRequest` object containing additional request parameters including e.g. headers and body data. + .. attribute:: RequestHandler.path_args + .. attribute:: RequestHandler.path_kwargs + + The ``path_args`` and ``path_kwargs`` attributes contain the positional + and keyword arguments that are passed to the `get`/`post`/etc methods. + These attributes are set before those methods are called, so the values + are available during `prepare`. + Output ^^^^^^