Author: jmorliaguet
Date: Thu Mar 30 00:46:21 2006
New Revision: 2744

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

- began modularizing the negotiation chain(s)



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        
Thu Mar 30 00:46:21 2006
@@ -39,3 +39,9 @@
     def getPageViewer():
         """Return the viewer used to render the page."""
 
+
+class INegotiator(Interface):
+
+    def __call__(self):
+        """Return the result of the negotiation."""
+

Added: cpsskins/branches/jmo-perspectives/browser/negotiation/negotiator.py
==============================================================================
--- (empty file)
+++ cpsskins/branches/jmo-perspectives/browser/negotiation/negotiator.py        
Thu Mar 30 00:46:21 2006
@@ -0,0 +1,29 @@
+##############################################################################
+#
+# 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"
+
+class Negotiator(object):
+
+    def __init__(self, context, manager, request):
+        self.context = context
+        self.manager = manager
+        self.request = request
+
+    def __call__(self):
+        raise NotImplementedError(
+            "A negotiator must implement the __call__() method.")

Added: 
cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/__init__.py
==============================================================================
--- (empty file)
+++ cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/__init__.py  
Thu Mar 30 00:46:21 2006
@@ -0,0 +1,19 @@
+##############################################################################
+#
+# 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"
+

Added: cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/theme.py
==============================================================================
--- (empty file)
+++ cpsskins/branches/jmo-perspectives/browser/negotiation/plugins/theme.py     
Thu Mar 30 00:46:21 2006
@@ -0,0 +1,64 @@
+##############################################################################
+#
+# 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 implements
+
+from cpsskins.browser.negotiation.interfaces import INegotiator
+from cpsskins.browser.negotiation.negotiator import Negotiator
+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)
+
+    def __call__(self):
+        name = self.request.form.get('theme')
+        if not name:
+            return None
+        return self.manager.getThemeByName(name)
+
+class Cookie(Negotiator):
+    """Look for a theme whose name is stored in a cookie
+    """
+    implements(INegotiator)
+
+    def __call__(self):
+        name = self.request.cookies.get('cpsskins_work_theme')
+        if not name:
+            return None
+        return self.manager.getThemeByName(name)
+
+class ThemeInContext(Negotiator):
+    """Look for a theme located in a parent folder.
+    """
+    implements(INegotiator)
+
+    def __call__(self):
+        return queryUtility(ITheme, context=self.context)
+
+class DefaultTheme(Negotiator):
+    """Look for a default theme
+    """
+    implements(INegotiator)
+
+    def __call__(self):
+        return self.manager.getDefaultTheme()
+

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     Thu Mar 
30 00:46:21 2006
@@ -28,6 +28,8 @@
 from cpsskins.browser.negotiation.interfaces import INegotiation
 from cpsskins.utils import getThemeManager
 
+from cpsskins.browser.negotiation.plugins import theme as themePlugin
+
 class NegotiationView(BrowserView):
     """A view for the negotiation of the effective theme, perspective, ...
     """
@@ -43,25 +45,17 @@
         """
         context = self.context
         request = self.request
+        manager = getThemeManager()
 
-        tmutil = getThemeManager()
-
-        # ?theme=...
-        name = request.form.get('theme')
-        if name:
-            theme = tmutil.getThemeByName(name)
-            if theme:
-                return theme
-
-        # cookie
-        name = request.cookies.get('cpsskins_work_theme')
-        if name:
-            theme = tmutil.getThemeByName(name)
-            if theme:
-                return theme
-
-        # theme in context or default theme
-        return queryUtility(ITheme, context=context) or 
tmutil.getDefaultTheme()
+        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
 
     def setWorkTheme(self, name=u''):
         """Set the work theme.
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to