Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding catalog patch.

  • Loading branch information...
commit 3173b7ce07176ba4b88c6ad10288cb5d6e2494f1 1 parent f7cc1dc
Malthe Borch malthe authored
8 README.rst
View
@@ -144,14 +144,6 @@ Plone when cookie-based language selection is enabled (see the
language tool for more information).
-To-Do
------
-
-There are some features that are missing at this point:
-
-- Integration with Plone's search user interface and collections.
-
-
History
=======
1  src/collective/multilingual/__init__.py
View
@@ -0,0 +1 @@
+from . import patches
55 src/collective/multilingual/patches.py
View
@@ -0,0 +1,55 @@
+from Products.CMFPlone.CatalogTool import CatalogTool
+from Products.CMFCore.utils import getToolByName
+
+from .utils import logger
+from .interfaces import IBrowserLayer
+
+_searchResults = CatalogTool.searchResults
+_no_filter = set(('UID', 'id', 'getId'))
+
+
+def applyLanguageFilter(site, query):
+ # Accept both capitalized and lowercase language parameter.
+ language = query.pop('language', None) or query.pop('Language', None)
+ if language is not None:
+ # The string 'all' short-circuits.
+ if language != 'all':
+ query['language'] = language
+
+ return
+
+ if set(query) & _no_filter:
+ return
+
+ lt = getToolByName(site, 'portal_languages', None)
+ if lt is None:
+ return
+
+ language = lt.getPreferredLanguage()
+ if language == lt.getDefaultLanguage():
+ language = (language, u"")
+
+ query['language'] = language
+
+ # XXX: For path queries that target a path under a language
+ # folder, and if we want to support a list of (language-neutral)
+ # shared or common folders, this would be the place to customize
+ # the path query to include these. This might be an images folder.
+
+
+def searchResults(self, REQUEST=None, **kw):
+ for req in (REQUEST, getattr(self, "REQUEST", None)):
+ if req is not None and IBrowserLayer.providedBy(req):
+ if REQUEST is not None and kw.get('Language', '') != 'all':
+ query = REQUEST
+ else:
+ query = kw
+
+ applyLanguageFilter(self, query)
+ break
+
+ return _searchResults(self, REQUEST, **kw)
+
+CatalogTool.searchResults = searchResults
+CatalogTool.__call__ = searchResults
+logger.info("patched catalog tool to filter on current language.")
4 src/collective/multilingual/testing.py
View
@@ -35,11 +35,11 @@ def setUpContent(self, portal):
# 2. Create language folders /da and /de:
danish = createContentInContainer(
- portal, "Container", id="da", language=u"da",
+ portal, "Container", id="da", language="da",
)
german = createContentInContainer(
- portal, "Container", id="de", language=u"de",
+ portal, "Container", id="de", language="de",
)
from plone.app.layout.navigation.interfaces import INavigationRoot
76 src/collective/multilingual/tests/test_patches.py
View
@@ -0,0 +1,76 @@
+import unittest2 as unittest
+
+from ..testing import INTEGRATION_TESTING
+
+
+class TestCatalogPatch(unittest.TestCase):
+ layer = INTEGRATION_TESTING
+
+ def setUp(self):
+ from zope.event import notify
+ from zope.traversing.interfaces import BeforeTraverseEvent
+
+ # Manually set up the browser layer, see:
+ # https://dev.plone.org/ticket/11673
+ notify(BeforeTraverseEvent(
+ self.layer['portal'],
+ self.layer['request']
+ ))
+
+ @property
+ def catalog(self):
+ return self.layer['portal'].portal_catalog
+
+ def setLanguage(self, language_id):
+ from Products.CMFPlone.interfaces import IPloneSiteRoot
+ from Products.PloneLanguageTool.interfaces import INegotiateLanguage
+ from collective.multilingual.interfaces import IBrowserLayer
+
+ site = self.layer['portal']
+ assert language_id != "en"
+
+ class NegotiateLanguage(object):
+ def __init__(self, site, request):
+ self.default_language = "en"
+ self.language = language_id
+ self.language_list = ["en", language_id]
+
+ site.getSiteManager().registerAdapter(
+ NegotiateLanguage,
+ (IPloneSiteRoot, IBrowserLayer),
+ INegotiateLanguage
+ )
+
+ from Products.CMFCore.utils import getToolByName
+ lt = getToolByName(site, 'portal_languages', None)
+
+ if lt is not None:
+ lt.setLanguageBindings()
+
+ self.assertEqual(
+ language_id,
+ lt.getPreferredLanguage(),
+ )
+
+ def test_callable(self):
+ result = self.catalog()
+ languages = set(brain.language for brain in result)
+ self.assertEqual(languages, set([""]))
+
+ def test_all(self):
+ result = self.catalog(language='all')
+ languages = set(brain.language for brain in result)
+ self.assertEqual(languages, set(["", "da", "de", "es"]))
+
+ def test_search_language(self):
+ result = self.catalog(language="da")
+ languages = set(brain.language for brain in result)
+ self.assertTrue(result)
+ self.assertEqual(languages, set(["da"]))
+
+ def test_search_current_language(self):
+ self.setLanguage("da")
+ result = self.catalog()
+ languages = set(brain.language for brain in result)
+ self.assertTrue(result)
+ self.assertEqual(languages, set(["da"]))
Please sign in to comment.
Something went wrong with that request. Please try again.