Don't execute search to build single reference widget value map when value value is empty
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Mar 2019 14:05:01 +0100
changeset 90 afd059b0f037
parent 89 9ea651001f31
child 91 eba785a27a37
Don't execute search to build single reference widget value map when value value is empty
src/pyams_sequence/widget/__init__.py
--- a/src/pyams_sequence/widget/__init__.py	Fri Jan 18 15:40:51 2019 +0100
+++ b/src/pyams_sequence/widget/__init__.py	Mon Mar 11 14:05:01 2019 +0100
@@ -12,17 +12,28 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
 import json
 
-# import interfaces
+from hypatia.catalog import CatalogQuery
 from hypatia.interfaces import ICatalog
+from hypatia.query import Any, Eq
+from z3c.form.browser.widget import HTMLInputWidget
+from z3c.form.converter import BaseDataConverter
+from z3c.form.interfaces import IDataConverter, IFieldWidget
+from z3c.form.widget import FieldWidget, Widget
+from zope.interface import implementer_only
+
+from pyams_catalog.query import CatalogResultSet
 from pyams_form.interfaces.form import IFormLayer
+from pyams_form.widget import widgettemplate_config
 from pyams_i18n.interfaces import II18n
-from pyams_sequence.interfaces import ISequentialIntIds, ISequentialIdInfo
+from pyams_sequence.interfaces import ISequentialIdInfo, ISequentialIntIds
+from pyams_sequence.reference import get_last_version
 from pyams_sequence.schema import IInternalReferenceField, IInternalReferencesListField
 from pyams_sequence.widget.interfaces import IInternalReferenceWidget, IInternalReferencesListWidget
+from pyams_utils.adapter import adapter_config
+from pyams_utils.registry import get_utility
+
 
 try:
     from pyams_workflow.interfaces import IWorkflowVersions, IWorkflowVersion, IWorkflowManagedContent
@@ -31,21 +42,6 @@
 else:
     handle_workflow = True
 
-from z3c.form.interfaces import IFieldWidget, IDataConverter
-
-# import packages
-from hypatia.catalog import CatalogQuery
-from hypatia.query import Eq, Any
-from pyams_catalog.query import CatalogResultSet
-from pyams_form.widget import widgettemplate_config
-from pyams_sequence.reference import get_last_version
-from pyams_utils.adapter import adapter_config
-from pyams_utils.registry import get_utility
-from z3c.form.browser.widget import HTMLInputWidget
-from z3c.form.converter import BaseDataConverter
-from z3c.form.widget import Widget, FieldWidget
-from zope.interface import implementer_only
-
 from pyams_sequence import _
 
 
@@ -68,16 +64,20 @@
     @property
     def values_map(self):
         catalog = get_utility(ICatalog)
-        params = Eq(catalog['oid'], self.value)
-        results = list(map(get_last_version, CatalogResultSet(CatalogQuery(catalog).query(params))))
-        if results:
-            sequence = get_utility(ISequentialIntIds)
-            return json.dumps({self.value: '{0} ({1})'.format(II18n(results[0]).query_attribute('title',
-                                                                                                request=self.request),
-                                                              sequence.get_short_oid(ISequentialIdInfo(results[0]).oid))})
+        if self.value:
+            params = Eq(catalog['oid'], self.value)
+            results = list(map(get_last_version, CatalogResultSet(CatalogQuery(catalog).query(params))))
+            if results:
+                sequence = get_utility(ISequentialIntIds)
+                return json.dumps({self.value: '{0} ({1})'.format(II18n(results[0]).query_attribute('title',
+                                                                                                    request=self.request),
+                                                                  sequence.get_short_oid(
+                                                                      ISequentialIdInfo(results[0]).oid))})
+            else:
+                translate = self.request.localizer.translate
+                return json.dumps({self.value: translate(_("missing reference: {0}")).format(self.value)})
         else:
-            translate = self.request.localizer.translate
-            return json.dumps({self.value: translate(_("missing reference: {0}")).format(self.value)})
+            return json.dumps({})
 
 
 @adapter_config(context=(IInternalReferenceField, IFormLayer), provides=IFieldWidget)