Added links content checker
authorThierry Florac <thierry.florac@onf.fr>
Fri, 10 Nov 2017 11:51:55 +0100
changeset 248 381f392c6edb
parent 247 4534b0edd1ad
child 249 990a1e45f07c
Added links content checker
src/pyams_content/component/links/__init__.py
--- a/src/pyams_content/component/links/__init__.py	Fri Nov 10 11:51:29 2017 +0100
+++ b/src/pyams_content/component/links/__init__.py	Fri Nov 10 11:51:55 2017 +0100
@@ -18,11 +18,15 @@
 # import interfaces
 from pyams_content.component.association.interfaces import IAssociationInfo, IAssociationTarget, IAssociationContainer
 from pyams_content.component.links.interfaces import IBaseLink, IInternalLink, IExternalLink, IMailtoLink
+from pyams_content.features.checker.interfaces import IContentChecker, ERROR_VALUE
+from pyams_content.interfaces import IBaseContent
 from pyams_i18n.interfaces import II18n
 from pyams_sequence.interfaces import ISequentialIdInfo
+from pyams_workflow.interfaces import IWorkflow
 
 # import packages
 from pyams_content.component.association import AssociationItem
+from pyams_content.features.checker import BaseContentChecker
 from pyams_content.workflow import VISIBLE_STATES
 from pyams_sequence.utility import get_reference_target
 from pyams_utils.adapter import adapter_config, ContextAdapter
@@ -74,6 +78,17 @@
         return self.context.icon_class
 
 
+class BaseLinkContentChecker(BaseContentChecker):
+    """Base link 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())
+
+
 #
 # Internal links
 #
@@ -131,6 +146,24 @@
         return '--'
 
 
+@adapter_config(context=IInternalLink, provides=IContentChecker)
+class InternalLinkContentChecker(BaseLinkContentChecker):
+    """Internal link content checker"""
+
+    def inner_check(self, request):
+        output = []
+        translate = request.localizer.translate
+        content = get_parent(self.context, IBaseContent)
+        if content is not None:
+            workflow = IWorkflow(content, None)
+            if workflow is not None:
+                target = self.context.get_target(state=workflow.published_states)
+                if target is None:
+                    output.append(translate(ERROR_VALUE).format(field=IInternalLink['reference'].title,
+                                                                message=translate(_("target is not published"))))
+        return output
+
+
 #
 # External links
 #
@@ -172,6 +205,11 @@
         return '--'
 
 
+@adapter_config(context=IExternalLink, provides=IContentChecker)
+class ExternalLinkContentChecker(BaseLinkContentChecker):
+    """External link content checker"""
+
+
 #
 # Mailto links
 #
@@ -211,3 +249,8 @@
     @property
     def human_size(self):
         return '--'
+
+
+@adapter_config(context=IMailtoLink, provides=IContentChecker)
+class MailtoLinkContentChecker(BaseLinkContentChecker):
+    """Mailto link content checker"""