Skip to content

Commit

Permalink
Added the resourceViewlet directive
Browse files Browse the repository at this point in the history
  • Loading branch information
Ch4ni committed May 30, 2007
0 parents commit ef2befb
Show file tree
Hide file tree
Showing 3 changed files with 346 additions and 0 deletions.
25 changes: 25 additions & 0 deletions meta.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<configure
xmlns:meta="http://namespaces.zope.org/meta">

<meta:directives namespace="http://namespaces.zope.org/browser">

<meta:directive
name="viewlet"
schema=".metadirectives.IViewletDirective"
handler=".metaconfigure.viewletDirective"
/>

<meta:directive
name="viewletManager"
schema=".metadirectives.IViewletManagerDirective"
handler=".metaconfigure.viewletManagerDirective"
/>

<meta:directive
name="resourceViewletManager"
schema=".metadirectives.IResourceViewletDirective"
handler=".metaconfigure.resourceViewletDirective"
/>
</meta:directives>

</configure>
197 changes: 197 additions & 0 deletions metaconfigure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
##############################################################################
#
# Copyright (c) 2004 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Viewlet metadconfigure
$Id$
"""
__docformat__ = 'restructuredtext'

import os

from zope.security import checker
from zope.configuration.exceptions import ConfigurationError
from zope.interface import Interface, classImplements
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
from zope.publisher.interfaces.browser import IBrowserView
from zope.component import zcml
from zope.viewlet import viewlet, manager, interfaces

from zope.app.publisher.browser import viewmeta

def viewletManagerDirective(
_context, name, permission,
for_=Interface, layer=IDefaultBrowserLayer, view=IBrowserView,
provides=interfaces.IViewletManager, class_=None, template=None,
allowed_interface=None, allowed_attributes=None):

# A list of attributes available under the provided permission
required = {}

# Get the permission; mainly to correctly handle CheckerPublic.
permission = viewmeta._handle_permission(_context, permission)

# If class is not given we use the basic viewlet manager.
if class_ is None:
class_ = manager.ViewletManagerBase

# Make sure that the template exists and that all low-level API methods
# have the right permission.
if template:
template = os.path.abspath(str(_context.path(template)))
if not os.path.isfile(template):
raise ConfigurationError("No such file", template)
required['__getitem__'] = permission

# Create a new class based on the template and class.
new_class = manager.ViewletManager(
name, provides, template=template, bases=(class_, ))
else:
# Create a new class based on the class.
new_class = manager.ViewletManager(name, provides, bases=(class_, ))

# Register some generic attributes with the security dictionary
for attr_name in ('browserDefault', 'update', 'render', 'publishTraverse'):
required[attr_name] = permission

# Register the ``provides`` interface and register fields in the security
# dictionary
viewmeta._handle_allowed_interface(
_context, (provides,), permission, required)

# Register the allowed interface and add the field's security entries
viewmeta._handle_allowed_interface(
_context, allowed_interface, permission, required)

# Register single allowed attributes in the security dictionary
viewmeta._handle_allowed_attributes(
_context, allowed_attributes, permission, required)

# Register interfaces
viewmeta._handle_for(_context, for_)
zcml.interface(_context, view)

# Create a checker for the viewlet manager
checker.defineChecker(new_class, checker.Checker(required))

# register a viewlet manager
_context.action(
discriminator = ('viewletManager', for_, layer, view, name),
callable = zcml.handler,
args = ('registerAdapter',
new_class, (for_, layer, view), provides, name,
_context.info),)


def viewletDirective(
_context, name, permission,
for_=Interface, layer=IDefaultBrowserLayer, view=IBrowserView,
manager=interfaces.IViewletManager, class_=None, template=None,
attribute='render', allowed_interface=None, allowed_attributes=None,
**kwargs):

# Security map dictionary
required = {}

# Get the permission; mainly to correctly handle CheckerPublic.
permission = viewmeta._handle_permission(_context, permission)

# Either the class or template must be specified.
if not (class_ or template):
raise ConfigurationError("Must specify a class or template")

# Make sure that all the non-default attribute specifications are correct.
if attribute != 'render':
if template:
raise ConfigurationError(
"Attribute and template cannot be used together.")

# Note: The previous logic forbids this condition to evere occur.
if not class_:
raise ConfigurationError(
"A class must be provided if attribute is used")

# Make sure that the template exists and that all low-level API methods
# have the right permission.
if template:
template = os.path.abspath(str(_context.path(template)))
if not os.path.isfile(template):
raise ConfigurationError("No such file", template)
required['__getitem__'] = permission

# Make sure the has the right form, if specified.
if class_:
if attribute != 'render':
if not hasattr(class_, attribute):
raise ConfigurationError(
"The provided class doesn't have the specified attribute "
)
if template:
# Create a new class for the viewlet template and class.
new_class = viewlet.SimpleViewletClass(
template, bases=(class_, ), attributes=kwargs, name=name)
else:
if not hasattr(class_, 'browserDefault'):
cdict = {'browserDefault':
lambda self, request: (getattr(self, attribute), ())}
else:
cdict = {}

cdict['__name__'] = name
cdict['__page_attribute__'] = attribute
cdict.update(kwargs)
new_class = type(class_.__name__,
(class_, viewlet.SimpleAttributeViewlet), cdict)

if hasattr(class_, '__implements__'):
classImplements(new_class, IBrowserPublisher)

else:
# Create a new class for the viewlet template alone.
new_class = viewlet.SimpleViewletClass(template, name=name,
attributes=kwargs)

# Set up permission mapping for various accessible attributes
viewmeta._handle_allowed_interface(
_context, allowed_interface, permission, required)
viewmeta._handle_allowed_attributes(
_context, allowed_attributes, permission, required)
viewmeta._handle_allowed_attributes(
_context, kwargs.keys(), permission, required)
viewmeta._handle_allowed_attributes(
_context,
(attribute, 'browserDefault', 'update', 'render', 'publishTraverse'),
permission, required)

# Register the interfaces.
viewmeta._handle_for(_context, for_)
zcml.interface(_context, view)

# Create the security checker for the new class
checker.defineChecker(new_class, checker.Checker(required))

# register viewlet
_context.action(
discriminator = ('viewlet', for_, layer, view, manager, name),
callable = zcml.handler,
args = ('registerAdapter',
new_class, (for_, layer, view, manager), interfaces.IViewlet,
name, _context.info),)

def ResourceViewletDirective(_context, name, permission,
for_=Interface,layer=IDefaultBrowserLayer,view=IBrowserView,
manager=interfaces.IViewletManager,class_=None,template=None,
attribute='render',allowed_interface=None,allowed_attributes=None,
**kwargs):
"""There is no need for a function body here because it will merely call
on all the parent classes (et al) for all required functionality."""
124 changes: 124 additions & 0 deletions metadirectives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
##############################################################################
#
# Copyright (c) 2004 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Viewlet metadirective
$Id$
"""
__docformat__ = 'restructuredtext'

