Skip to content

Commit

Permalink
Layered listing searchable text adapter lookup
Browse files Browse the repository at this point in the history
Lookup listing searchable text adapter with request
  • Loading branch information
ramonski committed Oct 27, 2023
1 parent 47938df commit fd1a5d9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
21 changes: 18 additions & 3 deletions src/senaite/core/catalog/indexer/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from bika.lims.interfaces import IAnalysisRequest
from bika.lims.interfaces import IListingSearchableTextProvider
from plone.indexer import indexer
from senaite.core import logger
from senaite.core.catalog import SAMPLE_CATALOG
from senaite.core.interfaces import ISampleCatalog
from zope.component import getAdapters
Expand Down Expand Up @@ -118,10 +119,24 @@ def listing_searchable_text(instance):
batch = obj.getBatch()
entries.add(batch.getId() if batch else '')

catalog = api.get_tool(SAMPLE_CATALOG)
text_providers = getAdapters((instance, api.get_request(), catalog),
IListingSearchableTextProvider)
# BBB
bbb_text_providers = getAdapters((instance, catalog),
IListingSearchableTextProvider)

# combine the adapters for backwards compatibility
adapters = list(text_providers) + list(bbb_text_providers)

# Allow to extend search tokens via adapters
for name, adapter in getAdapters((instance, api.get_tool(SAMPLE_CATALOG)),
IListingSearchableTextProvider):
value = adapter()
for name, adapter in adapters:
try:
value = adapter()
except (AttributeError, TypeError, api.APIError) as exc:
logger.error(exc)
value = []

if isinstance(value, (list, tuple)):
values = map(api.to_searchable_text_metadata, value)
entries.update(values)
Expand Down
20 changes: 17 additions & 3 deletions src/senaite/core/catalog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from Products.CMFPlone.CatalogTool import \
sortable_title as plone_sortable_title
from Products.CMFPlone.utils import safe_callable
from senaite.core import logger
from zope.component import getAdapters


Expand Down Expand Up @@ -65,10 +66,23 @@ def get_searchable_text_tokens(instance, catalog_name,
field_value = api.to_searchable_text_metadata(field_value)
entries.add(field_value)

text_providers = getAdapters((instance, api.get_request(), catalog),
IListingSearchableTextProvider)
# BBB
bbb_text_providers = getAdapters((instance, catalog),
IListingSearchableTextProvider)

# combine the adapters for backwards compatibility
adapters = list(text_providers) + list(bbb_text_providers)

# Extend metadata entries with pluggable text providers
for name, adapter in getAdapters((instance, catalog),
IListingSearchableTextProvider):
value = adapter()
for name, adapter in adapters:
try:
value = adapter()
except (AttributeError, TypeError, api.APIError) as exc:
logger.error(exc)
value = []

if isinstance(value, (list, tuple)):
values = map(api.to_searchable_text_metadata, value)
entries.update(values)
Expand Down

0 comments on commit fd1a5d9

Please sign in to comment.