[Zope3-checkins] SVN: Zope3/trunk/ Added zope.app.pagelet package
from 'svn.tiks.org' repository
Roger Ineichen
roger at projekt01.ch
Mon Nov 8 08:46:19 EST 2004
Log message for revision 28392:
Added zope.app.pagelet package from 'svn.tiks.org' repository
Changed:
_U Zope3/trunk/
A Zope3/trunk/package-includes/pagelet-configure.zcml
A Zope3/trunk/package-includes/pagelet-meta.zcml
A Zope3/trunk/src/zope/app/pagelet/
A Zope3/trunk/src/zope/app/pagelet/DEPENDENCIES.cfg
A Zope3/trunk/src/zope/app/pagelet/README.txt
A Zope3/trunk/src/zope/app/pagelet/SETUP.cfg
A Zope3/trunk/src/zope/app/pagelet/__init__.py
A Zope3/trunk/src/zope/app/pagelet/collector.py
A Zope3/trunk/src/zope/app/pagelet/configure.zcml
A Zope3/trunk/src/zope/app/pagelet/exceptions.py
A Zope3/trunk/src/zope/app/pagelet/interfaces.py
A Zope3/trunk/src/zope/app/pagelet/meta.zcml
A Zope3/trunk/src/zope/app/pagelet/metaconfigure.py
A Zope3/trunk/src/zope/app/pagelet/metadirectives.py
A Zope3/trunk/src/zope/app/pagelet/tales.py
A Zope3/trunk/src/zope/app/pagelet/tests/
A Zope3/trunk/src/zope/app/pagelet/tests/__init__.py
A Zope3/trunk/src/zope/app/pagelet/tests/pagelets.zcml
A Zope3/trunk/src/zope/app/pagelet/tests/test_directives.py
A Zope3/trunk/src/zope/app/pagelet/tests/test_pagelet.py
A Zope3/trunk/src/zope/app/pagelet/tests/testfiles/
A Zope3/trunk/src/zope/app/pagelet/tests/testfiles/__init__.py
A Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagedata.pt
A Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelet.pt
A Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelet_iface_error.pt
A Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelets.pt
A Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelets_iface_error.pt
A Zope3/trunk/src/zope/app/pagelet/tests/testfiles/test_pagelet.pt
-=-
Property changes on: Zope3/trunk
___________________________________________________________________
Name: svn:ignore
- overrides_ftesting.zcml
products_ftesting.zcml
principals.zcml
products.zcml
overrides.zcml
zope.conf
Data.fs
Data.fs.*
build
dist
coverage
z3.log
access.log
test-db
zdsock
+ overrides_ftesting.zcml
products_ftesting.zcml
principals.zcml
products.zcml
overrides.zcml
zope.conf
Data.fs
Data.fs.*
build
dist
coverage
z3.log
access.log
test-db
zdsock
test.bat
setup.bat
start.bat
Added: Zope3/trunk/package-includes/pagelet-configure.zcml
===================================================================
--- Zope3/trunk/package-includes/pagelet-configure.zcml 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/package-includes/pagelet-configure.zcml 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1 @@
+<include package="zope.app.pagelet" />
\ No newline at end of file
Property changes on: Zope3/trunk/package-includes/pagelet-configure.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/package-includes/pagelet-meta.zcml
===================================================================
--- Zope3/trunk/package-includes/pagelet-meta.zcml 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/package-includes/pagelet-meta.zcml 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1 @@
+<include package="zope.app.pagelet" file="meta.zcml" />
Property changes on: Zope3/trunk/package-includes/pagelet-meta.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/DEPENDENCIES.cfg
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/DEPENDENCIES.cfg 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/DEPENDENCIES.cfg 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,8 @@
+zope.app
+zope.component
+zope.configuration
+zope.interface
+zope.publisher
+zope.schema
+zope.security
+zope.tales
Property changes on: Zope3/trunk/src/zope/app/pagelet/DEPENDENCIES.cfg
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/README.txt
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/README.txt 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/README.txt 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,230 @@
+========
+Pagelets
+========
+
+Definition
+----------
+ _________
+ | |
+ | Context |
+ |_________|
+ ^
+ |
+ |*
+ ____|____
+ | |
+ | View |
+ |_________|
+ |
+ |
+ |* a view is composed of slots
+ ____v____
+ | |
+ | ViewSlot |
+ |__________|
+ |
+ |
+ |* a slot contains a list of viewlets
+ ____v____ _____________
+ | | | |
+ | Viewlet |------| ViewletType |
+ |_________|* |_____________|
+ ^
+ /_\
+ ____________|____________
+ | |
+ ____|____ ____|____
+ | | | |
+ | Pagelet | | Portlet |
+ |_________| |_________|
+
+A view instance is assoziated with one context. A context may be viewed
+with none ore more views.
+
+The distinction between Pagelet and Portlet is still fuzzy. Pagelets and
+portlets are designed to be parts of a view. In that meaning pagelets
+and portles are equal. They are specialized viewlets. A view is composed
+of viewlets which are related to a specific slot of that view.
+
+But what is the difference between them? The reconstructed interpretation
+of zope's common sense is the following::
+
+ A pagelet of a view displays the underlying context.
+ A portlet of a view displays data from different contexts.
+
+ Examples: The metadata pagelet displays the metadata of
+ the underlying context. The metadata portlet displays for
+ example the metadata of all its children.
+
+ A calendar pagelet displays the calendar data of a content
+ object that implements an own calendar. A calendar portlet
+ displays global calendar data on different objects that may
+ come from an utility for example.
+
+
+In view of the component architecture this differentiation does not make
+sense anymore, because the adaption mechanism hides such criterias
+(implementation decisions and details) transparently inside an adapter.
+
+That is the reason, why we try to provide a new definition::
+
+ A pagelet of a view operates of the underlying context.
+ A portlet of a view operates of the underlying or a
+ different context.
+
+ Examples: The metadata pagelet displays the metadata.
+ Therefore it adapts the underlying context to IMetadata.
+ The metadata portlet displays metadata too, but it adapts
+ a context indepentently to the underlying view context.
+
+ Hence serveral pagelets of the same type composed inside
+ one view must display always the similar content where
+ several portlets of the same type composed inside a view
+ can present different contents.
+
+
+Usage
+-----
+This pagelet implementation suports pagelets and portlets in respect of
+the first definition, but it only suports pagelets in respect of the
+second definition.
+
+In the following text we us pagelet in the sense of the latter.
+
+Pagelets are responsible for a piece of content in a view. They can be
+used to render additionaly provided information into a pagetemplate.
+
+Pagelets are small, view-like components that can registered to
+skin layers(request)-, contenttype-, view- and slot-interfaces.
+
+Inside a pagetemplate of a view, the registered pagelets can be called by
+the tal:pagelets command. The return value is a list of macros where each
+macro correspondents to a registered pagelet. This macros can be used to
+invoke the pagelets::
+
+ <div class="row">
+ <tal:repeat="pagelets pagelets:zope.app.demo.pagelet.interfaces.IDemoSlot">
+ <tal:block metal:use-macro="pagelets" />
+ </tal:repeat>
+ </div>
+
+Such a macro may process static content or invoke the context- or
+view-namespace for dynamic contents::
+
+ <div class="row">
+ <h4>content:</h4>
+ <span tal:content="view/title">title</span>
+ </div>
+
+The latter is not recommend, because it glues view and pagelet together.
+That means a pagelet depends on a specific view- or context implementation.
+
+In respect of modularization we provide an additional tal:pagedata
+command. This command allows to lookup adapters providing an interface
+derived form IPageletData::
+
+ <div class="row">
+ <tal:define="data pagedata:zope.app.demo.pagelet.interfaces.IDemoPageData">
+ <h4>content:</h4>
+ <span tal:content="data/title">title</span>
+ </tal:define>
+ </div>
+
+This is a restricted adapter invocation. It should prevent uncontrolled
+adapter invocation inside pagetemplates, because that would glue view
+layer and programming layer in not appreciable manner.
+
+
+Let's show how to use pagelets
+==============================
+
+Imports:
+
+ >>> import zope.component
+ >>> from zope.app import zapi
+ >>> from zope.interface import Interface
+ >>> from zope.security.checker import defineChecker
+ >>> from zope.publisher.browser import TestRequest
+ >>> from zope.publisher.interfaces.browser import IBrowserRequest
+ >>> from zope.component.interfaces import IView
+ >>> from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
+ >>> from zope.app.publisher.browser import BrowserView
+ >>> from zope.app.pagelet.interfaces import IPagelet
+ >>> from zope.app.pagelet.interfaces import IPageletSlot
+ >>> from zope.app.pagelet.interfaces import IMacrosCollector
+ >>> from zope.app.pagelet.tales import TALESPageletsExpression
+ >>> from zope.app.pagelet.collector import MacrosCollector
+ >>> from zope.app.pagelet.tests import TestPagelet
+ >>> from zope.app.pagelet.tests import TestContext
+ >>> from zope.app.pagelet.tests import testChecker
+
+Setup:
+
+ >>> adaptersrv = zope.component.getService('Adapters')
+
+Register slot interface:
+
+ >>> from zope.app.component.interface import provideInterface
+ >>> provideInterface('', IPageletSlot, None)
+
+Register TALES pagelet expression:
+
+ >>> from zope.app.pagetemplate.metaconfigure import registerType
+ >>> registerType('pagelets', TALESPageletsExpression)
+
+Define a pagelet in a ZCML directive pagelet like:
+
+<zope:pagelet
+ name="demopagelet"
+ layer="zope.publisher.interfaces.browser.IBrowserRequest"
+ slot="zope.app.pagelet.interfaces.IPageletSlot"
+ template="path_to/pagelet.pt"
+ for="*"
+ permission="zope.View"
+ weight="0"
+ />
+
+ >>> name = 'testpagelet'
+ >>> layer = IBrowserRequest
+ >>> slot = IPageletSlot
+ >>> template = u'src/zope/app/pagelet/tests/testfiles/test_pagelet.pt'
+ >>> for_ = Interface
+ >>> permission = 'zope.View'
+ >>> weight = 0
+
+Register the pagelet:
+
+ >>> pagelet_factory = TestPagelet
+ >>> defineChecker(pagelet_factory, testChecker)
+ >>> adaptersrv.register(
+ ... (Interface, IBrowserRequest, IView, IPageletSlot)
+ ... , IPagelet, name, pagelet_factory)
+
+Register pagelet collector as a adapter:
+
+ >>> collector_factory = MacrosCollector
+ >>> adaptersrv.register(
+ ... (Interface, IBrowserRequest, IView, IPageletSlot)
+ ... , IMacrosCollector, '', collector_factory)
+
+Setup a simply browser view with a 'index_pagelets.pt' template:
+
+ >>> ob = TestContext()
+ >>> request = TestRequest()
+ >>> view = BrowserView(ob, request)
+
+Setup a view page template called 'index':
+
+ >>> from zope.app.pagelet.tests import testfiles
+ >>> index = ViewPageTemplateFile('index_pagelets.pt',
+ ... 'src/zope/app/pagelet/tests/testfiles')
+
+Call the 'index' (view) on the browser view instance:
+
+ >>> html = index(view, request)
+
+Test if the pagelet content is in the html output:
+
+ >>> import string
+ >>> string.count(html, 'testpagelet macro content')
+ 1
Property changes on: Zope3/trunk/src/zope/app/pagelet/README.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/SETUP.cfg
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/SETUP.cfg 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/SETUP.cfg 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,5 @@
+# Tell zpkg how to install the ZCML slugs.
+
+<data-files zopeskel/etc/package-includes>
+ pagelet-*.zcml
+</data-files>
Property changes on: Zope3/trunk/src/zope/app/pagelet/SETUP.cfg
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/__init__.py 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/__init__.py 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,17 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Pagelet exceptions
+
+$Id:$
+"""
Property changes on: Zope3/trunk/src/zope/app/pagelet/__init__.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/collector.py
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/collector.py 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/collector.py 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,190 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Pagelet collectors
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.interface import implements
+from zope.interface import directlyProvides
+
+from zope.app import zapi
+
+from zope.app.pagelet.interfaces import IPagelet
+from zope.app.pagelet.interfaces import IMacrosCollector
+from zope.app.pagelet.interfaces import IMacroCollector
+
+
+
+class MacrosCollector(object):
+ """Replaceable sample implementation of IMacrosCollector.
+
+ Collects pagelets from the adapter service.
+ Pagelet adapters are registred on context, request, view and slot
+ interfaces. Use your own IMacrosCollector implementation for
+ to support a layout manager.
+
+ Imports:
+
+ >>> import zope.component
+ >>> from zope.interface import Interface
+ >>> from zope.publisher.browser import TestRequest
+ >>> from zope.publisher.interfaces.browser import IBrowserRequest
+ >>> from zope.component.interfaces import IView
+ >>> from zope.app.publisher.browser import BrowserView
+ >>> from zope.app.pagelet.interfaces import IPagelet
+ >>> from zope.app.pagelet.interfaces import IPageletSlot
+ >>> from zope.app.pagelet.tests import TestPagelet
+ >>> from zope.app.pagelet.tests import TestContext
+ >>> from zope.app.pagelet.tests import TestSlot
+
+ Setup pagelet:
+
+ >>> ob = TestContext()
+ >>> name = 'testpagelet'
+ >>> factory = TestPagelet
+
+ Register the pagelet class as a factory on the adapter service:
+
+ >>> from zope.app.tests import placelesssetup, ztapi
+ >>> placelesssetup.setUp()
+ >>> adaptersrv = zope.component.getService('Adapters')
+ >>> adaptersrv.register(
+ ... (Interface, IBrowserRequest, IView, IPageletSlot)
+ ... , IPagelet, name, factory)
+
+ Setup macros collector:
+
+ >>> request = TestRequest()
+ >>> view = BrowserView(ob, request)
+ >>> slot = TestSlot()
+ >>> collector = MacrosCollector(ob, request, view, slot)
+
+ Get macros form the collector
+
+ >>> macros = collector.macros()
+
+ Test if we have the string form the test_pagelet in the macro:
+
+ >>> rawtextOffset = macros[0][5][1][0]
+ >>> rawtextOffset
+ 'testpagelet macro content</div>'
+
+ >>> placelesssetup.tearDown()
+
+ """
+
+ implements(IMacrosCollector)
+
+ def __init__ (self, context, request, view, slot):
+ self.context = context
+ self.request = request
+ self.view = view
+ self.slot = slot
+
+ def macros(self):
+ macros = []
+
+ # collect pagelets
+ objects = self.context, self.request, self.view, self.slot
+ adapters = zapi.getAdapters(objects, IPagelet)
+ adapters.sort(lambda x, y: x[1].weight - y[1].weight)
+
+ for name, pagelet in adapters:
+ # append pagelet macros
+ macros.append(pagelet[name])
+
+ return macros
+
+
+
+class MacroCollector(object):
+ """Replaceable sample implementation of IMacroCollector.
+
+ Collect a single pagelet from the adapter service and returns
+ a macro by name.
+ Pagelet adapters are registred on context, request, view and slot
+ interfaces. Use your own IMacroCollector implementation for
+ to support a layout manager which can return a macro dependent
+ on additional rules.
+
+
+ Imports:
+
+ >>> import zope.component
+ >>> from zope.interface import Interface
+ >>> from zope.publisher.browser import TestRequest
+ >>> from zope.publisher.interfaces.browser import IBrowserRequest
+ >>> from zope.component.interfaces import IView
+ >>> from zope.app.publisher.browser import BrowserView
+ >>> from zope.app.pagelet.interfaces import IPagelet
+ >>> from zope.app.pagelet.interfaces import IPageletSlot
+ >>> from zope.app.pagelet.tests import TestPagelet
+ >>> from zope.app.pagelet.tests import TestContext
+ >>> from zope.app.pagelet.tests import TestSlot
+
+ Setup pagelet:
+
+ >>> ob = TestContext()
+ >>> name = 'testpagelet'
+ >>> factory = TestPagelet
+
+ Register the pagelet class as a factory on the adapter service:
+
+ >>> from zope.app.tests import placelesssetup, ztapi
+ >>> placelesssetup.setUp()
+ >>> adaptersrv = zope.component.getService('Adapters')
+ >>> adaptersrv.register(
+ ... (Interface, IBrowserRequest, IView, IPageletSlot)
+ ... , IPagelet, name, factory)
+
+ Setup macros collector:
+
+ >>> request = TestRequest()
+ >>> view = BrowserView(ob, request)
+ >>> slot = TestSlot()
+ >>> collector = MacroCollector(ob, request, view, slot)
+
+ Get the macro form the collector
+
+ >>> macro = collector.__getitem__('testpagelet')
+
+ Test if we have the string form the test_pagelet.pt file in the macro:
+
+ >>> rawtextOffset = macro[5][1][0]
+ >>> rawtextOffset
+ 'testpagelet macro content</div>'
+
+ >>> placelesssetup.tearDown()
+
+ """
+
+ implements(IMacroCollector)
+
+ def __init__ (self, context, request, view, slot):
+ self.context = context
+ self.request = request
+ self.view = view
+ self.slot = slot
+
+ def __getitem__(self, key):
+ macros = []
+
+ # collect a single pagelet which is a pagelet
+ objects = self.context, self.request, self.view, self.slot
+ adapter = zapi.getMultiAdapter(objects, IPagelet, key)
+
+ return adapter[key]
+
Property changes on: Zope3/trunk/src/zope/app/pagelet/collector.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/configure.zcml 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/configure.zcml 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,49 @@
+<configure
+ xmlns="http://namespaces.zope.org/zope"
+ xmlns:tales="http://namespaces.zope.org/tales"
+ i18n_domain="zope"
+ >
+
+ <!-- tal:pagelets expression -->
+ <interface interface=".interfaces.ITALESPageletsExpression" />
+ <interface interface=".interfaces.IMacrosCollector" />
+
+ <adapter
+ for="zope.interface.Interface
+ zope.publisher.interfaces.browser.IBrowserRequest
+ zope.component.interfaces.IView
+ .interfaces.IPageletSlot"
+ factory=".collector.MacrosCollector"
+ provides=".interfaces.IMacrosCollector" />
+
+ <tales:expressiontype
+ name="pagelets"
+ handler=".tales.TALESPageletsExpression"
+ />
+
+ <!-- tal:pagelet expression -->
+ <interface interface=".interfaces.ITALESPageletExpression" />
+ <interface interface=".interfaces.IMacroCollector" />
+
+ <adapter
+ for="zope.interface.Interface
+ zope.publisher.interfaces.browser.IBrowserRequest
+ zope.component.interfaces.IView
+ .interfaces.IPageletSlot"
+ factory=".collector.MacroCollector"
+ provides=".interfaces.IMacroCollector" />
+
+ <tales:expressiontype
+ name="pagelet"
+ handler=".tales.TALESPageletExpression"
+ />
+
+ <!-- tal:pagedata expression -->
+ <interface interface=".interfaces.ITALESPageDataExpression" />
+
+ <tales:expressiontype
+ name="pagedata"
+ handler=".tales.TALESPageDataExpression"
+ />
+
+</configure>
\ No newline at end of file
Property changes on: Zope3/trunk/src/zope/app/pagelet/configure.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/exceptions.py
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/exceptions.py 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/exceptions.py 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,38 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Pagelet exceptions
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.component import ComponentLookupError
+
+from zope.app.i18n import ZopeMessageIDFactory as _
+
+
+
+
+class PageletSlotInterfaceLookupError(ComponentLookupError):
+ """IPageletSlot slot interface not found."""
+
+PageletError_slot_interface_not_found = _(
+ u'Pagelet slot interface not found.')
+
+
+class PageletSlotInterfaceNotProvidedException(Exception):
+ """IPageletSlot interface not provided."""
+
+PageletError_slot_interface_not_provided = _(
+ u'IPageletSlot interface not provided.')
Property changes on: Zope3/trunk/src/zope/app/pagelet/exceptions.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/interfaces.py 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/interfaces.py 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,188 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Pagelet interfaces
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.tales.interfaces import ITALESExpression
+
+from zope.interface import Interface
+from zope.interface import Attribute
+from zope.schema import Int
+
+from zope.app.i18n import ZopeMessageIDFactory as _
+
+
+
+class IPageletSlot(Interface):
+ """Marker interface for pagelet slots.
+
+ The pagelet slot is used as a part ot the key for to register and
+ collect pagelets.
+ """
+
+
+
+class IPagelet(Interface):
+ """Interface for custom pagelet adapters.
+
+ Pagelets can be used in a page template as a piece of content
+ rendered with it's own python view class. Yes with pagelets
+ you can use more then one views in a pageltemplate. This
+ let's pagelets act as portlets. The pagelet view can support
+ content independent information where you can access in every
+ page template on which the pagelet is registred.
+
+ The meta directive set the 'weight' attribute to the class attribute
+ '_weight'. If you whould like to use the settings from the meta
+ directive point the attribute 'weight' to this default attribute.
+
+ If you use a 'template', the meta directive sets the 'template' to
+ the class attribute '_template'.
+ """
+
+ weight = Int(
+ title=_(u'weight'),
+ description=_(u"""
+ Key for sorting pagelets if the pagelet collector is supporting
+ this sort mechanism."""),
+ required=False,
+ default=0)
+
+ def __getitem__(name):
+ """Returns the macro code of the template by the given name."""
+
+
+
+class IPageData(Interface):
+ """Base interface for custom page data adapters."""
+
+
+
+
+class IMacrosCollector(Interface):
+ """Lookup pagelets from the TALES directive 'pagelets:'.
+
+ A adpater providing this interface is called in:
+ pagelet.tales.TALESPageletsExpression via the TALES expression
+ called tal:pagelets.
+
+ If you like to use a layout manager for managing pagelets, implement
+ your own pagelet collector which calls a layout manager.
+
+ Remember: you can register your own pagelet collector on
+ layers, because there is a request in the adapter registration
+ tuple. (Request provides the layer interface)
+ """
+
+ def macros():
+ """Returns macros related to the context, request, view and slot.
+
+ The pagelets are registred as adapters on a tuple like:
+
+ (context, request, view, slot)
+
+ where the attributes are:
+
+ context -- the content object
+ request -- the browser request providing a layer interface
+ view -- the context view, normaly a browser page or view
+ slot -- a slot wrapper instance providing the slot interface
+ """
+
+
+
+class IMacroCollector(Interface):
+ """Lookup a single pagelet from the TALES directive 'pagelet:'
+
+ by the given interface.
+
+ A adpater providing this interface is called in:
+ pagelet.tales.TALESPageletsExpression via the TALES expression
+ called tal:pagelet.
+
+ """
+
+ def __getitem__(key):
+ """Returns a single pagelet macro registred by the given name.
+
+ The pagelets are registred as adapters on a tuple like:
+
+ (context, request, view, slot)
+
+ where the attributes are:
+
+ context -- the content object
+ request -- the browser request providing a layer interface
+ view -- the context view, normaly a browser page or view
+ slot -- a slot wrapper instance providing the slot interface
+ """
+
+
+
+class ITALESPageletsExpression(ITALESExpression):
+ """Tal namespace for getting a list of macros form a IMacrosCollector.
+
+ For to call pagelets in a view use the the following syntax in
+ a page template:
+ <metal:block
+ tal:repeat="pagelet pagelets:zope.app.demo.pagelet.interfaces.IDemoSlot">
+ <tal:block metal:use-macro="pagelet" />
+ </metal:block>
+ where 'zope.app.pagelet.demo.interfaces.IDemoSlot' is a slot interface
+ wich implements pagelet.interfaces.IPageletSlot.
+ """
+
+ pagelets = Attribute("pagelets",
+ _(u"Pagelets registred for context, request, view and slot."))
+
+
+
+class ITALESPageletExpression(ITALESExpression):
+ """Tal namespace for getting a IMacroCollector adapter.
+
+ For to call pagelets in a view use the the following syntax in
+ a page template:
+ <div class="row"
+ tal:define="collector global ...
+ ... pagelets:zope.app.pagelet.demo.interfaces.IDemoSlot">
+ <tal:block metal:use-macro="collector/testpagelet" />
+ </div>
+ where 'zope.app.pagelet.demo.interfaces.IDemoSlot' is a slot interface wich
+ implements pagelet.interfaces.IPageletSlot.
+ """
+
+ pagelet = Attribute("pagelet",
+ _(u"Pagelet registred for context, request, view and slot."))
+
+
+
+class ITALESPageDataExpression(ITALESExpression):
+ """Tal namespace for set the view namespace in MacrosCollector.
+
+ For to call a page data adapter in a page template use the the
+ following syntax:
+ <metal:block
+ tal:define="data pagedata:x.y.interfaces.IDemoPageData" />
+ where 'x.y.interfaces.IDemoPageData' is a portlet interface wich
+ implements pagelet.interfaces.IPageData.
+ """
+
+ pagedata = Attribute("pagedata",
+ _(u"Page data adapter registred for context, request and view."))
+
+ def __call__():
+ """Returns the page data adapter."""
Property changes on: Zope3/trunk/src/zope/app/pagelet/interfaces.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/meta.zcml
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/meta.zcml 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/meta.zcml 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,15 @@
+<configure
+ xmlns="http://namespaces.zope.org/zope"
+ xmlns:meta="http://namespaces.zope.org/meta">
+
+ <meta:directives namespace="http://namespaces.zope.org/browser">
+
+ <meta:directive
+ name="pagelet"
+ schema=".metadirectives.IPageletDirective"
+ handler=".metaconfigure.pagelet"
+ />
+
+ </meta:directives>
+
+</configure>
Property changes on: Zope3/trunk/src/zope/app/pagelet/meta.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/metaconfigure.py
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/metaconfigure.py 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/metaconfigure.py 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,154 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Pagelet metadconfigure
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+import os
+import sys
+
+from zope.interface import Interface
+from zope.interface import implements
+
+from zope.security.checker import defineChecker
+from zope.security.checker import CheckerPublic, Checker
+
+from zope.configuration.exceptions import ConfigurationError
+
+from zope.publisher.interfaces.browser import IDefaultLayer
+
+from zope.app import zapi
+from zope.app.component.metaconfigure import handler
+from zope.app.component.interface import provideInterface
+from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
+from zope.component.interfaces import IView
+
+from zope.app.pagelet.interfaces import IPageletSlot
+from zope.app.pagelet.interfaces import IPagelet
+
+
+
+def checkInterface(iface, baseIface):
+ if not iface.isOrExtends(baseIface):
+ raise ConfigurationError(
+ "slot has to implement pagelet.interfaces.IPageletSlot")
+
+
+def PageletClass(template, weight=0, bases=()):
+
+ frame = sys._getframe(1).f_globals
+
+ class_ = type("PageletClass from %s" % template, bases,
+ {'_template':ViewPageTemplateFile(template, frame)
+ ,'_weight':weight})
+
+ return class_
+
+
+
+class simplepagelet(object):
+ """Pagelet adapter class used in meta directive as a mixin class."""
+
+ implements(IPagelet)
+
+ _weight = 0
+
+ def __init__(self, context, request, view, ignored):
+ self.context = context
+ self.request = request
+ self.view = view
+
+ def __getitem__(self, name):
+ """Get the macro by name."""
+ return self._template.macros[name]
+
+ def _getWeight (self):
+ """The weight of the pagelet."""
+ return self._weight
+
+ weight = property(_getWeight)
+
+
+
+def pagelet(_context, name, slot, permission, for_=Interface,
+ layer=IDefaultLayer, view=IView, weight=0, template=None):
+
+ required = {}
+
+ # set permission checker
+ permission = _handle_permission(_context, permission)
+
+ if not name:
+ raise ConfigurationError("Must specify name.")
+
+ if not slot:
+ raise ConfigurationError("Must specify a slot interface.")
+
+ if not template:
+ raise ConfigurationError("Must specify a template.")
+
+ template = os.path.abspath(str(_context.path(template)))
+ if not os.path.isfile(template):
+ raise ConfigurationError("No such file", template)
+
+ required['__getitem__'] = permission
+
+ new_class = PageletClass(template, weight, bases=(simplepagelet, ))
+
+ # set permissions
+ for n in ('__getitem__', '__call__', 'weight'):
+ required[n] = permission
+
+ #register interface
+ _handle_iface(_context, for_)
+ _handle_iface(_context, view)
+ _handle_iface(_context, slot)
+
+ # check slot interface
+ _handle_check_interface(_context, slot, IPageletSlot)
+
+ # define checker
+ defineChecker(new_class, Checker(required))
+
+ # register pagelet
+ _context.action(
+ discriminator = ('pagelet', for_, layer, view, slot, name),
+ callable = handler,
+ args = (zapi.servicenames.Adapters, 'register',
+ (for_, layer, view, slot), IPagelet, name, new_class
+ , _context.info),)
+
+
+def _handle_iface(_context, iface):
+ if iface is not None:
+ _context.action(
+ discriminator = None,
+ callable = provideInterface,
+ args = ('', iface)
+ )
+
+def _handle_check_interface(_context, iface, baseIface):
+ if iface is not None and baseIface is not None:
+ _context.action(
+ discriminator = None,
+ callable = checkInterface,
+ args = (iface, baseIface)
+ )
+
+def _handle_permission(_context, permission):
+ if permission == 'zope.Public':
+ permission = CheckerPublic
+ return permission
Property changes on: Zope3/trunk/src/zope/app/pagelet/metaconfigure.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/metadirectives.py 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/metadirectives.py 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,87 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Pagelet metadirective
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.interface import Interface
+
+from zope.schema import TextLine
+from zope.schema import Int
+
+from zope.app.security.fields import Permission
+
+from zope.app.publisher.browser.fields import LayerField
+
+from zope.configuration.fields import GlobalObject
+
+
+
+class IPageletDirective(Interface):
+ """TODO: write documentation."""
+
+ name = TextLine(
+ title=u"The name of the pagelet.",
+ description=u"The name of the pagelet has to be unique",
+ required=True
+ )
+
+ slot = GlobalObject(
+ title=u"slot",
+ description=u"The slot interface this pagelet is for.",
+ required=True
+ )
+
+ permission = Permission(
+ title=u"Permission",
+ description=u"The permission needed to use the pagelet.",
+ required=True
+ )
+
+ for_ = GlobalObject(
+ title=u"for",
+ description=u"The interface this pagelet is for (default IInterface)",
+ required=False
+ )
+
+ layer = LayerField(
+ title=u"The layer the pagelet should be found in",
+ description=u"""
+ For information on layers, see the documentation for the skin
+ directive. Defaults to "default".""",
+ required=False
+ )
+
+ view = GlobalObject(
+ title=u"view",
+ description=u"""
+ The interface of the view this pagelet is for. (default IView)""",
+ required=False
+ )
+
+ weight = Int(
+ title=u"weight",
+ description=u"Integer key for sorting pagelets in the same slot.",
+ required=False
+ )
+
+ template = TextLine(
+ title=u"Page template.",
+ description=u"""
+ Refers to a file containing a page template (must end in
+ extension '.pt').""",
+ required=False
+ )
Property changes on: Zope3/trunk/src/zope/app/pagelet/metadirectives.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/tales.py
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/tales.py 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/tales.py 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,417 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Pagelet tales expression registrations
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.interface import implements
+from zope.interface import directlyProvides
+
+from zope.tales.expressions import StringExpr
+
+from zope.app import zapi
+from zope.app.component.interface import queryInterface
+
+from zope.app.pagelet.exceptions import PageletSlotInterfaceLookupError
+from zope.app.pagelet.exceptions import \
+ PageletSlotInterfaceNotProvidedException
+from zope.app.pagelet.exceptions import PageletError_slot_interface_not_found
+from zope.app.pagelet.exceptions import \
+ PageletError_slot_interface_not_provided
+from zope.app.pagelet.interfaces import ITALESPageletsExpression
+from zope.app.pagelet.interfaces import ITALESPageletExpression
+from zope.app.pagelet.interfaces import ITALESPageDataExpression
+from zope.app.pagelet.interfaces import IPageletSlot
+from zope.app.pagelet.interfaces import IPagelet
+from zope.app.pagelet.interfaces import IPageData
+from zope.app.pagelet.interfaces import IMacrosCollector
+from zope.app.pagelet.interfaces import IMacroCollector
+
+
+
+class Wrapper:
+ """Dummy class for to provide a interface."""
+
+
+
+class TALESPageletsExpression(StringExpr):
+ """Collect pagelets via a tal namespace called tal:pagelets.
+
+ Imports:
+
+ >>> import zope.component
+ >>> from zope.interface import Interface
+ >>> from zope.security.checker import defineChecker
+ >>> from zope.publisher.browser import TestRequest
+ >>> from zope.publisher.interfaces.browser import IDefaultLayer
+ >>> from zope.component.interfaces import IView
+ >>> from zope.app.publisher.browser import BrowserView
+ >>> from zope.app.pagelet.interfaces import IPagelet
+ >>> from zope.app.pagelet.interfaces import IPageletSlot
+ >>> from zope.app.pagelet.tests import TestPagelet
+ >>> from zope.app.pagelet.tests import TestContext
+ >>> from zope.app.pagelet.tests import testChecker
+
+ Register pagelet:
+
+ >>> from zope.app.tests import setup, ztapi
+ >>> setup.placefulSetUp()
+ >>> name = 'testpagelet'
+ >>> pagelet_factory = TestPagelet
+ >>> defineChecker(pagelet_factory, testChecker)
+ >>> adaptersrv = zope.component.getService('Adapters')
+ >>> adaptersrv.register(
+ ... (Interface, IDefaultLayer, IView, IPageletSlot)
+ ... , IPagelet, name, pagelet_factory)
+
+ Register slot interface:
+
+ >>> from zope.app.component.interface import provideInterface
+ >>> provideInterface('', IPageletSlot, None)
+
+ Register pagelets collector as a adapter:
+
+ >>> from zope.app.pagelet.collector import MacrosCollector
+ >>> collector_factory = MacrosCollector
+ >>> adaptersrv.register(
+ ... (Interface, IDefaultLayer, IView, IPageletSlot)
+ ... , IMacrosCollector, '', collector_factory)
+
+ Register pagelets expression:
+
+ >>> from zope.app.pagetemplate.metaconfigure import registerType
+ >>> registerType('pagelets', TALESPageletsExpression)
+
+ Setup a simply browser view called 'index' with a 'index_pagelets.pt' template:
+
+ >>> from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
+ >>> ob = TestContext()
+ >>> request = TestRequest()
+ >>> view = BrowserView(ob, request)
+ >>> index = ViewPageTemplateFile('tests/testfiles/index_pagelets.pt')
+
+ Call the 'index' (view) on the browser view instance:
+
+ >>> html = index(view, request)
+
+ Test if the pagelet content is in the html output:
+
+ >>> import string
+ >>> string.count(html, 'testpagelet macro content')
+ 1
+
+ Test PageletSlotInterfaceLookupError:
+
+ >>> no_slot_iface_index = ViewPageTemplateFile(
+ ... 'tests/testfiles/index_pagelets_iface_error.pt')
+
+ >>> try:
+ ... html = no_slot_iface_index(view, request)
+ ... except PageletSlotInterfaceLookupError, e:
+ ... print e
+ (u'Pagelet slot interface not found.', 'zope.interface.Interface')
+
+ Register zope.app.interface as a utility and try again:
+
+ >>> utilities = zapi.getGlobalService(zapi.servicenames.Utilities)
+ >>> provideInterface('', Interface, None)
+ >>> try:
+ ... html = no_slot_iface_index(view, request)
+ ... except PageletSlotInterfaceNotProvidedException, e:
+ ... print e
+ (u'IPageletSlot interface not provided.', 'zope.interface.Interface')
+
+ >>> setup.placefulTearDown()
+
+ """
+
+ implements(ITALESPageletsExpression)
+
+ def __call__(self, econtext):
+ macros = []
+ expr = self._s
+ context = econtext.vars['context']
+ request = econtext.vars['request']
+ view = econtext.vars['view']
+
+ # get interface from key
+ slotiface = queryInterface(expr)
+
+ # check slot
+ if slotiface is None:
+ raise PageletSlotInterfaceLookupError(
+ PageletError_slot_interface_not_found, expr)
+
+ # check interface
+ if not slotiface.isOrExtends(IPageletSlot):
+ raise PageletSlotInterfaceNotProvidedException(
+ PageletError_slot_interface_not_provided, expr)
+
+ slot = Wrapper()
+ directlyProvides(slot, slotiface)
+
+ collector = zapi.getMultiAdapter((context, request, view, slot)
+ , IMacrosCollector)
+
+ macros = collector.macros()
+
+ return macros
+
+
+
+class TALESPageletExpression(StringExpr):
+ """Collects a single pagelet via a tal namespace called tal:pagelet.
+
+ Imports:
+
+ >>> import zope.component
+ >>> from zope.interface import Interface
+ >>> from zope.security.checker import defineChecker
+ >>> from zope.publisher.browser import TestRequest
+ >>> from zope.publisher.interfaces.browser import IDefaultLayer
+ >>> from zope.component.interfaces import IView
+ >>> from zope.app.publisher.browser import BrowserView
+ >>> from zope.app.pagelet.interfaces import IPagelet
+ >>> from zope.app.pagelet.interfaces import IPageletSlot
+ >>> from zope.app.pagelet.tests import TestPagelet
+ >>> from zope.app.pagelet.tests import TestContext
+ >>> from zope.app.pagelet.tests import testChecker
+
+ Register pagelet:
+
+ >>> from zope.app.tests import setup, ztapi
+ >>> setup.placefulSetUp()
+ >>> name = 'testpagelet'
+ >>> pagelet_factory = TestPagelet
+ >>> defineChecker(pagelet_factory, testChecker)
+ >>> adaptersrv = zope.component.getService('Adapters')
+ >>> adaptersrv.register(
+ ... (Interface, IDefaultLayer, IView, IPageletSlot)
+ ... , IPagelet, name, pagelet_factory)
+
+ Register slot interface:
+
+ >>> from zope.app.component.interface import provideInterface
+ >>> provideInterface('', IPageletSlot, None)
+
+ Register pagelets collector as a adapter:
+
+ >>> from zope.app.pagelet.collector import MacroCollector
+ >>> collector_factory = MacroCollector
+ >>> adaptersrv.register(
+ ... (Interface, IDefaultLayer, IView, IPageletSlot)
+ ... , IMacroCollector, '', collector_factory)
+
+ Register pagelets expression:
+
+ >>> from zope.app.pagetemplate.metaconfigure import registerType
+ >>> registerType('pagelet', TALESPageletExpression)
+
+ Setup a simply browser view called 'index' with a 'index_pagelet.pt' template:
+
+ >>> from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
+ >>> ob = TestContext()
+ >>> request = TestRequest()
+ >>> view = BrowserView(ob, request)
+ >>> index = ViewPageTemplateFile('tests/testfiles/index_pagelet.pt')
+
+ Call the 'index' (view) on the browser view instance:
+
+ >>> html = index(view, request)
+
+ Test if the pagelet content is in the html output:
+
+ >>> import string
+ >>> string.count(html, 'testpagelet macro content')
+ 1
+
+ Test PageletSlotInterfaceLookupError:
+
+ >>> no_slot_iface_index = ViewPageTemplateFile(
+ ... 'tests/testfiles/index_pagelet_iface_error.pt')
+
+ >>> try:
+ ... html = no_slot_iface_index(view, request)
+ ... except PageletSlotInterfaceLookupError, e:
+ ... print e
+ (u'Pagelet slot interface not found.', 'zope.interface.Interface')
+
+ Register zope.app.interface as a utility and try again:
+
+ >>> utilities = zapi.getGlobalService(zapi.servicenames.Utilities)
+ >>> provideInterface('', Interface, None)
+ >>> try:
+ ... html = no_slot_iface_index(view, request)
+ ... except PageletSlotInterfaceNotProvidedException, e:
+ ... print e
+ (u'IPageletSlot interface not provided.', 'zope.interface.Interface')
+
+ >>> setup.placefulTearDown()
+
+ """
+
+ implements(ITALESPageletExpression)
+
+ def __init__(self, name, expr, engine):
+ if not '/' in expr:
+ error_msg = "use iface/pageletname for defining the pagelet."
+ raise KeyError(error_msg)
+ parts = expr.split('/')
+ if len(parts) > 2:
+ error_msg = "Do not use more then one / for defining iface/key"
+ raise KeyError(error_msg)
+
+ # get interface from key
+ self._iface = parts[0]
+ self._name = parts[1]
+
+ def __call__(self, econtext):
+ macros = []
+ iface = self._iface
+ name = self._name
+ context = econtext.vars['context']
+ request = econtext.vars['request']
+ view = econtext.vars['view']
+
+ # get interface from key
+ slotiface = queryInterface(iface)
+
+ # check slot
+ if slotiface == None:
+ raise PageletSlotInterfaceLookupError(
+ PageletError_slot_interface_not_found, iface)
+
+ # check interface
+ if not slotiface.isOrExtends(IPageletSlot):
+ raise PageletSlotInterfaceNotProvidedException(
+ PageletError_slot_interface_not_provided, iface)
+
+ slot = Wrapper()
+ directlyProvides(slot, slotiface)
+
+ collector = zapi.getMultiAdapter((context, request, view, slot)
+ , IMacroCollector)
+
+ return collector.__getitem__(name)
+
+
+
+class TALESPageDataExpression(StringExpr):
+ """Collect page data adapters via a tal namespace called tal:pagedata.
+
+ Imports:
+
+ >>> import zope.component
+ >>> from zope.interface import Interface
+ >>> from zope.security.checker import defineChecker
+ >>> from zope.publisher.browser import TestRequest
+ >>> from zope.publisher.interfaces.browser import IDefaultLayer
+ >>> from zope.component.interfaces import IView
+ >>> from zope.app.publisher.browser import BrowserView
+ >>> from zope.app.pagelet.interfaces import IPageletSlot
+ >>> from zope.app.pagelet.tests import TestContext
+ >>> from zope.app.pagelet.tests import TestClass
+ >>> from zope.app.pagelet.tests import testChecker
+
+ Register pagedata class:
+
+ >>> from zope.app.tests import setup, ztapi
+ >>> setup.placefulSetUp()
+ >>> factory = TestClass
+ >>> defineChecker(factory, testChecker)
+ >>> adaptersrv = zope.component.getService('Adapters')
+ >>> adaptersrv.register(
+ ... (Interface, IDefaultLayer, IView)
+ ... , IPageData, '', factory)
+
+ Register slot interface:
+
+ >>> from zope.app.component.interface import provideInterface
+ >>> provideInterface('', IPageData, None)
+
+ Register pagelets collector as a adapter:
+
+ >>> from zope.app.pagelet.collector import MacroCollector
+ >>> collector_factory = MacroCollector
+ >>> adaptersrv.register(
+ ... (Interface, IDefaultLayer, IView, IPageletSlot)
+ ... , IMacroCollector, '', collector_factory)
+
+ Register pagedata expression:
+
+ >>> from zope.app.pagetemplate.metaconfigure import registerType
+ >>> registerType('pagedata', TALESPageDataExpression)
+
+ Setup a simply browser view called 'index' with a 'index_pagedata.pt' template:
+
+ >>> from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
+ >>> ob = TestContext()
+ >>> request = TestRequest()
+ >>> view = BrowserView(ob, request)
+ >>> index = ViewPageTemplateFile('tests/testfiles/index_pagedata.pt')
+
+ Call the 'index' (view) on the browser view instance:
+
+ >>> html = index(view, request)
+
+ Test if the pagelet content is in the html output:
+
+ >>> import string
+ >>> string.count(html, 'A demo string.')
+ 1
+
+ """
+
+ implements(ITALESPageDataExpression)
+
+ def __init__(self, name, expr, engine):
+ if '/' in expr:
+ # named adapter
+ parts = expr.split('/')
+ self._iface = parts[0]
+ self._name = parts[1]
+
+ else:
+ # unnamed adapter
+ self._iface = expr
+ self._name = ''
+
+ def __call__(self, econtext):
+ macros = []
+ iface = self._iface
+ name = self._name
+ context = econtext.vars['context']
+ request = econtext.vars['request']
+ view = econtext.vars['view']
+
+ # get interface from key
+ iface = queryInterface(iface)
+
+ # check slot
+ if iface == None:
+ raise PageletSlotInterfaceLookupError(
+ PageletError_slot_interface_not_found, iface)
+
+ # check interface
+ if not iface.isOrExtends(IPageData):
+ raise PageletSlotInterfaceNotProvidedException(
+ PageletError_slot_interface_not_provided, iface)
+
+ # get a page data adapter registred on context, request, view
+ pagedata = zapi.getMultiAdapter((context, request, view)
+ , iface, name)
+
+ return pagedata
Property changes on: Zope3/trunk/src/zope/app/pagelet/tales.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/tests/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/tests/__init__.py 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/tests/__init__.py 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,84 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Pagelet tests
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.interface import Interface, implements
+
+from zope.security.checker import NamesChecker
+
+from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
+
+from zope.app.pagelet.interfaces import IPageletSlot
+from zope.app.pagelet.interfaces import IPagelet
+from zope.app.pagelet.interfaces import IPageData
+
+
+
+class ITestSlot(IPageletSlot):
+ """Pagelet test slot."""
+
+
+class TestSlot(object):
+ """Test pagelet slot"""
+
+ implements(ITestSlot)
+
+
+class TestPagelet(object):
+ """Test pagelet"""
+
+ implements(IPagelet)
+
+ _template = ViewPageTemplateFile('testfiles/test_pagelet.pt')
+ _weight = 0
+
+ def __init__(self, context, request, view, ignored):
+ self.context = context
+ self.request = request
+ self.view = view
+
+ def __getitem__(self, name):
+ """Get the macro by name."""
+ return self._template.macros[name]
+
+ def _getWeight (self):
+ """The weight of the pagelet."""
+ return self._weight
+
+ weight = property(_getWeight)
+
+
+class TestContext(object):
+ """Test context"""
+
+ implements(Interface)
+
+
+class TestClass(object):
+ """Test class"""
+
+ implements(IPageData)
+
+ def __init__(self, context, request, view):
+ pass
+
+ def getString(self):
+ return "A demo string."
+
+
+testChecker = NamesChecker(('__getitem__', 'request', 'weight'))
Property changes on: Zope3/trunk/src/zope/app/pagelet/tests/__init__.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/tests/pagelets.zcml
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/tests/pagelets.zcml 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/tests/pagelets.zcml 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,28 @@
+<zope:configure
+ xmlns:zope="http://namespaces.zope.org/zope"
+ xmlns="http://namespaces.zope.org/browser"
+ xmlns:meta="http://namespaces.zope.org/meta"
+ i18n_domain="zope"
+ >
+
+ <meta:directives namespace="http://namespaces.zope.org/browser">
+
+ <meta:directive
+ name="pagelet"
+ schema="zope.app.pagelet.metadirectives.IPageletDirective"
+ handler="zope.app.pagelet.metaconfigure.pagelet"
+ />
+
+ </meta:directives>
+
+ <pagelet
+ name="testpagelet"
+ layer="zope.publisher.interfaces.browser.IBrowserRequest"
+ slot="zope.app.pagelet.tests.ITestSlot"
+ template="testfiles/test_pagelet.pt"
+ for="*"
+ permission="zope.Public"
+ />
+
+</zope:configure>
+
Property changes on: Zope3/trunk/src/zope/app/pagelet/tests/pagelets.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/tests/test_directives.py
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/tests/test_directives.py 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/tests/test_directives.py 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,73 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Pagelet tests
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+import unittest
+
+from zope.configuration import xmlconfig
+
+from zope.publisher.browser import TestRequest
+
+from zope.interface import directlyProvides
+
+from zope.security.proxy import removeSecurityProxy
+
+from zope.component.tests.placelesssetup import PlacelessSetup
+from zope.component.interfaces import IView
+
+from zope.app import zapi
+from zope.app.servicenames import Adapters
+from zope.app.component.interface import queryInterface
+
+from zope.app.pagelet.interfaces import IPagelet
+from zope.app.pagelet import tests
+
+
+
+class Wrapper:
+ """Dummy class for to provide some interface."""
+
+
+class PageletDirectiveTest(PlacelessSetup, unittest.TestCase):
+ """Pagelet directive test."""
+
+ def setUp (self):
+ PlacelessSetup.setUp(self)
+ self.context = xmlconfig.file("pagelets.zcml", tests)
+
+ def test_pagelets (self):
+ key = 'zope.app.pagelet.tests.ITestSlot'
+ slot = queryInterface(key)
+ context = removeSecurityProxy(self.context)
+ slotwrapper = Wrapper()
+ viewwrapper = Wrapper()
+ directlyProvides(slotwrapper, slot)
+ directlyProvides(viewwrapper, IView)
+ objects = context, TestRequest(), viewwrapper, slotwrapper
+ views = zapi.getAdapters(objects, IPagelet)
+ self.assertEqual(len(views), 1)
+ self.assertEqual(views[0][0], u'testpagelet')
+
+
+def test_suite():
+ return unittest.TestSuite((
+ unittest.makeSuite(PageletDirectiveTest),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Property changes on: Zope3/trunk/src/zope/app/pagelet/tests/test_directives.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/tests/test_pagelet.py
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/tests/test_pagelet.py 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/tests/test_pagelet.py 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,45 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Pagelet tests
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+import unittest
+
+import zope.component
+
+from zope.security.checker import defineChecker
+
+from zope.testing.doctestunit import DocTestSuite
+from zope.testing.doctestunit import DocFileSuite
+
+from zope.app.tests import placelesssetup, ztapi
+from zope.app.tests import setup
+
+
+
+def test_suite():
+ return unittest.TestSuite((
+ DocTestSuite('zope.app.pagelet.tales'),
+ DocTestSuite('zope.app.pagelet.collector'),
+ DocFileSuite('../README.txt',
+ setUp=setup.placefulSetUp,
+ tearDown=setup.placefulTearDown(),
+ ),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
\ No newline at end of file
Property changes on: Zope3/trunk/src/zope/app/pagelet/tests/test_pagelet.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/tests/testfiles/__init__.py 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/tests/testfiles/__init__.py 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,11 @@
+################################################################################
+#
+# Copyright 2003-2004 by Projekt01 GmbH, CH-Cham
+# Licensed under the Open Software License version 2.0
+#
+################################################################################
+
+"""Tiks for Zope 3: Package pagelet.tests.
+
+$Id: __init__.py 1327 2004-10-21 22:20:22Z roger.ineichen $
+"""
Property changes on: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/__init__.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagedata.pt
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagedata.pt 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagedata.pt 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,13 @@
+<html>
+<body>
+
+ <h1 i18n:translate="">PageletContent View</h1>
+
+ <div class="row">
+ <metal:block tal:define="global data pagedata:zope.app.pagelet.interfaces.IPageData">
+ <tal:block content="data/getString" />
+ </metal:block>
+ </div>
+
+</body>
+</html>
Property changes on: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagedata.pt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelet.pt
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelet.pt 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelet.pt 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,11 @@
+<html>
+<body>
+
+ <h1 i18n:translate="">PageletContent View</h1>
+
+ <div class="row" tal:define="macro pagelet:zope.app.pagelet.interfaces.IPageletSlot/testpagelet">
+ <tal:block metal:use-macro="macro" />
+ </div>
+
+</body>
+</html>
Property changes on: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelet.pt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelet_iface_error.pt
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelet_iface_error.pt 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelet_iface_error.pt 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,11 @@
+<html>
+<body>
+
+ <h1 i18n:translate="">PageletContent View</h1>
+
+ <div class="row">
+ <tal:block metal:use-macro="pagelet:zope.interface.Interface/testpagelet" />
+ </div>
+
+</body>
+</html>
Property changes on: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelet_iface_error.pt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelets.pt
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelets.pt 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelets.pt 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,13 @@
+<html>
+<body>
+
+ <h1 i18n:translate="">PageletContent View</h1>
+
+ <div class="row">
+ <metal:block tal:repeat="pagelets pagelets:zope.app.pagelet.interfaces.IPageletSlot">
+ <tal:block metal:use-macro="pagelets" />
+ </metal:block>
+ </div>
+
+</body>
+</html>
Property changes on: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelets.pt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelets_iface_error.pt
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelets_iface_error.pt 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelets_iface_error.pt 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,13 @@
+<html>
+<body>
+
+ <h1 i18n:translate="">PageletContent View</h1>
+
+ <div class="row">
+ <metal:block tal:repeat="pagelets pagelets:zope.interface.Interface">
+ <tal:block metal:use-macro="pagelets" />
+ </metal:block>
+ </div>
+
+</body>
+</html>
Property changes on: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/index_pagelets_iface_error.pt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/test_pagelet.pt
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/tests/testfiles/test_pagelet.pt 2004-11-08 09:55:05 UTC (rev 28391)
+++ Zope3/trunk/src/zope/app/pagelet/tests/testfiles/test_pagelet.pt 2004-11-08 13:46:18 UTC (rev 28392)
@@ -0,0 +1,6 @@
+<html>
+<body>
+
+<div metal:define-macro="testpagelet">testpagelet macro content</div>
+
+</body></html>
Property changes on: Zope3/trunk/src/zope/app/pagelet/tests/testfiles/test_pagelet.pt
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the Zope3-Checkins
mailing list