From 2eca00a3dbbcdb2bd767840d0cebde5d9e172340 Mon Sep 17 00:00:00 2001 From: Malthe Borch Date: Thu, 4 Dec 2008 21:49:51 +0000 Subject: View component registration is now able to use the 'adaptation annotation' scheme (see zope.component.adaptedBy); the change-log entry has detailed information. --- CHANGES.txt | 7 +++++++ repoze/bfg/zcml.py | 26 ++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 5426da283..83126b062 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,12 @@ Next release + - The component registration handler for views (functions or class + instances) now observes component adaptation annotations (see + ``zope.component.adaptedBy``) and uses them before the fallback + values for ``for_`` and ``request_type``. This change does not + affect existing code insomuch as the code does not rely on these + defaults when an annotation is set on the view (unlikely). + - Strip all slashes from end and beginning of path in clean_path within traversal machinery. diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py index 960be33bc..aba5847d2 100644 --- a/repoze/bfg/zcml.py +++ b/repoze/bfg/zcml.py @@ -2,10 +2,12 @@ import cPickle import os from os.path import realpath import time +import types from zope.configuration import xmlconfig from zope.component import getSiteManager +from zope.component import adaptedBy import zope.configuration.config from zope.component.interface import provideInterface @@ -36,7 +38,7 @@ def view(_context, for_=None, view=None, name="", - request_type=IRequest, + request_type=None, cacheable=True, ): @@ -50,6 +52,26 @@ def view(_context, args = ('', for_) ) + # views may be either functions or class instances + if isinstance(view, types.FunctionType): + adapted_by = adaptedBy(view) + else: + adapted_by = adaptedBy(type(view)) + + if adapted_by is not None: + try: + if for_ is None: + for_, _ = adapted_by + if request_type is None: + _, request_type = adapted_by + except ValueError: + # the component adaptation annotation does not conform to + # the view specification; we ignore it. + pass + + if request_type is None: + request_type = IRequest + if permission: pfactory = ViewPermissionFactory(permission) _context.action( @@ -150,7 +172,7 @@ def zcml_configure(name, package, load=cPickle.load): return file_configure(name, package) mtime = os.stat(realpath(file)).st_mtime - + if mtime >= ptime: return file_configure(name, package) -- cgit v1.2.3