Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Add is_supported()
Browse files Browse the repository at this point in the history
  • Loading branch information
xispa committed Jul 7, 2020
1 parent 5cf36ab commit 98c1f7c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
30 changes: 18 additions & 12 deletions bika/health/adapters/referencewidget/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@

from bika.health import logger
from bika.health.utils import get_client_from_chain
from bika.health.utils import is_internal_client
from bika.health.utils import resolve_query_for_shareable
from bika.lims import api
from bika.lims.adapters.referencewidgetvocabulary import \
DefaultReferenceWidgetVocabulary
from bika.lims.interfaces import IClient
from bika.lims.interfaces import IReferenceWidgetVocabulary
from bika.lims.utils import chain
from bika.lims.utils import get_client


class IReferenceWidgetAdapter(IReferenceWidgetVocabulary):
"""Marker interface for reference widget adapters
"""

def is_supported(self):
"""Returns whether the current adapter supports the search
"""


class IExclusiveReferenceWidgetAdapter(IReferenceWidgetAdapter):
"""Marker interface for 'exclusive' reference widget adapters
Expand Down Expand Up @@ -87,6 +88,13 @@ class ClientAwareReferenceWidgetAdapter(DefaultReferenceWidgetVocabulary):
("Contact", "getParentUID"),
]

def is_supported(self):
"""Returns whether the adapter supports the portal_type from the query
"""
query = self.get_baseline_query()
portal_type = self.get_portal_type(query)
return portal_type in self.client_aware_types

def get_client_from_query(self, query, purge=False):
"""Resolves the client from the query passed-in
"""
Expand All @@ -103,24 +111,22 @@ def get_client_from_query(self, query, purge=False):
return client
return None

def get_baseline_query(self):
"""Returns the baseline query to work with
"""
return super(ClientAwareReferenceWidgetAdapter, self).get_raw_query()

def get_raw_query(self):
"""Returns the raw query to use for current search, based on the
base query + update query
"""
query = super(ClientAwareReferenceWidgetAdapter, self).get_raw_query()
query = self.get_baseline_query()
logger.info("===============================================")
logger.info("Custom client-aware reference widget vocabulary")
logger.info(repr(query))

# Get the portal types from the query
# Get the portal type from the query
portal_type = self.get_portal_type(query)
if not portal_type:
# Don't know the type we are searching for, do nothing
return query

if portal_type not in self.client_aware_types:
# The portal type is not client aware, do nothing
return query

# Try to resolve the client from the query
client = self.get_client_from_query(query, purge=True)
Expand Down
29 changes: 20 additions & 9 deletions bika/health/browser/referencewidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,35 @@ def search(self):
"""Returns the list of brains that match with the request criteria
"""
params = (self.context, self.request)
adapters = list(getAdapters(params, IReferenceWidgetAdapter))
adapters = list(getAdapters(params, IReferenceWidgetAdapter)) or []

# Convert adapters to a list (don't need the name)
adapters = map(lambda ad: ad[1], adapters)

# Filter supported adapters
adapters = filter(lambda ad: ad.is_supported(), adapters)

if not adapters:
# No health-specific adapters found, fallback to core's defaults
# Fallback to core's defaults
return super(ajaxReferenceWidgetSearch, self).search()

# Do not consider other adapters when an "exclusive" adapter is found
exclusive = filter(lambda ad:
IExclusiveReferenceWidgetAdapter.providedBy(ad[1]),
exclusive = filter(lambda adapter:
IExclusiveReferenceWidgetAdapter.providedBy(adapter),
adapters)

if exclusive:
if len(exclusive) > 1:
logger.error("Multiple exclusive adapters found!")
return []
if exclusive and len(exclusive) > 1:
logger.error("Multiple exclusive adapters found!")
# We do not fallback to core's default. We return empty to prevent
# inconsistencies
return []

elif exclusive:
# Discard other adapters
adapters = [exclusive[0]]

brains = []
for name, adapter in adapters:
for adapter in adapters:
brains.extend(adapter())

return brains

0 comments on commit 98c1f7c

Please sign in to comment.