]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add path_{kw,}args attributes to RequestHandler.
authorBen Darnell <ben@bendarnell.com>
Sun, 2 Dec 2012 18:37:54 +0000 (13:37 -0500)
committerBen Darnell <ben@bendarnell.com>
Sun, 2 Dec 2012 18:37:54 +0000 (13:37 -0500)
tornado/test/web_test.py
tornado/web.py
website/sphinx/releases/next.rst
website/sphinx/web.rst

index 39ba5ea9f13d247701ea7091808b2b3b60d62d15..73f7987758995e5609e87bdb7e67bc520aa41558 100644 (file)
@@ -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<path>.*)', 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'}})
index eb235488d84f828ed8c6191251b940e60c730c59..7d45ce5384d9aadeec909040e76120346245efa7 100644 (file)
@@ -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:
index 1e3cf40d9cd573a772e66eacc909ef1830976f53..d784660e9412cc13d3929916fa4c6906a11ebbd7 100644 (file)
@@ -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()``
index 4ae22e0836c30f28a83dd396d9257849b41b8f1e..4fba9869518d8aa621184434bf13f932036d7ccb 100644 (file)
       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
    ^^^^^^