forked from Pylons/pyramid_jinja2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from pyramid.url import model_url, route_url | ||
from pyramid.threadlocal import get_current_request | ||
|
||
__all__ = [ | ||
'model_url_filter', | ||
'route_url_filter', | ||
] | ||
|
||
def model_url_filter(model, *elements, **kw): | ||
"""A filter from ``model`` to a string representing the absolute URL. | ||
this filter call `pyramid.url.model_url`. | ||
""" | ||
request = get_current_request() | ||
return model_url(model, request, *elements, **kw) | ||
|
||
def route_url_filter(route_name, *elements, **kw): | ||
"""A filter from ``route_name`` to a string representing the absolute URL. | ||
this filter call `pyramid.url.route_url`. | ||
""" | ||
request = get_current_request() | ||
return route_url(route_name, request, *elements, **kw) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import unittest | ||
from jinja2 import Environment | ||
from pyramid.configuration import Configurator | ||
|
||
class DummyRequest(object): | ||
application_url = '' | ||
environ = {} | ||
class DummyRoot(object): | ||
__name__ = __parent__ = None | ||
|
||
class DummyModel(object): | ||
__name__ = 'dummy' | ||
__parent__ = DummyRoot() | ||
|
||
class Test_model_url_filter(unittest.TestCase): | ||
def setUp(self): | ||
self.environment = Environment() | ||
from pyramid_jinja2.filters import model_url_filter | ||
self.environment.filters['model_url'] = model_url_filter | ||
self.config = Configurator() | ||
self.config.begin(request=DummyRequest()) | ||
|
||
def tearDown(self): | ||
self.config.end() | ||
|
||
def _callFUT(self, context, tmpl): | ||
tmpl = self.environment.from_string(tmpl) | ||
return tmpl.render(**context) | ||
|
||
|
||
def test_filter(self): | ||
model = DummyModel() | ||
rendered = self._callFUT({'model':model}, '{{model|model_url}}') | ||
self.assertEqual(rendered, '/dummy/') | ||
|
||
def test_filter_with_elements(self): | ||
model = DummyModel() | ||
rendered = self._callFUT({'model':model}, "{{model|model_url('edit')}}") | ||
self.assertEqual(rendered, '/dummy/edit') | ||
|
||
class Test_route_url_filter(unittest.TestCase): | ||
def setUp(self): | ||
self.environment = Environment() | ||
from pyramid_jinja2.filters import route_url_filter | ||
self.environment.filters['route_url'] = route_url_filter | ||
self.config = Configurator() | ||
self.config.begin(request=DummyRequest()) | ||
self.config.add_route('dummy_route1', '/dummy/') | ||
self.config.add_route('dummy_route2', '/dummy/:name/') | ||
|
||
def tearDown(self): | ||
self.config.end() | ||
|
||
def _callFUT(self, context, tmpl): | ||
tmpl = self.environment.from_string(tmpl) | ||
return tmpl.render(**context) | ||
|
||
def test_filter(self): | ||
rendered = self._callFUT({}, "{{'dummy_route1' | route_url }}") | ||
self.assertEqual(rendered, '/dummy/') | ||
|
||
def test_filter_with_arguments(self): | ||
rendered = self._callFUT({}, "{{'dummy_route2' | route_url('x', name='test') }}") | ||
self.assertEqual(rendered, '/dummy/test/x') |