[Zope3-checkins] SVN: Zope3/trunk/ Added zope.app.pageletchooser package from 'svn.tiks.org' repository

Roger Ineichen roger at projekt01.ch
Mon Nov 8 08:47:33 EST 2004


Log message for revision 28393:
  Added zope.app.pageletchooser package from 'svn.tiks.org' repository

Changed:
  A   Zope3/trunk/package-includes/pageletchooser-configure.zcml
  A   Zope3/trunk/src/zope/app/pageletchooser/
  A   Zope3/trunk/src/zope/app/pageletchooser/DEPENDENCIES.cfg
  A   Zope3/trunk/src/zope/app/pageletchooser/README.txt
  A   Zope3/trunk/src/zope/app/pageletchooser/SETUP.cfg
  A   Zope3/trunk/src/zope/app/pageletchooser/__init__.py
  A   Zope3/trunk/src/zope/app/pageletchooser/adapters.py
  A   Zope3/trunk/src/zope/app/pageletchooser/browser/
  A   Zope3/trunk/src/zope/app/pageletchooser/browser/__init__.py
  A   Zope3/trunk/src/zope/app/pageletchooser/browser/notfound_pagelet.pt
  A   Zope3/trunk/src/zope/app/pageletchooser/collector.py
  A   Zope3/trunk/src/zope/app/pageletchooser/configure.zcml
  A   Zope3/trunk/src/zope/app/pageletchooser/exceptions.py
  A   Zope3/trunk/src/zope/app/pageletchooser/interfaces.py
  A   Zope3/trunk/src/zope/app/pageletchooser/tests.py
  A   Zope3/trunk/src/zope/app/pageletchooser/vocabulary.py

-=-
Added: Zope3/trunk/package-includes/pageletchooser-configure.zcml
===================================================================
--- Zope3/trunk/package-includes/pageletchooser-configure.zcml	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/package-includes/pageletchooser-configure.zcml	2004-11-08 13:47:33 UTC (rev 28393)
@@ -0,0 +1 @@
+<include package="zope.app.pageletchooser" />
\ No newline at end of file


Property changes on: Zope3/trunk/package-includes/pageletchooser-configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/pageletchooser/DEPENDENCIES.cfg
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/DEPENDENCIES.cfg	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/DEPENDENCIES.cfg	2004-11-08 13:47:33 UTC (rev 28393)
@@ -0,0 +1,6 @@
+zope.app
+zope.app.pagelet
+zope.component
+zope.interface
+zope.schema
+zope.security


