From e17c8d815136218d7dd07e21cf78f4104d773d48 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Mon, 4 Aug 2008 07:38:58 +0000 Subject: - Add a ``request_type`` attribute to the available attributes of a ``bfg:view`` configure.zcml element. This attribute will have a value which is a dotted Python path, pointing at an interface. If the request object implements this interface when the view lookup is performed, the appropriate view will be called. - Remove "template only" views. These were just confusing and were never documented. --- repoze/bfg/zcml.py | 61 +++++++++++------------------------------------------- 1 file changed, 12 insertions(+), 49 deletions(-) (limited to 'repoze/bfg/zcml.py') diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py index 7d81527d2..343abc1d7 100644 --- a/repoze/bfg/zcml.py +++ b/repoze/bfg/zcml.py @@ -1,68 +1,28 @@ -import os - from zope.component.zcml import handler from zope.component.interface import provideInterface from zope.configuration.exceptions import ConfigurationError from zope.configuration.fields import GlobalObject -from zope.configuration.fields import Path from zope.interface import Interface -from zope.interface import implements -from zope.interface import classProvides from zope.schema import TextLine from repoze.bfg.interfaces import IRequest -from repoze.bfg.interfaces import ITemplateFactory -from repoze.bfg.interfaces import ITemplate from repoze.bfg.interfaces import IViewPermission from repoze.bfg.interfaces import IView -from repoze.bfg.template import Z3CPTTemplateFactory -from repoze.bfg.template import render_template_to_response - from repoze.bfg.security import ViewPermissionFactory -class TemplateOnlyViewFactory(object): - """ Pickleable template-only view factory """ - classProvides(ITemplateFactory) - implements(IView) - - def __init__(self, template): - self.template = template - - def __call__(self, context, request): - kw = dict(view=self, context=context, request=request) - return render_template_to_response(self.template, **kw) - def view(_context, permission=None, for_=None, view=None, name="", - template=None, + request_type=IRequest, ): - if (template and view): - raise ConfigurationError( - 'One of template or view must be specified, not both') - - if template: - template_abs = os.path.abspath(str(_context.path(template))) - if not os.path.exists(template_abs): - raise ConfigurationError('No template file named %s' % template_abs) - utility = Z3CPTTemplateFactory(template_abs) - _context.action( - discriminator = ('utility', ITemplate, template_abs), - callable = handler, - args = ('registerUtility', utility, ITemplate, template_abs), - ) - view = TemplateOnlyViewFactory(template_abs) - if not view: - raise ConfigurationError( - 'Neither template nor factory was specified, though one must be ' - 'specified.') + raise ConfigurationError('"view" attribute was not specified') if for_ is not None: _context.action( @@ -74,18 +34,19 @@ def view(_context, if permission: pfactory = ViewPermissionFactory(permission) _context.action( - discriminator = ('permission', for_,name, IRequest,IViewPermission), + discriminator = ('permission', for_,name, request_type, + IViewPermission), callable = handler, args = ('registerAdapter', - pfactory, (for_, IRequest), IViewPermission, name, + pfactory, (for_, request_type), IViewPermission, name, _context.info), ) _context.action( - discriminator = ('view', for_, name, IRequest, IView), + discriminator = ('view', for_, name, request_type, IView), callable = handler, args = ('registerAdapter', - view, (for_, IRequest), IView, name, + view, (for_, request_type), IView, name, _context.info), ) @@ -115,9 +76,11 @@ class IViewDirective(Interface): required=False, ) - template = Path( - title=u"The name of a template that implements the view.", - description=u"""Refers to a file containing a z3c.pt page template""", + request_type = GlobalObject( + title=u"""The request type interface for the view""", + description=(u"The view will be called if the interface represented by " + u"'request_type' is implemented by the request. The " + u"default request type is repoze.bfg.interfaces.IRequest"), required=False ) -- cgit v1.2.3