Added imagemap 'preview' and 'review' features support, and added form content checker
authorThierry Florac <thierry.florac@onf.fr>
Fri, 10 Nov 2017 12:01:18 +0100
changeset 265 4ac7ac893ff8
parent 264 1ee1a79dd347
child 266 16ff75a093d8
Added imagemap 'preview' and 'review' features support, and added form content checker
src/pyams_content/shared/imagemap/__init__.py
--- a/src/pyams_content/shared/imagemap/__init__.py	Fri Nov 10 12:00:48 2017 +0100
+++ b/src/pyams_content/shared/imagemap/__init__.py	Fri Nov 10 12:01:18 2017 +0100
@@ -9,7 +9,6 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-from pyams_content.component.association.interfaces import IAssociationContainer
 
 __docformat__ = 'restructuredtext'
 
@@ -19,16 +18,21 @@
 from persistent.mapping import PersistentMapping
 
 # import interfaces
+from pyams_content.component.association.interfaces import IAssociationContainer
 from pyams_content.component.extfile.interfaces import IExtFileContainerTarget
 from pyams_content.component.links.interfaces import ILinkContainerTarget
+from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE, ERROR_VALUE
+from pyams_content.features.preview.interfaces import IPreviewTarget
+from pyams_content.features.review.interfaces import IReviewTarget
 from pyams_content.shared.imagemap.interfaces import IMAGEMAP_CONTENT_TYPE, IMAGEMAP_CONTENT_NAME, \
     IWfImageMap, IImageMap, IImageMapArea
+from pyams_i18n.interfaces import II18n, II18nManager
 from z3c.form.interfaces import NOT_CHANGED
 from zope.location.interfaces import ISublocations
 from zope.traversing.interfaces import ITraversable
 
 # import packages
-from pyams_content.shared.common import WfSharedContent, register_content_type, SharedContent
+from pyams_content.shared.common import WfSharedContent, register_content_type, SharedContent, WfSharedContentChecker
 from pyams_i18n.property import I18nFileProperty
 from pyams_utils.adapter import adapter_config, ContextAdapter
 from pyramid.threadlocal import get_current_registry
@@ -38,6 +42,8 @@
 from zope.location import locate
 from zope.schema.fieldproperty import FieldProperty
 
+from pyams_content import _
+
 
 @implementer(IImageMapArea)
 class ImageMapArea(Persistent, Contained):
@@ -48,7 +54,8 @@
     area = FieldProperty(IImageMapArea['area'])
 
 
-@implementer(IWfImageMap, IExtFileContainerTarget, ILinkContainerTarget)
+@implementer(IWfImageMap, IExtFileContainerTarget, ILinkContainerTarget,
+             IPreviewTarget, IReviewTarget)
 class WfImageMap(WfSharedContent):
     """Base image map"""
 
@@ -116,3 +123,28 @@
 
     def sublocations(self):
         return self.context.areas.values()
+
+
+@adapter_config(name='properties', context=IWfImageMap, provides=IContentChecker)
+class WfImageMapContentChecker(WfSharedContentChecker):
+    """Image map content checker"""
+
+    def inner_check(self, request):
+        output = super(WfImageMapContentChecker, self).inner_check(request)
+        translate = request.localizer.translate
+        missing_value = translate(MISSING_VALUE)
+        missing_lang_value = translate(MISSING_LANG_VALUE)
+        langs = II18nManager(self.context).get_languages()
+        i18n = II18n(self.context)
+        for lang in langs:
+            data = i18n.get_attribute('image', lang, request)
+            if not data:
+                if len(langs) == 1:
+                    output.append(missing_value.format(field=translate(IWfImageMap['image'].title)))
+                else:
+                    output.append(missing_lang_value.format(field=translate(IWfImageMap['image'].title),
+                                                            lang=lang))
+        if not self.context.areas:
+            output.append(translate(ERROR_VALUE).format(field=translate(IWfImageMap['areas'].title),
+                                                        message=translate(_("no area defined"))))
+        return output