Property changes on: Zope3/trunk/src/zope/app/pageletchooser/DEPENDENCIES.cfg
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/pageletchooser/README.txt
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/README.txt	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/README.txt	2004-11-08 13:47:33 UTC (rev 28393)
@@ -0,0 +1,89 @@
+==============
+PageletChooser
+==============
+
+It's really helpfull if you understand how page template macros 
+get rendered in the metal:use-macro tag.
+
+The PageletChooser is a enhancement for the MacroCollector
+adapter which can lookup paelet macros for a given name
+related to the instance. If you use it, you will get a 
+view on the instance for to choose which pagelt macro will
+be used. You can select a pagelet macro name from the different 
+registred pagelets.
+
+The adapter PageletChooser registred for IChooseablePagelets
+can lookup and return a pagelet name for a given key. 
+The PageletChooser adapter requires a IPageletNameManager adapter 
+for to get a object which provides the property with the property
+where we whant lookup the mapped name. 
+
+Use case
+--------
+
+Say you have a skin with a first level navigation with a image which
+requieres a image map driven by javascript. 
+
+What we can do with the PageletChooser is:
+
+First add a page template and image with the image map and register
+this page template as a pagelet 'called firstlevel_macro_10'.
+
+Add a interface inherited called 'IFirstLevelPagelets' from 
+IChooseablePagelets which is inherited from IPageletSlot. This means you 
+are defining a pagelet slot interface. 
+
+Add a index.html view and use the pagelet: tag with the interface
+'IFirstLevelPagelets' like:
+<metal:block tal:define="global chooser ...
+  ...pagelet:zope.app.demo.pageletchooser.interfaces.IFirstLevelPagelets">
+  <tal:block metal:use-macro="chooser/firstlevel" />
+</metal:block>
+
+Register a content type which implements the schema 'IChooseablePageletNames'
+and register a adapter which supports the  'IPageletNameManager' schema.
+The 'IPageletNameManager' adapter has to support your pagelet macro
+names as field properties. In our use case 'firstlevel'.
+
+For to let you change the pagelet macro names on the contentn type,
+you need some different components.
+
+Register a PageletNamesVocabulary vocabulary called 'firstlevelnames'.
+Declare also your layer, view and slot interface in the vocabulary ZCML
+directive. This vocabulary can lookup the macro names of pagelets registred
+on this layer, views and slots. Use this vocabulary in the property fields
+of your IPageletNameStore interface as a Choice field. This will render the 
+fields as a selectbox where you can select on of the registred pagelet macro
+names. Use this vacabulary for the schema of your pagelet names.
+
+Define Choice fields in the schema 'IFirstLevelPagelets'. 
+The name of the field is the key of the pagelt where you will lookup in a 
+page template. In our example the field is called 'firstlevel' which is 
+useing the vocabulary "firstlevelmacronames".
+
+Add another adapter on your content type supporting the 'IFirstLevelPagelets' 
+schema. This adapter is setting and getting the mapped names of the 
+property field 'firstlevel'.
+
+So, that's enough for this moment. For a real example look at the package 
+zope.app.demo pageletchooser.
+
+Feel free to enhance or change this README for a better understanding.
+
+
+Glosary:
+--------
+
+Pagelet name or macro name:
+
+Pagelet names are page template macro names which can be rendered 
+in the metal:use-macro tag. A pagelet name is the name of the macro.
+This means if you register a pagelet with the name 'firstlevel_macro_10' 
+the page template must contain a macro called 'firstlevel_macro_10' like:
+'define-macro="firstlevel_macro_10"'
+
+PageletChooser:
+
+Can lookup pagelet names on objects via the adapter IPageletNameManager
+A PageletChooser is a replacement for the MacroCollector which is used
+as a IMacroCollctor adapter.

Added: Zope3/trunk/src/zope/app/pageletchooser/SETUP.cfg
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/SETUP.cfg	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/SETUP.cfg	2004-11-08 13:47:33 UTC (rev 28393)
@@ -0,0 +1,5 @@
+# Tell zpkg how to install the ZCML slugs.
+
+<data-files zopeskel/etc/package-includes>
+  pageletchooser-*.zcml
+</data-files>


Property changes on: Zope3/trunk/src/zope/app/pageletchooser/SETUP.cfg
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/pageletchooser/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/__init__.py	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/__init__.py	2004-11-08 13:47:33 UTC (rev 28393)
@@ -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.
+#
+##############################################################################
+"""PageletChooser
+
+$Id:$
+"""


