Skip to content

Commit

Permalink
fix fallback mechanism in case 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Jul 12, 2019
1 parent 2b18895 commit 9d2e783
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
35 changes: 22 additions & 13 deletions src/plone/app/multilingualindexes/languagefallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from plone import api
from plone.app.multilingual.events import ITranslationRegisteredEvent
from plone.app.multilingual.interfaces import ITG
from plone.app.multilingual.interfaces import ITranslatable
from plone.app.multilingual.interfaces import ITranslationManager
from plone.app.multilingualindexes.utils import get_configuration
from plone.indexer.interfaces import IIndexableObject
from Products.CMFCore.indexing import processQueue
from Products.CMFPlone.utils import safe_hasattr
from Products.DateRecurringIndex.index import DateRecurringIndex
from Products.PluginIndexes.common.UnIndex import UnIndex
Expand Down Expand Up @@ -64,8 +64,8 @@ def _catalog(self):
"LanguageFallbackIndex cant work w/o knowing about its catalog"
)

def get_primary_languages_of_fallback(self, lang):
"""Iterator returning primary languages for a given fallback
def primary_languages_of_fallback(self, lang):
"""Iterator of primary languages for a given fallback
"""
for primary_lang, fallbacks in get_configuration().items():
if lang in fallbacks:
Expand Down Expand Up @@ -100,6 +100,8 @@ def index_object(
self, documentId, obj, threshold=None, recursive=True
): # noqa: C901
res = False
if not ITranslatable.providedBy(obj):
return res
# Start handling the language of the object itself
obj_lang = getattr(aq_base(obj), "Language", _marker) or _marker
if obj_lang is _marker:
Expand Down Expand Up @@ -147,27 +149,33 @@ def index_object(
tm = ITranslationManager(wrapped_obj, None)
if tm is None:
return res
# to be sure to have all translations in place, lets reindex the
# translationgroup for our document first
self.caller.getIndex("TranslationGroup").index_object(documentId, obj)

# proceed with creating fallback entries
translations = tm.get_translations()
translated_langs = list(translations.keys())
if obj_lang in translated_langs:
# it can happen, that our index gets called before the Language
# Index gets called. In that case, our document might not
# be registered yet as translated langs
translated_langs.remove(obj_lang)
# now we iterate over all possible primary languages and check if
# Check if the object is a possible fallback for some other language.
# Iterate over all possible primary languages and check if
# the current objects language is a possible fallback
for primary_lang in self.get_primary_languages_of_fallback(obj_lang):
# Add fallback entry, if all preconditions pass
for primary_lang in self.primary_languages_of_fallback(obj_lang):
if (
primary_lang in translated_langs
primary_lang in translated_langs # skip, we have a real translation
or self.has_translation_with_higher_prio_fallback(
primary_lang, obj_lang, translated_langs
)
) # skip, we have a better fallback
):
# No fallback needed or fallback with higher priority exists:
# remove fallback entry if exists
self._remove_docid_for_language(documentId, primary_lang)
continue
# Add fallback entry
self.insertForwardIndexEntry(primary_lang, documentId)
self._unindex[documentId].add(primary_lang)
res = True
Expand Down Expand Up @@ -249,13 +257,14 @@ def fallback_finder(context, row):
def reindex_languagefallback(event):
"""Object event subscriber to reindex the index 'language_or_fallback'.
"""
event.object.reindexObject(idxs=["language_or_fallback"])
if ITranslationRegisteredEvent.providedBy(event):
event.target.reindexObject(idxs=["language_or_fallback"])
other = event.target
else:
event.old_object.reindexObject(idxs=["language_or_fallback"])
# ensure this is done before any other indexers running
# processQueue()
other = event.old_object
annotate_documentid_to_tg(event.object)
annotate_documentid_to_tg(other)
event.object.reindexObject(idxs=["language_or_fallback"])
other.reindexObject(idxs=["language_or_fallback"])


def annotate_documentid_to_tg(obj):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_case_8_1(self):
"""
self.set_config(dict(ca=["en"], en=[], es=[]))
en_obj = self.make_obj("en")
self.assertEqual(1, len(self.search("ca")))
self.assertEqual(["/plone/en/test"], self.search("ca"))
ca_obj = self.make_obj("ca")
ITranslationManager(en_obj).register_translation("ca", ca_obj)
self.assertEqual(["/plone/ca/test"], self.search("ca"))
Expand Down

0 comments on commit 9d2e783

Please sign in to comment.