Added content checker
authorThierry Florac <thierry.florac@onf.fr>
Fri, 10 Nov 2017 11:52:17 +0100
changeset 249 990a1e45f07c
parent 248 381f392c6edb
child 250 104e2c370c76
Added content checker
src/pyams_content/component/illustration/__init__.py
src/pyams_content/component/illustration/paragraph.py
--- a/src/pyams_content/component/illustration/__init__.py	Fri Nov 10 11:51:55 2017 +0100
+++ b/src/pyams_content/component/illustration/__init__.py	Fri Nov 10 11:52:17 2017 +0100
@@ -18,8 +18,9 @@
 # import interfaces
 from pyams_content.component.illustration.interfaces import IIllustrationRenderer, IIllustration, IIllustrationTarget, \
     ILLUSTRATION_KEY
+from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
 from pyams_file.interfaces import DELETED_FILE, IResponsiveImage, IFileInfo
-from pyams_i18n.interfaces import INegotiator, II18n
+from pyams_i18n.interfaces import INegotiator, II18n, II18nManager
 from zope.annotation.interfaces import IAnnotations
 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent
 from zope.location.interfaces import ISublocations
@@ -27,10 +28,12 @@
 
 # import packages
 from persistent import Persistent
+from pyams_content.features.checker import BaseContentChecker
 from pyams_i18n.property import I18nFileProperty
 from pyams_utils.adapter import adapter_config, ContextAdapter
-from pyams_utils.registry import query_utility
+from pyams_utils.registry import query_utility, get_utility
 from pyams_utils.request import check_request
+from pyams_utils.traversing import get_parent
 from pyams_utils.vocabulary import vocabulary_config
 from pyramid.events import subscriber
 from pyramid.threadlocal import get_current_registry
@@ -41,6 +44,8 @@
 from zope.schema.fieldproperty import FieldProperty
 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
+from pyams_content import _
+
 
 @implementer(IIllustration)
 class Illustration(Persistent, Contained):
@@ -129,6 +134,55 @@
         return IIllustration(self.context),
 
 
+@adapter_config(context=IIllustration, provides=IContentChecker)
+class IllustrationContentChecker(BaseContentChecker):
+    """Illustration content checker"""
+
+    label = _("Illustration")
+    weight = 40
+
+    def inner_check(self, request):
+        output = []
+        translate = request.localizer.translate
+        manager = get_parent(self.context, II18nManager)
+        if manager is not None:
+            langs = manager.get_languages()
+        else:
+            negotiator = get_utility(INegotiator)
+            langs = (negotiator.server_language, )
+        missing_value = translate(MISSING_VALUE)
+        missing_lang_value = translate(MISSING_LANG_VALUE)
+        i18n = II18n(self.context)
+        has_data = False
+        for attr in ('title', 'alt_title', 'description'):
+            for lang in langs:
+                data = i18n.get_attribute('data', lang, request)
+                if not data:
+                    continue
+                has_data = True
+                value = i18n.get_attribute(attr, lang, request)
+                if not value:
+                    if len(langs) == 1:
+                        output.append(missing_value.format(field=translate(IIllustration[attr].title)))
+                    else:
+                        output.append(missing_lang_value.format(field=translate(IIllustration[attr].title),
+                                                                lang=lang))
+        if has_data:
+            for attr in ('author', 'filename'):
+                value = getattr(self.context, attr)
+                if not value:
+                    output.append(missing_value.format(field=translate(IIllustration[attr].title)))
+        return output
+
+
+@adapter_config(name='illustration', context=IIllustrationTarget, provides=IContentChecker)
+def IllustrationTargetContentChecker(context):
+    """Illustration target content checker"""
+    illustration = IIllustration(context, None)
+    if illustration is not None:
+        return IContentChecker(illustration)
+
+
 @vocabulary_config(name='PyAMS illustration renderers')
 class IllustrationRendererVocabulary(SimpleVocabulary):
     """Illustration renderer utilities vocabulary"""
--- a/src/pyams_content/component/illustration/paragraph.py	Fri Nov 10 11:51:55 2017 +0100
+++ b/src/pyams_content/component/illustration/paragraph.py	Fri Nov 10 11:52:17 2017 +0100
@@ -18,11 +18,15 @@
 # import interfaces
 from pyams_content.component.illustration.interfaces import IIllustrationParagraph
 from pyams_content.component.paragraph.interfaces import IParagraphFactory
+from pyams_content.features.checker.interfaces import IContentChecker
+from pyams_i18n.interfaces import II18n
 
 # import packages
-from pyams_content.component.illustration import Illustration as BaseIllustration
+from pyams_content.component.illustration import Illustration as BaseIllustration, IllustrationContentChecker
 from pyams_content.component.paragraph import BaseParagraph
+from pyams_utils.adapter import adapter_config
 from pyams_utils.registry import utility_config
+from pyams_utils.request import check_request
 from zope.interface import implementer
 
 from pyams_content import _
@@ -42,3 +46,15 @@
 
     name = _("Illustration")
     content_type = Illustration
+
+
+@adapter_config(context=IIllustrationParagraph, provides=IContentChecker)
+class IllustrationParagraphContentChecker(IllustrationContentChecker):
+    """Illustration paragraph content checker"""
+
+    @property
+    def label(self):
+        request = check_request()
+        translate = request.localizer.translate
+        return II18n(self.context).query_attribute('title', request) or \
+            '({0})'.format(translate(self.context.icon_hint).lower())