Author: jmorliaguet
Date: Fri Mar 31 14:40:42 2006
New Revision: 2749

Added:
   
cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/configure.zcml   
(contents, props changed)
   cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/interfaces.py 
  (contents, props changed)
Modified:
   cpsskins/branches/jmo-perspectives/browser/negotiation/configure.zcml
   cpsskins/branches/jmo-perspectives/browser/negotiation/interfaces.py
   cpsskins/branches/jmo-perspectives/browser/negotiation/negotiator.py
   cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/theme.py
   cpsskins/branches/jmo-perspectives/browser/negotiation/views.py
Log:

- implemented negotiation chains



Modified: cpsskins/branches/jmo-perspectives/browser/negotiation/configure.zcml
==============================================================================
--- cpsskins/branches/jmo-perspectives/browser/negotiation/configure.zcml       
(original)
+++ cpsskins/branches/jmo-perspectives/browser/negotiation/configure.zcml       
Fri Mar 31 14:40:42 2006
@@ -26,4 +26,6 @@
 
   </pages>
 
+  <include package=".plugins" />
+
 </configure>

Modified: cpsskins/branches/jmo-perspectives/browser/negotiation/interfaces.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/browser/negotiation/interfaces.py        
(original)
+++ cpsskins/branches/jmo-perspectives/browser/negotiation/interfaces.py        
Fri Mar 31 14:40:42 2006
@@ -45,3 +45,10 @@
     def __call__(self):
         """Return the result of the negotiation."""
 
+
+class INegotiationChain(Interface):
+    """A marker interface for registering a list of negotiator interfaces
+    """
+
+    def __call__():
+        """Return the list of interfaces"""

Modified: cpsskins/branches/jmo-perspectives/browser/negotiation/negotiator.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/browser/negotiation/negotiator.py        
(original)
+++ cpsskins/branches/jmo-perspectives/browser/negotiation/negotiator.py        
Fri Mar 31 14:40:42 2006
@@ -13,7 +13,7 @@
 ##############################################################################
 """
 
-$Id:$
+$Id$
 """
 __docformat__ = "reStructuredText"
 

Added: 
cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/configure.zcml
==============================================================================
--- (empty file)
+++ 
cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/configure.zcml   
    Fri Mar 31 14:40:42 2006
@@ -0,0 +1,47 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope";>
+
+  <!-- theme negotiators -->
+
+  <adapter
+      factory=".theme.QueryParameter"
+      provides=".interfaces.IQueryParameterNegotiator"
+      for="cpsskins.elements.interfaces.IElement
+           cpsskins.interfaces.IThemeManagementFolder
+           zope.publisher.interfaces.IRequest"
+  />
+
+  <adapter
+      factory=".theme.Cookie"
+      provides=".interfaces.ICookieNegotiator"
+      for="cpsskins.elements.interfaces.IElement
+           cpsskins.interfaces.IThemeManagementFolder
+           zope.publisher.interfaces.IRequest"
+  />
+
+  <adapter
+      factory=".theme.Local"
+      provides=".interfaces.ILocalNegotiator"
+      for="cpsskins.elements.interfaces.IElement
+           cpsskins.interfaces.IThemeManagementFolder
+           zope.publisher.interfaces.IRequest"
+  />
+
+  <adapter
+      factory=".theme.Default"
+      provides=".interfaces.IDefaultNegotiator"
+      for="cpsskins.elements.interfaces.IElement
+           cpsskins.interfaces.IThemeManagementFolder
+           zope.publisher.interfaces.IRequest"
+  />
+
+  <adapter
+      factory=".theme.Context"
+      provides=".interfaces.IContextNegotiator"
+      for="cpsskins.elements.interfaces.IElement
+           cpsskins.interfaces.IThemeManagementFolder
+           zope.publisher.interfaces.IRequest"
+  />
+
+
+</configure>

Added: 
cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/interfaces.py
==============================================================================
--- (empty file)
+++ 
cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/interfaces.py    
    Fri Mar 31 14:40:42 2006
@@ -0,0 +1,43 @@
+##############################################################################
+#
+# Copyright (c) 2005-2006 Nuxeo 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.
+#
+##############################################################################
+"""
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+from zope.interface import Interface
+
+from cpsskins.browser.negotiation.interfaces import INegotiator
+
+class IQueryParameterNegotiator(INegotiator):
+    """Negotiation though a query parameter in a URL.
+    """
+
+class ICookieNegotiator(INegotiator):
+    """Cookie-based negotiation.
+    """
+
+class ILocalNegotiator(INegotiator):
+    """Local negotiation.
+    """
+
+class IDefaultNegotiator(INegotiator):
+    """Use the default object.
+    """
+
+class IContextNegotiator(INegotiator):
+    """Contextual negotiation.
+    """
+

Modified: 
cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/theme.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/theme.py     
(original)
+++ cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/theme.py     
Fri Mar 31 14:40:42 2006
@@ -17,17 +17,23 @@
 """
 __docformat__ = "reStructuredText"
 
