Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix collectionfilter modifier #167

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}
Loading