[Zope3-checkins] SVN: Zope3/branches/hdima-language-namespace/src/zope/app/publisher/browser/ Added NotCompatibleAdapterError exception

Dmitry Vasiliev dima at hlabs.spb.ru
Tue Oct 18 10:46:02 EDT 2005


Log message for revision 39496:
  Added NotCompatibleAdapterError exception 
  

Changed:
  U   Zope3/branches/hdima-language-namespace/src/zope/app/publisher/browser/__init__.py
  U   Zope3/branches/hdima-language-namespace/src/zope/app/publisher/browser/tests/test_browserlanguages.py

-=-
Modified: Zope3/branches/hdima-language-namespace/src/zope/app/publisher/browser/__init__.py
===================================================================
--- Zope3/branches/hdima-language-namespace/src/zope/app/publisher/browser/__init__.py	2005-10-18 14:43:41 UTC (rev 39495)
+++ Zope3/branches/hdima-language-namespace/src/zope/app/publisher/browser/__init__.py	2005-10-18 14:46:01 UTC (rev 39496)
@@ -21,6 +21,7 @@
 import zope.interface
 from zope.interface import implements, directlyProvidedBy, directlyProvides
 from zope.publisher.browser import BrowserLanguages
+from zope.i18n.interfaces import IUserPreferredLanguages
 from zope.i18n.interfaces import IModifiableUserPreferredLanguages
 
 from zope.app.annotation import IAnnotations
@@ -135,22 +136,22 @@
     ifaces.append(skin)
     directlyProvides(request, *ifaces)
 
-class ModifiableBrowserLanguages(BrowserLanguages):
+class NotCompatibleAdapterError(Exception):
+    """Adapter not compatible with ModifiableBrowserLanguages
+       adapter has been used.
+    """
 
-    implements(IModifiableUserPreferredLanguages)
+class CacheableBrowserLanguages(BrowserLanguages):
 
-    def setPreferredLanguages(self, languages):
-        languages_data = self._getLanguagesData()
-        languages_data["overridden"] = languages
-        self.request.setupLocale()
+    implements(IUserPreferredLanguages)
 
     def getPreferredLanguages(self):
         languages_data = self._getLanguagesData()
         if "overridden" in languages_data:
             return languages_data["overridden"]
         elif "cached" not in languages_data:
-            languages_data["cached"] = super(ModifiableBrowserLanguages,
-                self).getPreferredLanguages()
+            languages_data["cached"] = super(
+                CacheableBrowserLanguages, self).getPreferredLanguages()
         return languages_data["cached"]
 
     def _getLanguagesData(self):
@@ -159,3 +160,16 @@
         if languages_data is None:
             annotations[key] = languages_data = {}
         return languages_data
+
+class ModifiableBrowserLanguages(CacheableBrowserLanguages):
+
+    implements(IModifiableUserPreferredLanguages)
+
+    def setPreferredLanguages(self, languages):
+        annotations = IAnnotations(self.request)
+        languages_data = annotations.get(key)
+        if languages_data is None:
+            raise NotCompatibleAdapterError("Adapter not compatible with "
+                "ModifiableBrowserLanguages adapter has been used.")
+        languages_data["overridden"] = languages
+        self.request.setupLocale()

