diff options
| author | Chris McDonough <chrism@plope.com> | 2010-11-15 22:52:46 -0500 |
|---|---|---|
| committer | Chris McDonough <chrism@plope.com> | 2010-11-15 22:52:46 -0500 |
| commit | 9e2e1cfa5d7b00b952b79a628347a89d2263f777 (patch) | |
| tree | 5f3dcfaf86f911931ff39afc3f0939e48c273e6d | |
| parent | b1e3633b3874d565c7b6debcf5051bf4e638d4f4 (diff) | |
| download | pyramid-9e2e1cfa5d7b00b952b79a628347a89d2263f777.tar.gz pyramid-9e2e1cfa5d7b00b952b79a628347a89d2263f777.tar.bz2 pyramid-9e2e1cfa5d7b00b952b79a628347a89d2263f777.zip | |
add convenience static_url method to request
| -rw-r--r-- | CHANGES.txt | 6 | ||||
| -rw-r--r-- | TODO.txt | 1 | ||||
| -rw-r--r-- | pyramid/request.py | 35 | ||||
| -rw-r--r-- | pyramid/tests/test_request.py | 26 |
4 files changed, 63 insertions, 5 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index b9f6a69c7..f1e55ffce 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -13,9 +13,9 @@ Features - New API method: ``pyramid.settings.asbool``. -- New API methods for ``pyramid.request.Request``: ``model_url`` and - ``route_url``. These are simple passthroughs for their respective - functions in ``pyramid.url``. +- New API methods for ``pyramid.request.Request``: ``model_url``, + ``route_url``, and ``static_url``. These are simple passthroughs for their + respective functions in ``pyramid.url``. - The ``settings`` object which used to be available only when ``request.settings.get_settings`` was called is now available as @@ -129,4 +129,3 @@ - zcml.file_configure -- Add static_url as method of request. diff --git a/pyramid/request.py b/pyramid/request.py index 891c33fff..dff70d93c 100644 --- a/pyramid/request.py +++ b/pyramid/request.py @@ -8,8 +8,9 @@ from pyramid.interfaces import ISessionFactory from pyramid.exceptions import ConfigurationError from pyramid.decorator import reify -from pyramid.url import route_url from pyramid.url import model_url +from pyramid.url import route_url +from pyramid.url import static_url class TemplateContext(object): pass @@ -220,6 +221,38 @@ class Request(WebobRequest): """ return model_url(model, self, *elements, **kw) + def static_url(self, path, **kw): + """ Generates a fully qualified URL for a static :term:`resource`. + The resource must live within a location defined via the + :meth:`pyramid.configuration.Configurator.add_static_view` + :term:`configuration declaration` or the ``<static>`` ZCML + directive (see :ref:`static_resources_section`). + + This is a convenience method. The result of calling + :meth:`pyramid.request.Request.static_url` is the same as calling + :func:`pyramid.url.static_url` with an explicit ``request`` parameter. + + The :meth:`pyramid.request.Request.static_url` method calls the + :func:`pyramid.url.static_url` function using the Request object as + the ``request`` argument. The ``*kw`` arguments passed to + :meth:`pyramid.request.Request.static_url` are passed through to + :func:`pyramid.url.static_url` unchanged and its result is returned. + + This call to :meth:`pyramid.request.Request.static_url`:: + + request.static_url('mypackage:static/foo.css') + + Is completely equivalent to calling :func:`pyramid.url.static_url` + like this:: + + from pyramid.url import static_url + static_url('mypackage:static/foo.css, request) + + See :func:`pyramid.url.static_url` for more information + + """ + return static_url(path, self, **kw) + # override default WebOb "environ['adhoc_attr']" mutation behavior __getattr__ = object.__getattribute__ __setattr__ = object.__setattr__ diff --git a/pyramid/tests/test_request.py b/pyramid/tests/test_request.py index d12b47642..a398bf3af 100644 --- a/pyramid/tests/test_request.py +++ b/pyramid/tests/test_request.py @@ -266,6 +266,24 @@ class TestRequest(unittest.TestCase): self.assertEqual(result, 'http://example.com:5432/1/2/3/extra1/extra2?a=1#foo') + def test_static_url(self): + from pyramid.interfaces import IStaticURLInfo + environ = { + 'PATH_INFO':'/', + 'SERVER_NAME':'example.com', + 'SERVER_PORT':'5432', + 'QUERY_STRING':'', + 'wsgi.url_scheme':'http', + } + request = self._makeOne(environ) + info = DummyStaticURLInfo('abc') + self.config.registry.registerUtility(info, IStaticURLInfo) + result = request.static_url('pyramid.tests:static/foo.css') + self.assertEqual(result, 'abc') + self.assertEqual(info.args, + ('pyramid.tests:static/foo.css', request, {}) ) + + class Test_route_request_iface(unittest.TestCase): def _callFUT(self, name): from pyramid.request import route_request_iface @@ -323,3 +341,11 @@ class DummyRoute: def generate(self, kw): self.kw = kw return self.result + +class DummyStaticURLInfo: + def __init__(self, result): + self.result = result + + def generate(self, path, request, **kw): + self.args = path, request, kw + return self.result |
