Function or class decorator which allows Python code to make view registrations instead of using ZCML for the same purpose.
E.g. in the module views.py:
from models import IMyModel
from repoze.bfg.interfaces import IRequest
@bfg_view(name='my_view', request_type=IRequest, for_=IMyModel,
permission='read', route_name='site1'))
def my_view(context, request):
return render_template_to_response('templates/my.pt')
Equates to the ZCML:
<view
for='.models.IMyModel'
view='.views.my_view'
name='my_view'
permission='read'
route_name='site1'
/>
If name is not supplied, the empty string is used (implying the default view).
If request_type is not supplied, the interface repoze.bfg.interfaces.IRequest is used.
If for_ is not supplied, the interface zope.interface.Interface (implying all interfaces) is used.
If permission is not supplied, no permission is registered for this view (it’s accessible by any caller).
If route_name is not supplied, the view declaration is considered to be made against a URL that doesn’t match any defined route. The use of a route_name is an advanced feature, useful only if you’re using url dispatch.
Any individual or all parameters can be omitted. The simplest bfg_view declaration then becomes:
@bfg_view()
def my_view(...):
...
Such a registration implies that the view name will be my_view, registered for models with the zope.interface.Interface interface, using no permission, registered against requests which implement the default IRequest interface when no urldispatch route matches.
The bfg_view decorator can also be used as a class decorator in Python 2.6 and better (Python 2.5 and below do not support class decorators):
from webob import Response
from repoze.bfg.view import bfg_view
@bfg_view()
class MyView(object):
def __init__(self, context, request):
self.context = context
self.request = request
def __call__(self):
return Response('hello from %s!' % self.context)
In Python 2.5 and below, the bfg_view decorator can still be used against a class, although not in decorator form:
from webob import Response
from repoze.bfg.view import bfg_view
class MyView(object):
def __init__(self, context, request):
self.context = context
self.request = request
def __call__(self):
return Response('hello from %s!' % self.context)
MyView = bfg_view()(MyView)
Note
When a view is a class, the calling semantics are different than when it is a function or another non-class callable. See Defining a View as a Class for more information.
Warning
Using a class as a view is a new feature in 0.8.1+.
To make use of any bfg_view declaration, you must insert the following boilerplate into your application registry’s ZCML:
<scan package="."/>
An instance of this class is a callable which can act as a BFG view; this view will serve static files from a directory on disk based on the root_dir you provide to its constructor.
The directory may contain subdirectories (recursively); the static view implementation will descend into these directories as necessary based on the components of the URL in order to resolve a path into a response.
You may pass an absolute or relative filesystem path to the directory containing static files directory to the constructor as the root_dir argument.
If the path is relative, and the package argument is None, it will be considered relative to the directory in which the Python file which calls static resides. If the package name argument is provided, and a relative root_dir is provided, the root_dir will be considered relative to the Python package specified by package_name (a dotted path to a Python package).
cache_max_age influences the Expires and Max-Age response headers returned by the view (default is 3600 seconds or five minutes). level influences how relative directories are resolved (the number of hops in the call stack), not used very often.
Note
If the root_dir is relative to a package, the BFG resource ZCML directive can be used to override resources within the named root_dir package-relative directory. However, if the root_dir is absolute, the resource directive will not be able to override the resources it contains.