import zope.configuration.fields
import zope.schema
from zope.publisher.interfaces import browser
from zope.i18nmessageid import MessageFactory
_ = MessageFactory('zope')

from zope.app.publisher.browser import metadirectives
from zope.viewlet import interfaces
from zope.app.publisher.browser import IResourceDirective


class IContentProvider(metadirectives.IPagesDirective):
"""A directive to register a simple content provider.
Content providers are registered by their context (`for` attribute), the
request (`layer` attribute) and the view (`view` attribute). They also
must provide a name, so that they can be found using the TALES
``provider`` namespace. Other than that, content providers are just like
any other views.
"""

view = zope.configuration.fields.GlobalObject(
title=_("The view the content provider is registered for."),
description=_("The view can either be an interface or a class. By "
"default the provider is registered for all views, "
"the most common case."),
required=False,
default=browser.IBrowserView)

name = zope.schema.TextLine(
title=_("The name of the content provider."),
description=_("The name of the content provider is used in the TALES "
"``provider`` namespace to look up the content "
"provider."),
required=True)


class ITemplatedContentProvider(IContentProvider):
"""A directive for registering a content provider that uses a page
template to provide its content."""

template = zope.configuration.fields.Path(
title=_("Content-generating template."),
description=_("Refers to a file containing a page template (should "
"end in extension ``.pt`` or ``.html``)."),
required=False)


class IViewletManagerDirective(ITemplatedContentProvider):
"""A directive to register a new viewlet manager.
Viewlet manager registrations are very similar to content provider
registrations, since they are just a simple extension of content
providers. However, viewlet managers commonly have a specific provided
interface, which is used to discriminate the viewlets they are providing.
"""

provides = zope.configuration.fields.GlobalInterface(
title=_("The interface this viewlet manager provides."),
description=_("A viewlet manager can provide an interface, which "
"is used to lookup its contained viewlets."),
required=False,
default=interfaces.IViewletManager,
)


class IViewletDirective(ITemplatedContentProvider):
"""A directive to register a new viewlet.
Viewlets are content providers that can only be displayed inside a viewlet
manager. Thus they are additionally discriminated by the manager. Viewlets
can rely on the specified viewlet manager interface to provide their
content.
The viewlet directive also supports an undefined set of keyword arguments
that are set as attributes on the viewlet after creation. Those attributes
can then be used to implement sorting and filtering, for example.
"""

manager = zope.configuration.fields.GlobalObject(
title=_("view"),
description=u"The interface of the view this viewlet is for. "
u"(default IBrowserView)""",
required=False,
default=interfaces.IViewletManager)

class IResourceViewletDirective(IViewletDirective,IResourceDirective):
"""A directive to register a new viewlet and it's resource.
By registering both a viewlet *and* it's directive we save typing, and
complexity, by redicing the calls to the viewlet directive in the
configuration of a viewlet. This reduces the calls by half.
Author: Aaron Cripps, MUN Housing. cripps@cs.mun.ca
"""

manager = zope.configuration.fields.GlobalObject(
title=_("view"),
description=u"The interface of the view this viewlet is for. "
u"(default BrowserView)""",
required=False,
default=interfaces.IViewletManager)


# Arbitrary keys and values are allowed to be passed to the viewlet.
IViewletDirective.setTaggedValue('keyword_arguments', True)

0 comments on commit ef2befb

Please sign in to comment.