summaryrefslogtreecommitdiff
path: root/repoze/bfg/zcml.py
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2009-09-14 04:33:38 +0000
committerChris McDonough <chrism@agendaless.com>2009-09-14 04:33:38 +0000
commit04e182bbc0c077afcd921f0df4231020549dc217 (patch)
treea994d2508aaac376b76e04dd4335129b6391dca1 /repoze/bfg/zcml.py
parentf587c76deac60c0a328975dcc4641d0f85984e63 (diff)
downloadpyramid-04e182bbc0c077afcd921f0df4231020549dc217.tar.gz
pyramid-04e182bbc0c077afcd921f0df4231020549dc217.tar.bz2
pyramid-04e182bbc0c077afcd921f0df4231020549dc217.zip
- A ZCML ``view`` directive (and the associated ``bfg_view``
decorator) can now accept an "attr" value. If an "attr" value is supplied, it is considered a method named of the view object to be called when the response is required. This is typically only good for views that are classes or instances (not so useful for functions, as functions typically have no methods other than ``__call__``). - A ZCML ``view`` directive (and the associated ``bfg_view`` decorator) can now accept a "template" value. If a "template" value is supplied, and the view callable returns a dictionary, the associated template is rendered with the dictionary as keyword arguments.
Diffstat (limited to 'repoze/bfg/zcml.py')
-rw-r--r--repoze/bfg/zcml.py44
1 files changed, 40 insertions, 4 deletions
diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py
index f584d17fb..3a2a24819 100644
--- a/repoze/bfg/zcml.py
+++ b/repoze/bfg/zcml.py
@@ -39,6 +39,7 @@ from repoze.bfg.interfaces import ILogger
from repoze.bfg.interfaces import IPackageOverrides
from repoze.bfg.interfaces import IRequest
from repoze.bfg.interfaces import IRouteRequest
+from repoze.bfg.interfaces import ITemplateRendererFactory
from repoze.bfg.path import package_name
@@ -81,6 +82,8 @@ def view(
request_method=None,
request_param=None,
containment=None,
+ attr=None,
+ template=None,
cacheable=True, # not used, here for b/w compat < 0.8
):
@@ -164,8 +167,14 @@ def view(
else:
score = sys.maxint
+ if template and (not ':' in template) and (not os.path.isabs(template)):
+ # if it's not a package:relative/name and it's not an
+ # /absolute/path it's a relative/path; this means its relative
+ # to the package in which the ZCML file is defined.
+ template = '%s:%s' % (package_name(_context.resolve('.')), template)
+
def register():
- derived_view = derive_view(view, permission, predicates)
+ derived_view = derive_view(view, permission, predicates, attr, template)
r_for_ = for_
r_request_type = request_type
if r_for_ is None:
@@ -210,7 +219,7 @@ def view(
name, _context.info)
_context.action(
discriminator = ('view', for_, name, request_type, IView, containment,
- request_param, request_method, route_name),
+ request_param, request_method, route_name, attr),
callable = register,
args = (),
)
@@ -234,8 +243,9 @@ def notfound(_context, view):
def forbidden(_context, view):
view_utility(_context, view, IForbiddenView)
-def derive_view(original_view, permission=None, predicates=()):
- mapped_view = map_view(original_view)
+def derive_view(original_view, permission=None, predicates=(), attr=None,
+ template=None):
+ mapped_view = map_view(original_view, attr, template)
secured_view = secure_view(mapped_view, permission)
debug_view = authdebug_view(secured_view, permission)
derived_view = predicate_wrap(debug_view, predicates)
@@ -530,6 +540,22 @@ def connect_route(path, name, factory):
mapper = getUtility(IRoutesMapper)
mapper.connect(path, name, factory)
+class ITemplateRendererDirective(Interface):
+ renderer = GlobalObject(
+ title=u'ITemplateRendererFactory implementation',
+ required=True)
+
+ extension = TextLine(
+ title=u'Filename extension (e.g. ".pt")',
+ required=False)
+
+def template_renderer(_context, renderer, extension=''):
+ # renderer factories must be registered eagerly so they can be
+ # found by the view machinery
+ sm = getSiteManager()
+ sm.registerUtility(renderer, ITemplateRendererFactory, name=extension)
+ _context.action(discriminator=(ITemplateRendererFactory, extension))
+
class IStaticDirective(Interface):
name = TextLine(
title=u"The URL prefix of the static view",
@@ -591,6 +617,16 @@ class IViewDirective(Interface):
required=False,
)
+ attr = TextLine(
+ title=u'The callable attribute of the view object(default is __call__)',
+ description=u'',
+ required=False)
+
+ template = TextLine(
+ title=u'The template asssociated with the view',
+ description=u'',
+ required=False)
+
request_type = TextLine(
title=u"The request type string or dotted name interface for the view",
description=(u"The view will be called if the interface represented by "