Skip to content

Commit

Permalink
Merge pull request #167 from collective/refactor-collectionfilter-mod…
Browse files Browse the repository at this point in the history
…ifier

Fix collectionfilter modifier
  • Loading branch information
petschki authored Oct 24, 2024
2 parents 4157b4c + 18992cf commit ab10ec7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ name: Test Collective Taxonomy

on:
push:
branches:
- master
pull_request:
branches:
- master
workflow_dispatch:

jobs:
Expand Down
5 changes: 3 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -14,7 +15,7 @@ Changes
[1letter]

- refactor JS resources and update README for better developer experience
[petschki
[petschki]

3.1.2 (2024-07-21)
------------------
Expand Down
45 changes: 22 additions & 23 deletions src/collective/taxonomy/collectionfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,42 @@
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)
@adapter(IGroupByCriteria)
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,
}

0 comments on commit ab10ec7

Please sign in to comment.