diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ee219b9..0d12d98 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,6 +2,11 @@ name: Test Collective Taxonomy on: push: + branches: + - master + pull_request: + branches: + - master workflow_dispatch: jobs: diff --git a/CHANGES.rst b/CHANGES.rst index fd45e14..e6276df 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,8 @@ Changes 3.1.4 (unreleased) ------------------ -- Nothing changed yet. +- Fix collectionfilter modifier when index names ended with the same string. + [petschki] 3.1.3 (2024-10-17) @@ -14,7 +15,7 @@ Changes [1letter] - refactor JS resources and update README for better developer experience - [petschki + [petschki] 3.1.2 (2024-07-21) ------------------ diff --git a/src/collective/taxonomy/collectionfilter.py b/src/collective/taxonomy/collectionfilter.py index f7e60f0..85185c7 100644 --- a/src/collective/taxonomy/collectionfilter.py +++ b/src/collective/taxonomy/collectionfilter.py @@ -4,29 +4,27 @@ from plone import api from plone.behavior.interfaces import IBehavior from zope.component import adapter -from zope.component import queryUtility from zope.component.hooks import getSite from zope.interface import implementer -def resolve_title(token, index_name): - sm = getSite().getSiteManager() - utilities = sm.getUtilitiesFor(ITaxonomy) - taxonomies = [uname for uname, util in utilities] - shortname = None - taxonomy_name = None - for tax in taxonomies: - shortname = tax[len("collective.taxonomy.") :] - if not index_name.endswith(shortname): - continue - taxonomy_name = tax - if not shortname or not taxonomy_name: - return token - taxonomy = queryUtility(ITaxonomy, name=taxonomy_name) - lang = api.portal.get_current_language() - lang = lang in taxonomy.inverted_data and lang or taxonomy.default_language - term = taxonomy.translate(token, target_language=lang) - return term +class TaxonomyLabel: + taxonomy = None + + def __init__(self, taxonomy): + self.taxonomy = taxonomy + + def display(self, token, *args, **kw): + if self.taxonomy is None: + return token + lang = api.portal.get_current_language() + lang = ( + lang in self.taxonomy.inverted_data + and lang + or self.taxonomy.default_language + ) + term = self.taxonomy.translate(token, target_language=lang) + return term @implementer(IGroupByModifier) @@ -34,13 +32,14 @@ def resolve_title(token, index_name): def groupby_modifier(groupby): sm = getSite().getSiteManager() utilities = sm.getUtilitiesFor(ITaxonomy) - for taxonomy in utilities: - behavior = sm.queryUtility(IBehavior, name=taxonomy[1].getGeneratedName()) + for uname, util in utilities: + behavior = sm.queryUtility(IBehavior, name=util.getGeneratedName()) taxonomy_field_prefix = behavior.field_prefix - taxonomy_shortname = taxonomy[1].getShortName() + taxonomy_shortname = util.getShortName() taxonomy_index_name = f"{taxonomy_field_prefix}{taxonomy_shortname}" + taxonomy_label = TaxonomyLabel(util) groupby._groupby[taxonomy_index_name] = { "index": taxonomy_index_name, "metadata": taxonomy_index_name, - "display_modifier": lambda it, idx: resolve_title(it, idx), + "display_modifier": taxonomy_label.display, }