+from zope.component import queryUtility, provideUtility
 from zope.interface import implements
 
 from cpsskins.browser.negotiation.interfaces import INegotiator
+from cpsskins.browser.negotiation.interfaces import INegotiationChain
 from cpsskins.browser.negotiation.negotiator import Negotiator
+from cpsskins.browser.negotiation.plugins.interfaces import (
+    IQueryParameterNegotiator, ICookieNegotiator, ILocalNegotiator,
+    IDefaultNegotiator, IContextNegotiator)
+
 from cpsskins.elements.interfaces import ITheme
 
 class QueryParameter(Negotiator):
     """Look for a theme whose name is passed as a URL query parameter:
     ?theme=name
     """
-    implements(INegotiator)
+    implements(IQueryParameterNegotiator)
 
     def __call__(self):
         name = self.request.form.get('theme')
@@ -38,7 +44,7 @@
 class Cookie(Negotiator):
     """Look for a theme whose name is stored in a cookie
     """
-    implements(INegotiator)
+    implements(ICookieNegotiator)
 
     def __call__(self):
         name = self.request.cookies.get('cpsskins_work_theme')
@@ -46,19 +52,39 @@
             return None
         return self.manager.getThemeByName(name)
 
-class ThemeInContext(Negotiator):
+class Local(Negotiator):
     """Look for a theme located in a parent folder.
     """
-    implements(INegotiator)
+    implements(ILocalNegotiator)
 
     def __call__(self):
         return queryUtility(ITheme, context=self.context)
 
-class DefaultTheme(Negotiator):
+class Default(Negotiator):
     """Look for a default theme
     """
-    implements(INegotiator)
+    implements(IDefaultNegotiator)
 
     def __call__(self):
         return self.manager.getDefaultTheme()
 
+class Context(Negotiator):
+    """Look for a theme in the context
+    """
+    implements(IContextNegotiator)
+
+    def __call__(self):
+        return ITheme.implementedBy(self.context)
+
+
+class ThemeNegotiationChain:
+
+    implements(INegotiationChain)
+
+    def __call__(self):
+
+        return (IQueryParameterNegotiator, ICookieNegotiator,
+                IContextNegotiator, ILocalNegotiator, IDefaultNegotiator,
+               )
+
+provideUtility(ThemeNegotiationChain(), INegotiationChain, 'theme')

Modified: cpsskins/branches/jmo-perspectives/browser/negotiation/views.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/browser/negotiation/views.py     
(original)
+++ cpsskins/branches/jmo-perspectives/browser/negotiation/views.py     Fri Mar 
31 14:40:42 2006
@@ -20,21 +20,34 @@
 from zope.app import zapi
 from zope.app.publisher.browser import BrowserView
 from zope.app.session.interfaces import ISession
-from zope.component import queryUtility
+from zope.component import getMultiAdapter
 from zope.interface import implements
 
 from cpsskins.elements.interfaces import ITheme, IThemePage
 from cpsskins.browser.rendering.interfaces import IViewer
 from cpsskins.browser.negotiation.interfaces import INegotiation
+from cpsskins.browser.negotiation.interfaces import INegotiationChain
 from cpsskins.utils import getThemeManager
 
 from cpsskins.browser.negotiation.plugins import theme as themePlugin
+from cpsskins.browser.negotiation.plugins.interfaces import (
+    IQueryParameterNegotiator, ICookieNegotiator, ILocalNegotiator,
+    IDefaultNegotiator, IContextNegotiator)
 
 class NegotiationView(BrowserView):
     """A view for the negotiation of the effective theme, perspective, ...
     """
     implements(INegotiation)
 
+    def negotiate(self, type):
+        chain = zapi.getUtility(interface=INegotiationChain, name=type)()
+        negotiation_context = (self.context, getThemeManager(), self.request)
+        for negotiator in chain:
+            result = getMultiAdapter(negotiation_context, negotiator)()
+            if result:
+                return result
+        return None
+
     ###################################################################
     # Themes and pages
     ###################################################################
@@ -43,19 +56,7 @@
         """Return the effective theme
         (the one that will be effectively displayed)
         """
-        context = self.context
-        request = self.request
-        manager = getThemeManager()
-
-        chain = (themePlugin.QueryParameter, themePlugin.Cookie,
-                 themePlugin.ThemeInContext, themePlugin.DefaultTheme,
-                )
-
-        # TODO: use adapters
-        for negotiator in chain:
-            result = negotiator(context, manager, request)()
-            if result:
-                return result
+        return self.negotiate('theme')
 
     def setWorkTheme(self, name=u''):
         """Set the work theme.
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to