Property changes on: Zope3/trunk/src/zope/app/pageletchooser/__init__.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/pageletchooser/adapters.py
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/adapters.py	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/adapters.py	2004-11-08 13:47:33 UTC (rev 28393)
@@ -0,0 +1,111 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""PageletChooser adapters
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+from persistent.dict import PersistentDict
+
+from zope.security.proxy import removeSecurityProxy
+
+from zope.interface import implements
+
+from zope.app import zapi
+from zope.app.annotation.interfaces import IAnnotations
+from zope.app.pageletchooser.interfaces import IAnnotatableMappingAdapter
+
+annotation_key = "zope.app.pageletchooser.pageletnamemapping"
+
+
+
+class AnnotatableMappingAdapter(object):
+    """Abstract adapter base class for to store a pagelet name mapping.
+    
+    This adapter uses the annotation key 
+    'zope.app.pageletchooser.pageletnamemapping' for to store the mapping
+    which is just a persistent dict.
+    
+    Inherit from this base adapter class for to store the pagelet names
+    in the annotation. Your implementation has also to support your own
+    pagelet name schema. The pagelet names are stored in persistent dict
+    in the annotation of the object. To access this dict data use a 
+    adapter which maps the dict data to field property.  This let's you
+    register a edit view for to edit the pagelet names. You can use the
+    vocabular PageletNamesVocabulary for to lookup the registred pagelet 
+    names in the fields.
+    
+    For a example look at the package: zope.app.demo.pageletchooser.
+
+    Setup::
+
+        >>> from zope.interface import directlyProvides
+        >>> from zope.interface import Interface
+        >>> from zope.app.tests import placelesssetup, ztapi
+        >>> from zope.app.annotation.interfaces import IAnnotations
+        >>> from zope.app.annotation.interfaces import IAttributeAnnotatable
+        >>> from zope.app.annotation.attribute import AttributeAnnotations
+        >>> from zope.app.pagelet.tests import TestContext
+        
+        >>> placelesssetup.setUp()
+        >>> ztapi.provideAdapter(IAttributeAnnotatable, IAnnotations
+        ...                     ,AttributeAnnotations)
+        >>> ztapi.provideAdapter(Interface, IAnnotatableMappingAdapter
+        ...                     ,AnnotatableMappingAdapter)
+
+    Make test object:
+
+        >>> obj = TestContext()
+        >>> directlyProvides(obj, IAttributeAnnotatable)
+
+    Test AnnotatableMappingAdapter:
+
+        >>> mapping = IAnnotatableMappingAdapter(obj)
+        >>> mapping.__setitem__('key1', 'value1')
+        >>> mapping['key1']
+        'value1'
+
+        >>> mapping.__getitem__('key1')
+        'value1'
+
+        >>> mapping.__delitem__('key1')
+        >>> mapping['key1']
+        Traceback (most recent call last):
+        ...
+        KeyError: 'key1'
+
+    """
+
+    implements(IAnnotatableMappingAdapter)
+
+
+    def __init__(self, context=None):
+        context = removeSecurityProxy(context)
+        self.context = context
+        self._annotations = IAnnotations(context)
+        
+        if not self._annotations.get(annotation_key):
+            self._annotations[annotation_key] = PersistentDict()
+        
+        self._data = self._annotations.get(annotation_key)
+        
+    def __getitem__(self, key):
+        return self._data[key]
+
+    def __setitem__(self, key, value):
+        self._data[key] = value
+        
+    def __delitem__(self, key):
+        del self._data[key]


Property changes on: Zope3/trunk/src/zope/app/pageletchooser/adapters.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/pageletchooser/browser/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/browser/__init__.py	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/browser/__init__.py	2004-11-08 13:47:33 UTC (rev 28393)
@@ -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.
+#
+##############################################################################
+"""PageletChooser
+
+$Id:$
+"""


Property changes on: Zope3/trunk/src/zope/app/pageletchooser/browser/__init__.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/pageletchooser/browser/notfound_pagelet.pt
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/browser/notfound_pagelet.pt	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/browser/notfound_pagelet.pt	2004-11-08 13:47:33 UTC (rev 28393)
@@ -0,0 +1,3 @@
+<div class="notfoundmacro" metal:define-macro="notfoundmacro">
+<span i18n.translate="">PageletChooser didn't find a pagelet macro!</span>
+</div>


