Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make it work with bfg 1.2

git-svn-id: https://svn.bluedynamics.eu/svn/module/bda.bfg.tile/trunk@334 f119a143-da5b-4457-81e3-bee00661a780
  • Loading branch information...
commit f7f0b9b20b25e74b2c701a129c16a659e871a326 1 parent cf9d431
jensens authored
View
68 src/bda/bfg/tile/_api.py
@@ -1,23 +1,31 @@
import os
-from zope.interface import Interface, Attribute, implements
-from zope.component import queryUtility
-from zope.component import getMultiAdapter
-from zope.component import ComponentLookupError
-from repoze.bfg.threadlocal import get_current_registry
from webob import Response
from webob.exc import HTTPFound
-from repoze.bfg.interfaces import IRequest
-from repoze.bfg.interfaces import IResponseFactory
-from repoze.bfg.interfaces import IAuthenticationPolicy
-from repoze.bfg.interfaces import IViewPermission
+from zope.interface import (
+ Interface,
+ Attribute,
+ implements,
+)
+from zope.component import (
+ queryUtility,
+ getMultiAdapter,
+ ComponentLookupError,
+ getSiteManager,
+)
+from repoze.bfg.interfaces import (
+ IRequest,
+ IResponseFactory,
+ IAuthenticationPolicy,
+ IAuthorizationPolicy,
+)
+from repoze.bfg.configuration import _secure_view
+from repoze.bfg.threadlocal import get_current_registry
from repoze.bfg.path import caller_package
from repoze.bfg.renderers import template_renderer_factory
from repoze.bfg.chameleon_zpt import ZPTTemplateRenderer
-#from repoze.bfg.security import ViewPermissionFactory
-from repoze.bfg.security import has_permission
class ITile(Interface):
- """returns on call some HTML snippet."""
+ """Renders some HTML snippet."""
def __call__(model, request):
"""Renders the tile.
@@ -37,7 +45,7 @@ def prepare():
def _update_kw(**kw):
if not ('request' in kw and 'model' in kw):
- raise ValueError, "Eexpected kwargs missing: model, request."
+ raise ValueError, "Expected kwargs missing: model, request."
kw.update({'tile': TileRenderer(kw['model'], kw['request'])})
return kw
@@ -77,11 +85,12 @@ def __call__(self, model, request):
if not self.show:
return u''
if self.path:
- try:
- # XXX: do not catch exception.
+ try:
return render_template(self.path, request=request,
model=model, context=self)
except Exception, e:
+ # XXX: do not catch exception if in debug mode.
+ # todo: check if debug mode and raise, else return as following
return u"Error:<br /><pre>%s</pre>" % e
renderer = getattr(self, self.attribute)
result = renderer()
@@ -111,15 +120,6 @@ def __init__(self, model, request):
self.model, self.request = model, request
def __call__(self, name):
- registry = get_current_registry()
- # XXX fix me. new API in repoze.bfg
- #secured = not not registry.queryUtility(IAuthenticationPolicy)
- #if secured:
- # permitted = registry.getMultiAdapter((self.model, self.request),
- # IViewPermission,
- # name=name)
- # if not permitted:
- # return u'permission denied'
try:
tile = getMultiAdapter((self.model, self.request), ITile, name=name)
except ComponentLookupError, e:
@@ -127,24 +127,22 @@ def __call__(self, name):
(name, e)
return tile
-# Registration
+# Registration
def registerTile(name, path=None, attribute='render',
interface=Interface, _class=Tile, permission='view'):
- if isinstance(interface, basestring):
- pass # XXX: lookup
+ """registers a tile."""
if path:
if not (':' in path or os.path.isabs(path)):
caller = caller_package(level=1)
path = '%s:%s' % (caller.__name__, path)
- factory = _class(path, attribute)
+ view = _class(path, attribute)
registry = get_current_registry()
- registry.registerAdapter(factory, [interface, IRequest],
- ITile, name, event=False)
- # XXX fix me. new API in repoze.bfg
- #if permission:
- # factory = ViewPermissionFactory(permission)
- # registry.registerAdapter(factory, [interface, IRequest],
- # IViewPermission, name)
+ if permission is not None:
+ authn_policy = registry.queryUtility(IAuthenticationPolicy)
+ authz_policy = registry.queryUtility(IAuthorizationPolicy)
+ view = _secure_view(view, permission, authn_policy, authz_policy)
+ registry.registerAdapter(view, [interface, IRequest], ITile, name,
+ event=False)
class tile(object):
"""Tile decorator.
View
47 src/bda/bfg/tile/_api.txt
@@ -1,42 +1,54 @@
-A tile is a piece of web application, i.e. a form, a navigation, etc.
-
-Splitting your application in such small and logic application parts makes it
-easy to re-use this application, simplifies application ajaxification and
-the use of same application parts in different manners.
-
-Imports.:
+Imports.
+::
>>> from bda.bfg.tile import Tile
>>> from bda.bfg.tile import TileRenderer
>>> from bda.bfg.tile import registerTile
>>> from bda.bfg.tile import tile
-We need some dummies as model and request.:
+We need some dummies as model and request.
+::
>>> class Model(object): pass
>>> model = Model()
- >>> from repoze.bfg.request import DEFAULT_REQUEST_FACTORIES
- >>> request = DEFAULT_REQUEST_FACTORIES[None]['factory'](environ={})
+ >>> from repoze.bfg.request import Request
+ >>> environ = {
+ ... 'PATH_INFO':'/',
+ ... 'QUERY_STRING':''
+ ... }
+ >>> request = Request(environ=environ)
+
+For bfg we need to hook our registry into zca.
+::
+
+ >>> from zope.component import getSiteManager
+ >>> from repoze.bfg.threadlocal import get_current_registry
+ >>> sm = getSiteManager.sethook(get_current_registry)
The pure Tile itself. Normally you do not create this directly, this is done
-due registration, see below.:
+due registration, see below.
+::
- >>> mytile = Tile('testdata/tile1.pt', None)
+ >>> tile1pt = 'bda.bfg.tile:testdata/tile1.pt'
+ >>> mytile = Tile(tile1pt, None)
>>> mytile(model, request)
u'<span>Tile One</span>'
Register a tile using the prior template testtemplate. When no object is given,
-the default tile is instanciated as above.:
+the default tile is instanciated as above.
+::
- >>> registerTile('tileone', 'testdata/tile1.pt')
+ >>> registerTile('tileone', tile1pt)
-Render the already registered tile.:
+Render the already registered tile.
+::
>>> TileRenderer(model, request)('tileone')
u'<span>Tile One</span>'
Now the decorator - level=1 is needed for the doctest only to reduce the module
-level.:
+level.
+::
>>> @tile('tiletwo', 'testdata/tile2.pt', level=1)
... class Tile2(Tile):
@@ -46,7 +58,8 @@ level.:
You can define an attribute which is responsible to render the tile instead of
defining a template. By default ``render`` is taken. With the keyword argument
-``attribute`` you can point to a different attribute.:
+``attribute`` you can point to a different attribute.
+::
>>> @tile('attrtile')
... class Tile2(Tile):
View
4 src/bda/bfg/tile/tests.py
@@ -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',
Please sign in to comment.
Something went wrong with that request. Please try again.