Added gallery content checker
authorThierry Florac <thierry.florac@onf.fr>
Fri, 10 Nov 2017 11:53:47 +0100
changeset 252 2dafc720b378
parent 251 6328755f08a5
child 253 80837cd25447
Added gallery content checker
src/pyams_content/component/gallery/__init__.py
src/pyams_content/component/gallery/file.py
src/pyams_content/component/gallery/paragraph.py
--- a/src/pyams_content/component/gallery/__init__.py	Fri Nov 10 11:53:18 2017 +0100
+++ b/src/pyams_content/component/gallery/__init__.py	Fri Nov 10 11:53:47 2017 +0100
@@ -18,8 +18,10 @@
 # import interfaces
 from pyams_content.component.gallery.interfaces import IGallery, IGalleryTarget, \
     GALLERY_CONTAINER_KEY, IGalleryRenderer
+from pyams_content.features.checker.interfaces import IContentChecker
 from pyams_content.shared.common.interfaces import IWfSharedContent
 from pyams_form.interfaces.form import IFormContextPermissionChecker
+from pyams_i18n.interfaces import II18n
 from zope.annotation.interfaces import IAnnotations
 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
 from zope.location.interfaces import ISublocations
@@ -27,6 +29,7 @@
 
 # import packages
 from pyams_catalog.utils import index_object
+from pyams_content.features.checker import BaseContentChecker
 from pyams_utils.adapter import adapter_config, ContextAdapter
 from pyams_utils.container import BTreeOrderedContainer
 from pyams_utils.request import check_request
@@ -40,6 +43,8 @@
 from zope.schema.fieldproperty import FieldProperty
 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
+from pyams_content import _
+
 
 #
 # Galleries container
@@ -132,6 +137,34 @@
         get_current_registry().notify(ObjectModifiedEvent(content))
 
 
+@adapter_config(name='gallery', context=IGallery, provides=IContentChecker)
+class GalleryContentChecker(BaseContentChecker):
+    """Gallery content checker"""
+
+    label = _("Gallery")
+    sep = '\n'
+    weight = 60
+
+    def inner_check(self, request):
+        output = []
+        registry = request.registry
+        for image in IGallery(self.context).values():
+            if not image.visible:
+                continue
+            for name, checker in sorted(registry.getAdapters((image, ), IContentChecker),
+                                        key=lambda x: x[1].weight):
+                output.append('- {0} : '.format(checker.label or
+                                                II18n(image).query_attribute('title', request=request)))
+                output.append(checker.get_check_output(request))
+        return output
+
+
+@adapter_config(name='gallery', context=IGalleryTarget, provides=IContentChecker)
+def GalleryTargetContentChecker(context):
+    gallery = IGallery(context)
+    return IContentChecker(gallery, None)
+
+
 @vocabulary_config(name='PyAMS gallery renderers')
 class GalleryRendererVocabulary(SimpleVocabulary):
     """Gallery renderer utilities vocabulary"""
--- a/src/pyams_content/component/gallery/file.py	Fri Nov 10 11:53:18 2017 +0100
+++ b/src/pyams_content/component/gallery/file.py	Fri Nov 10 11:53:47 2017 +0100
@@ -17,15 +17,20 @@
 
 # import interfaces
 from pyams_content.component.gallery.interfaces import IGalleryFile
+from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
 from pyams_content.shared.common.interfaces import IWfSharedContent
 from pyams_file.interfaces import DELETED_FILE, IResponsiveImage
 from pyams_form.interfaces.form import IFormContextPermissionChecker
+from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
 
 # import packages
 from persistent import Persistent
+from pyams_content.features.checker import BaseContentChecker
 from pyams_file.property import FileProperty
 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 pyramid.events import subscriber
 from pyramid.threadlocal import get_current_registry
@@ -98,3 +103,38 @@
     content = get_parent(event.object, IWfSharedContent)
     if content is not None:
         get_current_registry().notify(ObjectModifiedEvent(content))
+
+
+@adapter_config(context=IGalleryFile, provides=IContentChecker)
+class GalleryFileContentChecker(BaseContentChecker):
+    """Gallery file content checker"""
+
+    @property
+    def label(self):
+        request = check_request()
+        return II18n(self.context).query_attribute('title', request) or self.context.data.filename
+
+    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, )
+        i18n = II18n(self.context)
+        for lang in langs:
+            for attr in ('title', 'alt_title', 'description'):
+                value = i18n.get_attribute(attr, lang, request)
+                if not value:
+                    field_title = translate(IGalleryFile[attr].title)
+                    if len(langs) == 1:
+                        output.append(translate(MISSING_VALUE).format(field=field_title))
+                    else:
+                        output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
+        for attr in ('data', 'author'):
+            value = getattr(self.context, attr)
+            if not value:
+                output.append(translate(MISSING_VALUE).format(field=translate(IGalleryFile[attr].title)))
+        return output
--- a/src/pyams_content/component/gallery/paragraph.py	Fri Nov 10 11:53:18 2017 +0100
+++ b/src/pyams_content/component/gallery/paragraph.py	Fri Nov 10 11:53:47 2017 +0100
@@ -18,11 +18,15 @@
 # import interfaces
 from pyams_content.component.gallery.interfaces import IGalleryParagraph
 from pyams_content.component.paragraph.interfaces import IParagraphFactory
+from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
+from pyams_i18n.interfaces import II18n, INegotiator, II18nManager
 
 # import packages
 from pyams_content.component.gallery import Gallery as BaseGallery
-from pyams_content.component.paragraph import BaseParagraph
-from pyams_utils.registry import utility_config
+from pyams_content.component.paragraph import BaseParagraph, BaseParagraphContentChecker
+from pyams_utils.adapter import adapter_config
+from pyams_utils.registry import utility_config, get_utility
+from pyams_utils.traversing import get_parent
 from zope.interface import implementer
 
 from pyams_content import _
@@ -42,3 +46,28 @@
 
     name = _("Images gallery")
     content_type = Gallery
+
+
+@adapter_config(context=IGalleryParagraph, provides=IContentChecker)
+class GalleryParagraphContentChecker(BaseParagraphContentChecker):
+    """Gallery paragraph content checker"""
+
+    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, )
+        i18n = II18n(self.context)
+        for lang in langs:
+            value = i18n.get_attribute('title', lang, request)
+            if not value:
+                field_title = translate(IGalleryParagraph['title'].title)
+                if len(langs) == 1:
+                    output.append(translate(MISSING_VALUE).format(field=field_title))
+                else:
+                    output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
+        return output