Property changes on: Zope3/trunk/src/zope/app/pageletchooser/browser/notfound_pagelet.pt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/pageletchooser/collector.py
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/collector.py	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/collector.py	2004-11-08 13:47:33 UTC (rev 28393)
@@ -0,0 +1,110 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""PageletChooser collector
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.interface import implements
+
+from zope.app import zapi
+
+from zope.app.pagelet.interfaces import IPagelet
+from zope.app.pagelet.collector import MacroCollector
+
+from zope.app.pageletchooser.interfaces import IMacroChooser
+from zope.app.pageletchooser.interfaces import IPageletNameManager
+
+
+
+class MacroChooser(MacroCollector):
+    """Returns the macro by name.
+        
+        For to get the macro name, the adapter IPageletNameManager is 
+        calling the mapped name under the given key via getattr. This
+        means the adapted object has to support a field property with
+        the given key and has to return a existing pagelet macro name.
+
+    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
+        >>> from zope.app.pageletchooser.tests import TestMapping
+
+    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 macro chooser:
+        
+        >>> request = TestRequest()
+        >>> view = BrowserView(ob, request)
+        >>> slot = TestSlot()
+        >>> chooser = MacroChooser(ob, request, view, slot)
+
+    Setup the IPageletNameManager adapter
+
+        >>> ztapi.provideAdapter(Interface, IPageletNameManager
+        ...                     ,TestMapping)
+
+    Get the macro form the MacroChooser
+
+        >>> macro = chooser.__getitem__('alias')
+
+    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(IMacroChooser)
+
+    _defaultmacroname = 'notfoundmacro'
+
+    def __getitem__(self, key):
+        adapter = IPageletNameManager(self.context)
+        try:
+            macroname = getattr(adapter, key)
+        except:
+            macroname = self._defaultmacroname
+        
+        objects = self.context, self.request, self.view, self.slot
+        pagelet = zapi.getMultiAdapter(objects, IPagelet, macroname)
+        
+        return pagelet[macroname]


Property changes on: Zope3/trunk/src/zope/app/pageletchooser/collector.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/pageletchooser/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/configure.zcml	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/configure.zcml	2004-11-08 13:47:33 UTC (rev 28393)
@@ -0,0 +1,42 @@
+<configure 
+    xmlns="http://namespaces.zope.org/zope"
+    xmlns:browser="http://namespaces.zope.org/browser"
+    xmlns:tiks="http://namespaces.tiks.org/tiks"
+    i18n_domain="zope"
+    >
+
+  <interface interface=".interfaces.IChooseablePageletNames" />
+
+  <!-- IMacroCollector adapter on IChooseablePagelets slots -->
+  <adapter
+      for="zope.interface.Interface
+           zope.publisher.interfaces.browser.IBrowserRequest
+           zope.component.interfaces.IView
+           .interfaces.IChooseablePagelets"
+      factory=".collector.MacroChooser"
+      provides="zope.app.pagelet.interfaces.IMacroCollector"
+      />
+
+  <!-- not found pagelet (pagelet chooser default pagelet) -->
+  <browser:pagelet
+      name="notfoundmacro"
+      for=".interfaces.IChooseablePageletNames"
+      slot=".interfaces.IChooseablePagelets"
+      template="browser/notfound_pagelet.pt"
+      permission="zope.View"
+      />
+
+  <!-- Vocabulary called 'firstlevelmacronames' for to collect the pagelet
+       macro names in the edit view. Used in the interface IMySchema.
+       ! Don't use dotted name shortcuts for slot, layer and view interfaces, 
+       because we get the interfaces for this dotted strings with the iface 
+       method queryInterface(). -->
+  <vocabulary
+      name="chooseablepageletnames"
+      factory=".vocabulary.PageletNamesVocabulary"
+      layer="zope.publisher.interfaces.browser.IBrowserRequest"
+      view="zope.component.interfaces.IView"
+      slot="zope.app.pageletchooser.interfaces.IChooseablePagelets"
+      />
+
+</configure>


Property changes on: Zope3/trunk/src/zope/app/pageletchooser/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/pageletchooser/exceptions.py
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/exceptions.py	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/exceptions.py	2004-11-08 13:47:33 UTC (rev 28393)
@@ -0,0 +1,30 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""PageletChooser exceptions
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.component import ComponentLookupError
+
+from zope.app.i18n import ZopeMessageIDFactory as _
+
+
+
+class PageletVocabularyInterfaceLookupError(ComponentLookupError):
+    """Pagelet vocabulary interface not found."""
+
+PageletError_vocabulary_interface_not_found = _(
+    u'Pagelet vocabulary interface not found.')


Property changes on: Zope3/trunk/src/zope/app/pageletchooser/exceptions.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/pageletchooser/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/interfaces.py	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/interfaces.py	2004-11-08 13:47:33 UTC (rev 28393)
@@ -0,0 +1,100 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""PageletChooser interfaces
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.interface import Interface
+
+from zope.interface.common.mapping import IItemMapping, IWriteMapping
+
+from zope.schema import Dict
+
+from zope.app.pagelet.interfaces import IPageletSlot
+from zope.app.pagelet.interfaces import IMacroCollector
+
+
+
+class IMacroChooser(IMacroCollector):
+    """Adapter for collect macros related to instances.
+    
+    The standard MacroChooser implementation will call a
+    adapter providing IPageletNameManager for collecting
+    the macro names.
+    """
+
+    def setDefault(name):
+        """Set the default name which will be returnd if no pagelet is found.
+        
+        This default pagelet name has to be set every time befor we call
+        the __Getitem__ method for to get a pagelet by a key. If the 
+        pagelet with the given key isn't found, the __getitem__ method
+        will try to find the pagelet with the default name.
+        
+        If you inherit your pagelet slot form the IChooseablePagelets slot 
+        you can use the default name 'notfoundmacro' for to get the 
+        macro called 'notfoundmacro' from the 'notfound_pagelet.pt' pagelet.
+        
+        """
+
+
+class IChooseablePageletNames(Interface):
+    """Marker interface for support IPageletNameManager adapter."""
+
+
+
+class IPageletNameManager(Interface):
+    """Marker interface for supporting pagelet name lookup.
+    
+    The adapter which implements this interface has to provide
+    macronames which are mapped as field properties.
+    """
+
+
+
+class IAnnotatableMappingAdapter(IItemMapping, IWriteMapping):
+    """Map macro keys to registred IPagelet adapter names.
+    
+    We use it as helper mapping for to let the macro names store to the 
+    annotation.
+    """
+
+    def __getitem__(self, key):
+        """Returns the macro name related to the given key."""
+
+    def __setitem__(self, key, value):
+        """Set the macro name related to the given key."""
+
+    def __delitem__(self, key):
+        """Deletes the macro name related to the given key."""
+
+
+
+class IChooseablePagelets(IPageletSlot):
+    """Base interface for chooseable pagelet slots.
+
+    Use this interface for inherit you own chooseable slots
+    where you can register your chooseable pagelets. This 
+    interface shouldn't be used directly otherwise you don't
+    have a possible slot interface for to support different
+    slots and all registred pagelets will returned if you 
+    use the vocabulary.
+
+    If you inherit from this interface use the new interface for
+    to register you own vocabulary, which returns all registred
+    pagelets to the new slot interface.
+
+    """


Property changes on: Zope3/trunk/src/zope/app/pageletchooser/interfaces.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/pageletchooser/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/tests.py	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/tests.py	2004-11-08 13:47:33 UTC (rev 28393)
@@ -0,0 +1,52 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""PageletChooser tests
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+import unittest
+from zope.testing.doctestunit import DocTestSuite
+from zope.testing.doctestunit import DocFileSuite
+
+import zope.component
+
+from zope.security.checker import defineChecker
+
+from zope.app.tests import placelesssetup, ztapi
+from zope.app.tests import setup
+
+
+class TestMapping(object):
+    def __init__(self, context=None):
+        pass
+
+    def _getAlias(self):
+        """Return the macro name."""
+        return 'testpagelet'
+
+    alias = property(_getAlias)
+
+
+
+def test_suite():
+    return unittest.TestSuite((
+        DocTestSuite('zope.app.pageletchooser.adapters'),
+        DocTestSuite('zope.app.pageletchooser.collector'),
+        DocTestSuite('zope.app.pageletchooser.vocabulary'),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: Zope3/trunk/src/zope/app/pageletchooser/tests.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/pageletchooser/vocabulary.py
===================================================================
--- Zope3/trunk/src/zope/app/pageletchooser/vocabulary.py	2004-11-08 13:46:18 UTC (rev 28392)
+++ Zope3/trunk/src/zope/app/pageletchooser/vocabulary.py	2004-11-08 13:47:33 UTC (rev 28393)
@@ -0,0 +1,148 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""PageletChooser vocabulary
+
+$Id:$
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.interface import directlyProvides
+
+from zope.schema.vocabulary import SimpleTerm
+from zope.schema.vocabulary import SimpleVocabulary
+
+from zope.app import zapi
+
+from zope.app.component.interface import queryInterface
+
+from zope.app.pagelet.interfaces import IPagelet
+
+from zope.app.pageletchooser.exceptions import \
+    PageletVocabularyInterfaceLookupError
+from zope.app.pageletchooser.exceptions import \
+    PageletError_vocabulary_interface_not_found
+
+from zope.app.pageletchooser.interfaces import IChooseablePagelets
+
+
+
+class Wrapper:
+    """Dummy class for to provide a interface."""
+
+
+
+class PageletNamesVocabulary(SimpleVocabulary):
+    """A vocabular of optional pagelet macro names for a given interface.
+
+    Imports:
+    
+        >>> import zope.component
+        >>> from zope.interface import Interface
+        >>> from zope.security.checker import defineChecker
+        >>> from zope.publisher.interfaces.browser import IBrowserRequest
+        >>> from zope.component.interfaces import IView
+        >>> 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
+
+    Setup:
+
+        >>> from zope.app.tests import setup, ztapi
+        >>> setup.placefulSetUp()
+
+    Register interfaces used for pagelet and vocabulary:
+
+        >>> from zope.app.component.interface import provideInterface
+        >>> utilities = zapi.getGlobalService(zapi.servicenames.Utilities)
+        >>> provideInterface('', IBrowserRequest, None)
+        >>> provideInterface('', IView, None)
+        >>> provideInterface('', IChooseablePagelets, None)
+
+    Register pagelet:
+
+        >>> name = 'testpagelet'
+        >>> pagelet_factory = TestPagelet
+        >>> defineChecker(pagelet_factory, testChecker)
+        >>> adaptersrv = zope.component.getService('Adapters')
+        >>> adaptersrv.register(
+        ...        (Interface, IBrowserRequest, IView, IPageletSlot)
+        ...        , IPagelet, name, pagelet_factory)
+
+    Register vocabulary:
+        
+        >>> from zope.app.pagelet.tests import TestContext
+        >>> name="chooseablepageletnames"
+        >>> factory=".vocabulary.PageletNamesVocabulary"
+        >>> layer="zope.publisher.interfaces.browser.IBrowserRequest"
+        >>> view="zope.component.interfaces.IView"
+        >>> slot="zope.app.pageletchooser.interfaces.IChooseablePagelets"
+        >>> obj = TestContext()
+        >>> vocab = PageletNamesVocabulary(obj, layer, view, slot)
+
+    Test vocabulary:
+
+        >>> "testpagelet" in vocab
+        True
+
+        >>> "nothing" in vocab
+        False
+
+        >>> setup.placefulTearDown()
+
+    """
+    def __init__(self, context, layer, view, slot):
+        macronames = []
+        terms = []
+        
+        # get and check interface
+        layeriface = queryInterface(layer)
+        if layeriface is None:
+            raise PageletVocabularyInterfaceLookupError(
+                    PageletError_vocabulary_interface_not_found, layer)
+
+        viewiface = queryInterface(view)
+        if viewiface is None:
+            raise PageletVocabularyInterfaceLookupError(
+                    PageletError_vocabulary_interface_not_found, view)
+
+        slotiface = queryInterface(slot)
+        if slotiface is None:
+            raise PageletVocabularyInterfaceLookupError(
+                    PageletError_vocabulary_interface_not_found, slot)
+
+        # prepare objects for lookup the adapters        
+        layerwrapper = Wrapper()
+        directlyProvides(layerwrapper, layeriface)
+        
+        viewwrapper = Wrapper()
+        directlyProvides(viewwrapper, viewiface)
+        
+        slotwrapper = Wrapper()
+        directlyProvides(slotwrapper, slotiface)
+        
+        # collect pagelets
+        objects = context, layerwrapper, viewwrapper, slotwrapper
+        pagelets = zapi.getAdapters(objects, IPagelet)
+        pagelets.sort(lambda x, y: x[1].weight - y[1].weight)
+
+        for name, pagelet in pagelets:
+            macronames.append(name)
+
+        for name in macronames:
+            terms.append(SimpleTerm(name, name, name))
+
+        terms.sort(lambda lhs, rhs: cmp(lhs.title, rhs.title))
+        super(PageletNamesVocabulary, self).__init__(terms)


Property changes on: Zope3/trunk/src/zope/app/pageletchooser/vocabulary.py
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Zope3-Checkins mailing list