Use PyAMS_sequence 'get_reference_target' function
authorThierry Florac <thierry.florac@onf.fr>
Tue, 12 Sep 2017 16:42:12 +0200
changeset 149 128e447b6a7f
parent 148 e4b89f5fb5ea
child 150 90a1cb45caf8
Use PyAMS_sequence 'get_reference_target' function
src/pyams_content/component/links/__init__.py
--- a/src/pyams_content/component/links/__init__.py	Tue Sep 12 16:41:24 2017 +0200
+++ b/src/pyams_content/component/links/__init__.py	Tue Sep 12 16:42:12 2017 +0200
@@ -16,21 +16,16 @@
 # import standard library
 
 # import interfaces
-from hypatia.interfaces import ICatalog
 from pyams_content.component.association.interfaces import IAssociationInfo, IAssociationTarget, IAssociationContainer
 from pyams_content.component.links.interfaces import IBaseLink, IInternalLink, IExternalLink, IMailtoLink
 from pyams_i18n.interfaces import II18n
 from pyams_sequence.interfaces import ISequentialIdInfo
 
 # import packages
-from hypatia.catalog import CatalogQuery
-from hypatia.query import Eq, Any
-from pyams_catalog.query import CatalogResultSet
 from pyams_content.component.association import AssociationItem
 from pyams_content.workflow import VISIBLE_STATES
-from pyams_sequence.utility import get_last_version
+from pyams_sequence.utility import get_reference_target
 from pyams_utils.adapter import adapter_config, ContextAdapter
-from pyams_utils.registry import get_utility
 from pyams_utils.request import check_request
 from pyams_utils.traversing import get_parent
 from pyams_utils.url import absolute_url
@@ -93,19 +88,7 @@
     reference = FieldProperty(IInternalLink['reference'])
 
     def get_target(self, state=None):
-        catalog = get_utility(ICatalog)
-        params = Eq(catalog['oid'], self.reference)
-        if state:
-            if not isinstance(state, (list, tuple)):
-                state = (state, )
-            params &= Any(catalog['workflow_state'], state)
-            results = list(CatalogResultSet(CatalogQuery(catalog).query(params)))
-            if results:
-                return results[0]
-        else:
-            results = list(map(get_last_version, CatalogResultSet(CatalogQuery(catalog).query(params))))
-            if results:
-                return results[0]
+        return get_reference_target(self.reference, state)
 
     def get_editor_url(self):
         return 'oid://{0}'.format(self.reference)