src/pyams_sequence/widget/__init__.py
changeset 0 980ffaa51a75
child 6 dd0072ed1543
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_sequence/widget/__init__.py	Wed Jun 17 09:59:43 2015 +0200
@@ -0,0 +1,83 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+import json
+
+# import interfaces
+from hypatia.interfaces import ICatalog
+from pyams_form.interfaces.form import IFormLayer
+from pyams_i18n.interfaces import II18n
+from pyams_sequence.interfaces import ISequentialIntIds, ISequentialIdInfo
+from pyams_sequence.schema import IInternalReference
+from pyams_sequence.widget.interfaces import IInternalReferenceWidget
+
+try:
+    from pyams_workflow.interfaces import IWorkflowVersions, IWorkflowVersion, IWorkflowManagedContent
+except ImportError:
+    handle_workflow = False
+else:
+    handle_workflow = True
+
+from z3c.form.interfaces import IFieldWidget
+
+# import packages
+from hypatia.catalog import CatalogQuery
+from hypatia.query import Eq
+from pyams_catalog.query import CatalogResultSet
+from pyams_form.widget import widgettemplate_config
+from pyams_sequence.utility 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.widget import Widget, FieldWidget
+from zope.interface import implementer_only
+
+from pyams_sequence import _
+
+
+#
+# Internal reference widget
+#
+
+@widgettemplate_config(mode='input', template='templates/reference-input.pt', layer=IFormLayer)
+@widgettemplate_config(mode='display', template='templates/reference-display.pt', layer=IFormLayer)
+@implementer_only(IInternalReferenceWidget)
+class InternalReferenceWidget(HTMLInputWidget, Widget):
+    """Internal reference widget"""
+
+    @property
+    def query_params(self):
+        return json.dumps({'content_type': self.field.content_type})
+
+    @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))})
+        else:
+            translate = self.request.localizer.translate
+            return json.dumps({self.value: translate(_("missing reference: {0}")).format(self.value)})
+
+
+@adapter_config(context=(IInternalReference, IFormLayer), provides=IFieldWidget)
+def InternalReferenceFieldWidget(field, request):
+    """Internal reference field widget factory"""
+    return FieldWidget(field, InternalReferenceWidget(request))