Added files content checkers
authorThierry Florac <thierry.florac@onf.fr>
Fri, 10 Nov 2017 11:55:35 +0100
changeset 254 eadd658a8416
parent 253 80837cd25447
child 255 38133975088e
Added files content checkers
src/pyams_content/component/extfile/__init__.py
--- a/src/pyams_content/component/extfile/__init__.py	Fri Nov 10 11:54:55 2017 +0100
+++ b/src/pyams_content/component/extfile/__init__.py	Fri Nov 10 11:55:35 2017 +0100
@@ -20,18 +20,19 @@
 from pyams_content.component.association.interfaces import IAssociationInfo
 from pyams_content.component.extfile.interfaces import IBaseExtFile, IExtFile, IExtImage, IExtVideo, IExtAudio, \
     IExtMedia
+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 IFileInfo, IResponsiveImage, DELETED_FILE
-from pyams_form.interfaces.form import IFormContextPermissionChecker
-from pyams_i18n.interfaces import II18n, INegotiator
+from pyams_i18n.interfaces import II18n, INegotiator, II18nManager
 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
 
 # import packages
 from pyams_content.component.association import AssociationItem
-from pyams_file.file import ImageFile, VideoFile, AudioFile, EXTENSIONS_THUMBNAILS
+from pyams_content.features.checker import BaseContentChecker
+from pyams_file.file import EXTENSIONS_THUMBNAILS
 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.size import get_human_size
 from pyams_utils.traversing import get_parent
@@ -86,11 +87,15 @@
 
     @property
     def user_title(self):
-        name, ext = os.path.splitext(self.context.filename)
-        return '{title} <img class="margin-left-5 align-top" ' \
-               'src="/--static--/pyams_skin/img/mimetypes/16x16/{thumb}" />'.format(
-            title=II18n(self.context).query_attribute('title') or self.context.filename,
-            thumb=EXTENSIONS_THUMBNAILS.get(ext, 'unknown.png'))
+        filename = self.context.filename
+        if filename:
+            name, ext = os.path.splitext(filename)
+            return '{title} <img class="margin-left-5 align-top" ' \
+                'src="/--static--/pyams_skin/img/mimetypes/16x16/{thumb}" />'.format(
+                 title=II18n(self.context).query_attribute('title') or filename,
+                 thumb=EXTENSIONS_THUMBNAILS.get(ext, 'unknown.png'))
+        else:
+            return '--'
 
     @property
     def inner_title(self):
@@ -105,16 +110,6 @@
             return '--'
 
 
-@adapter_config(context=IBaseExtFile, provides=IFormContextPermissionChecker)
-class BaseExtFilePermissionChecker(ContextAdapter):
-    """External file permission checker"""
-
-    @property
-    def edit_permission(self):
-        content = get_parent(self.context, IWfSharedContent)
-        return IFormContextPermissionChecker(content).edit_permission
-
-
 def update_properties(extfile):
     """Update missing file properties"""
     request = check_request()
@@ -177,6 +172,45 @@
 register_file_factory('file', ExtFile, _("Standard file"))
 
 
+@adapter_config(context=IExtFile, provides=IContentChecker)
+class ExtFileContentChecker(BaseContentChecker):
+    """Base external file 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())
+
+    interface = IExtFile
+
+    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 attr in ('title', 'data'):
+            for lang in langs:
+                value = i18n.get_attribute(attr, lang, request)
+                if not value:
+                    if len(langs) > 1:
+                        output.append(translate(MISSING_LANG_VALUE).format(field=translate(self.interface[attr].title),
+                                                                           lang=lang))
+                    else:
+                        output.append(translate(MISSING_VALUE).format(field=translate(self.interface[attr].title)))
+        for attr in ('author', 'filename'):
+            value = getattr(self.context, attr)
+            if not value:
+                output.append(translate(MISSING_VALUE).format(field=translate(self.interface[attr].title)))
+        return output
+
+
 @implementer(IExtImage)
 class ExtImage(BaseExtFile):
     """External image persistent class"""
@@ -202,6 +236,13 @@
 register_file_factory('image', ExtImage, _("Image"))
 
 
+@adapter_config(context=IExtImage, provides=IContentChecker)
+class ExtImageContentChecker(ExtFileContentChecker):
+    """External image content checker"""
+
+    interface = IExtImage
+
+
 @implementer(IExtVideo)
 class ExtVideo(BaseExtFile):
     """External video file persistent class"""
@@ -215,6 +256,13 @@
 register_file_factory('video', ExtVideo, _("Video"))
 
 
+@adapter_config(context=IExtVideo, provides=IContentChecker)
+class ExtVideoContentChecker(ExtFileContentChecker):
+    """External video file content checker"""
+
+    interface = IExtVideo
+
+
 @implementer(IExtAudio)
 class ExtAudio(BaseExtFile):
     """External audio file persistent class"""
@@ -226,3 +274,10 @@
     data = I18nFileProperty(IExtAudio['data'])
 
 register_file_factory('audio', ExtAudio, _("Audio file"))
+
+
+@adapter_config(context=IExtAudio, provides=IContentChecker)
+class ExtAudioContentChecker(ExtFileContentChecker):
+    """External audio file content checker"""
+
+    interface = IExtAudio