Modified: Zope3/branches/hdima-language-namespace/src/zope/app/publisher/browser/tests/test_browserlanguages.py
===================================================================
--- Zope3/branches/hdima-language-namespace/src/zope/app/publisher/browser/tests/test_browserlanguages.py	2005-10-18 14:43:41 UTC (rev 39495)
+++ Zope3/branches/hdima-language-namespace/src/zope/app/publisher/browser/tests/test_browserlanguages.py	2005-10-18 14:46:01 UTC (rev 39496)
@@ -1,65 +1,92 @@
-##############################################################################
-#
-# Copyright (c) 2001, 2002 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.
-#
-##############################################################################
-"""Test Modifiable Browser Languages detector
-
-$Id$
-"""
-import unittest
-
-from zope.interface import directlyProvides
-from zope.publisher.tests.test_browserlanguages import BrowserLanguagesTest
-from zope.publisher.tests.test_browserlanguages import TestRequest
-
-from zope.app.testing import ztapi
-from zope.app.testing.placelesssetup import PlacelessSetup
-from zope.app.annotation import IAttributeAnnotatable, IAnnotations
-from zope.app.annotation.attribute import AttributeAnnotations
-from zope.app.publisher.browser import ModifiableBrowserLanguages
-
-
-class ModifiableBrowserLanguagesTests(PlacelessSetup, BrowserLanguagesTest):
-
-    def setUp(self):
-        super(ModifiableBrowserLanguagesTests, self).setUp()
-        ztapi.provideAdapter(IAttributeAnnotatable, IAnnotations,
-            AttributeAnnotations)
-
-    def factory(self, request):
-        directlyProvides(request, IAttributeAnnotatable)
-        return ModifiableBrowserLanguages(request)
-
-    def test_setPreferredLanguages(self):
-        browser_languages = self.factory(TestRequest("da, en, pt"))
-        self.assertEqual(list(browser_languages.getPreferredLanguages()),
-            ["da", "en", "pt"])
-        browser_languages.setPreferredLanguages(["ru", "en"])
-        self.assertEqual(list(browser_languages.getPreferredLanguages()),
-            ["ru", "en"])
-
-    def test_cached_languages(self):
-        request = TestRequest("da, en, pt")
-        browser_languages = self.factory(request)
-        self.assertEqual(list(browser_languages.getPreferredLanguages()),
-            ["da", "en", "pt"])
-        request["HTTP_ACCEPT_LANGUAGE"] = "ru, en"
-        self.assertEqual(list(browser_languages.getPreferredLanguages()),
-            ["da", "en", "pt"])
-
-
-def test_suite():
-    loader=unittest.TestLoader()
-    return loader.loadTestsFromTestCase(ModifiableBrowserLanguagesTests)
-
-if __name__=='__main__':
-    unittest.TextTestRunner().run(test_suite())
+##############################################################################
+#
+# Copyright (c) 2001, 2002 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.
+#
+##############################################################################
+"""Test Modifiable Browser Languages detector
+
+$Id$
+"""
+import unittest
+
+from zope.interface import directlyProvides
+from zope.publisher.browser import BrowserLanguages
+from zope.publisher.tests.test_browserlanguages import BrowserLanguagesTest
+
+from zope.app.testing import ztapi
+from zope.app.testing.placelesssetup import PlacelessSetup
+from zope.app.annotation import IAttributeAnnotatable, IAnnotations
+from zope.app.annotation.attribute import AttributeAnnotations
+
+from zope.app.publisher.browser import CacheableBrowserLanguages
+from zope.app.publisher.browser import ModifiableBrowserLanguages
+from zope.app.publisher.browser import NotCompatibleAdapterError
+
+
+class TestRequest(dict):
+
+    def __init__(self, languages):
+        self.localized = False
+        self["HTTP_ACCEPT_LANGUAGE"] = languages
+
+    def setupLocale(self):
+        self.localized = True
+
+class CacheableBrowserLanguagesTests(PlacelessSetup, BrowserLanguagesTest):
+
+    def setUp(self):
+        super(CacheableBrowserLanguagesTests, self).setUp()
+        ztapi.provideAdapter(IAttributeAnnotatable, IAnnotations,
+            AttributeAnnotations)
+
+    def factory(self, request):
+        directlyProvides(request, IAttributeAnnotatable)
+        return CacheableBrowserLanguages(request)
+
+    def test_cached_languages(self):
+        eq = self.failUnlessEqual
+        request = TestRequest("da, en, pt")
+        browser_languages = self.factory(request)
+        eq(list(browser_languages.getPreferredLanguages()), ["da", "en", "pt"])
+        request["HTTP_ACCEPT_LANGUAGE"] = "ru, en"
+        eq(list(browser_languages.getPreferredLanguages()), ["da", "en", "pt"])
+
+class ModifiableBrowserLanguagesTests(CacheableBrowserLanguagesTests):
+
+    def factory(self, request):
+        directlyProvides(request, IAttributeAnnotatable)
+        return ModifiableBrowserLanguages(request)
+
+    def test_setPreferredLanguages(self):
+        eq = self.failUnlessEqual
+        request = TestRequest("da, en, pt")
+        browser_languages = self.factory(request)
+        eq(list(browser_languages.getPreferredLanguages()), ["da", "en", "pt"])
+        browser_languages.setPreferredLanguages(["ru", "en"])
+        self.failUnless(request.localized)
+        eq(list(browser_languages.getPreferredLanguages()), ["ru", "en"])
+
+    def test_conflicting_adapters(self):
+        request = TestRequest("da, en, pt")
+        not_compatible_browser_languages = BrowserLanguages(request)
+        browser_languages = self.factory(request)
+        self.assertRaises(NotCompatibleAdapterError,
+            browser_languages.setPreferredLanguages, ["ru", "en"])
+
+
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(CacheableBrowserLanguagesTests))
+    suite.addTest(unittest.makeSuite(ModifiableBrowserLanguagesTests))
+    return suite
+
+if __name__=='__main__':
+    unittest.TextTestRunner().run(test_suite())



More information about the Zope3-Checkins mailing list