Permalink
Browse files

introduce render_tile for more beautiful application code ;-)

git-svn-id: https://svn.bluedynamics.eu/svn/module/bda.bfg.tile/trunk@356 f119a143-da5b-4457-81e3-bee00661a780
  • Loading branch information...
jensens
jensens committed Jan 4, 2010
1 parent b0df58d commit 2a352e74e1b0bc8e31b8bdabeaa034223df55f56
Showing with 50 additions and 26 deletions.
  1. +1 −0 src/bda/bfg/tile/__init__.py
  2. +34 −19 src/bda/bfg/tile/_api.py
  3. +13 −5 src/bda/bfg/tile/_api.txt
  4. +2 −2 src/bda/bfg/tile/tests.py
@@ -1,5 +1,6 @@
from _api import render_template
from _api import render_template_to_response
+from _api import render_tile
from _api import ITile
from _api import TileRenderer
from _api import Tile
View
@@ -76,6 +76,34 @@ def render_template_to_response(path, **kw):
return HTTPFound(location=kw['request'].environ['redirect'])
response_factory = queryUtility(IResponseFactory, default=Response)
return response_factory(result)
+
+def render_tile(model, request, name):
+ """renders a tile. Intended usage is in application code.
+
+ ``model``
+ application model aka context
+
+ ``request``
+ the current request
+
+ ``name``
+ name of the requested tile
+ """
+ try:
+ tile = getMultiAdapter((model, request), ITile, name=name)
+ except ComponentLookupError, e:
+ return u"Tile with name '%s' not found:<br /><pre>%s</pre>" % \
+ (name, cgi.escape(str(e)))
+ return tile
+
+class TileRenderer(object):
+ """Renders a tile. Intended usage is as instance in template code."""
+
+ def __init__(self, model, request):
+ self.model, self.request = model, request
+
+ def __call__(self, name):
+ return render_tile(self.model, self.request, name)
class Tile(object):
implements(ITile)
@@ -115,25 +143,13 @@ def redirect(self, url):
def nodeurl(self):
relpath = [p for p in self.model.path if p is not None]
return '/'.join([self.request.application_url] + relpath)
-
-class TileRenderer(object):
-
- def __init__(self, model, request):
- self.model, self.request = model, request
- def __call__(self, name):
- try:
- #import pdb;pdb.set_trace()
- tile = getMultiAdapter((self.model, self.request), ITile, name=name)
- except ComponentLookupError, e:
- return u"Tile with name '%s' not found:<br /><pre>%s</pre>" % \
- (name, cgi.escape(str(e)))
- return tile
-
def _secure_tile(tile, permission, authn_policy, authz_policy, strict):
"""wraps tile and does security checks.
"""
wrapped_tile = tile
+ if not authn_policy and not authz_policy:
+ return tile
def _secured_tile(context, request):
principals = authn_policy.effective_principals(request)
if authz_policy.permits(context, principals, permission):
@@ -156,15 +172,14 @@ def _permitted(context, request):
decorate_view(wrapped_tile, tile)
return wrapped_tile
-
# Registration
def registerTile(name, path=None, attribute='render',
interface=Interface, _class=Tile,
permission='view', strict=True, _level=2):
"""registers a tile.
``name``
- identifier of the tile it later looked up with.
+ identifier of the tile (for later lookup).
``path``
either relative path to the template or absolute path or path prefixed
@@ -212,7 +227,7 @@ class tile(object):
"""
def __init__(self, name, path=None, attribute='render',
- interface=Interface, permission='view',
+ interface=Interface, permission='view',
strict=True, _level=2):
""" see ``registerTile`` for details on the other parameters.
"""
@@ -227,8 +242,8 @@ def __init__(self, name, path=None, attribute='render',
def __call__(self, ob):
registerTile(self.name,
- self.path,
- self.attribute,
+ path=self.path,
+ attribute=self.attribute,
interface=self.interface,
_class=ob,
permission=self.permission,
View
@@ -3,6 +3,7 @@ Imports.
>>> from bda.bfg.tile import Tile
>>> from bda.bfg.tile import TileRenderer
+ >>> from bda.bfg.tile import render_tile
>>> from bda.bfg.tile import registerTile
>>> from bda.bfg.tile import tile
@@ -40,10 +41,16 @@ doctest only to reduce the module level.
>>> registerTile('tileone', 'testdata/tile1.pt', _level=1)
-Render the already registered tile.
+Render the already registered tile - first how it works in templates.
::
- >>> TileRenderer(model, request)('tileone')
+ >>> tilerenderer = TileRenderer(model, request)
+ >>> tilerenderer('tileone')
+ u'<span>Tile One</span>'
+
+For simplification in Python code the same can be achieved by::
+
+ >>> render_tile(model, request, 'tileone')
u'<span>Tile One</span>'
Now the decorator (ignore the ``_level``)
@@ -52,7 +59,7 @@ Now the decorator (ignore the ``_level``)
>>> @tile('tiletwo', 'testdata/tile2.pt', _level=1)
... class Tile2(Tile):
... data = u'custom'
- >>> TileRenderer(model, request)('tiletwo')
+ >>> render_tile(model, request, 'tiletwo')
u'<span>Tile Two: <b><span>Tile One</span></b></span>'
You can define an attribute which is responsible to render the tile instead of
@@ -64,12 +71,13 @@ defining a template. By default ``render`` is taken. With the keyword argument
... class Tile2(Tile):
... def render(self):
... return u'<h1>Rendered via attribute call</h1>'
- >>> TileRenderer(model, request)('attrtile')
+ >>> render_tile(model, request, 'attrtile')
u'<h1>Rendered via attribute call</h1>'
>>> @tile('attrtile', attribute='foobar')
... class Tile2(Tile):
... def foobar(self):
... return u'<h1>Rendered via attribute foobar call</h1>'
- >>> TileRenderer(model, request)('attrtile')
+ >>> render_tile(model, request, 'attrtile')
u'<h1>Rendered via attribute foobar call</h1>'
+
@@ -4,8 +4,8 @@
from interlude import interact
optionflags = doctest.NORMALIZE_WHITESPACE | \
- doctest.ELLIPSIS | \
- doctest.REPORT_ONLY_FIRST_FAILURE
+ doctest.ELLIPSIS #| \
+ #doctest.REPORT_ONLY_FIRST_FAILURE
TESTFILES = [
'_api.txt',

0 comments on commit 2a352e7

Please sign in to comment.