Updated IWorkflowPublicationInfo to handle all attributes on contents which don't support versions
authorThierry Florac <tflorac@ulthar.net>
Wed, 16 Jan 2019 15:43:18 +0100
changeset 94 fef42b125ff9
parent 93 f6788c2cca51
child 95 110e3c726006
Updated IWorkflowPublicationInfo to handle all attributes on contents which don't support versions
src/pyams_workflow/content.py
--- a/src/pyams_workflow/content.py	Fri Jan 11 13:18:17 2019 +0100
+++ b/src/pyams_workflow/content.py	Wed Jan 16 15:43:18 2019 +0100
@@ -18,13 +18,13 @@
 from pyramid.events import subscriber
 from zope.container.contained import Contained
 from zope.dublincore.interfaces import IZopeDublinCore
-from zope.interface import implementer
 from zope.schema.fieldproperty import FieldProperty
 from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
 
 from pyams_security.interfaces import ISecurityManager
 from pyams_utils.adapter import adapter_config, get_annotation_adapter
 from pyams_utils.date import SH_DATE_FORMAT, format_date, format_datetime
+from pyams_utils.factory import factory_config
 from pyams_utils.interfaces import VIEW_PERMISSION
 from pyams_utils.registry import get_utility
 from pyams_utils.request import check_request
@@ -45,20 +45,21 @@
     def __init__(self, context):
         request = check_request()
         terms = []
-        versions = IWorkflowVersions(context.__parent__)
-        # check for first version
-        first_version_label = request.localizer.translate(VERSION_DISPLAY[DISPLAY_FIRST_VERSION])
-        try:
-            first_version = versions.get_version(1)
-        except VersionError:
-            pass
-        else:
-            info = IWorkflowPublicationInfo(first_version, None)
-            if info is not None and info.publication_effective_date:
-                first_version_label = '{1} (= {0})'.format(first_version_label.lower(),
-                                                           format_date(info.publication_effective_date,
-                                                                       format=SH_DATE_FORMAT))
-        terms.append(SimpleTerm(DISPLAY_FIRST_VERSION, title=first_version_label))
+        versions = IWorkflowVersions(context.__parent__, None)
+        if versions is not None:
+            # check for first version
+            first_version_label = request.localizer.translate(VERSION_DISPLAY[DISPLAY_FIRST_VERSION])
+            try:
+                first_version = versions.get_version(1)
+            except VersionError:
+                pass
+            else:
+                info = IWorkflowPublicationInfo(first_version, None)
+                if info is not None and info.publication_effective_date:
+                    first_version_label = '{1} (= {0})'.format(first_version_label.lower(),
+                                                               format_date(info.publication_effective_date,
+                                                                           format=SH_DATE_FORMAT))
+            terms.append(SimpleTerm(DISPLAY_FIRST_VERSION, title=first_version_label))
         # check for current version
         current_version_label = request.localizer.translate(VERSION_DISPLAY[DISPLAY_CURRENT_VERSION])
         info = IWorkflowPublicationInfo(context, None)
@@ -70,7 +71,7 @@
         super(WorkflowContentDisplayedDateVocabulary, self).__init__(terms)
 
 
-@implementer(IWorkflowPublicationInfo)
+@factory_config(IWorkflowPublicationInfo)
 class WorkflowContentPublicationInfo(Persistent, Contained):
     """Workflow content info"""
 
@@ -142,10 +143,12 @@
     def visible_publication_date(self):
         displayed_date = self.displayed_publication_date
         if displayed_date == DISPLAY_FIRST_VERSION:
-            state = IWorkflowState(self.__parent__)
-            if state.version_id > 1:
-                version = IWorkflowVersions(self.__parent__).get_version(1)
-                return IWorkflowPublicationInfo(version).publication_effective_date
+            state = IWorkflowState(self.__parent__, None)
+            if (state is not None) and (state.version_id > 1):
+                versions = IWorkflowVersions(self.__parent__, None)
+                if versions is not None:
+                    version = versions.get_version(1)
+                    return IWorkflowPublicationInfo(version).publication_effective_date
         return self.publication_effective_date
 
     def reset(self, complete=True):
@@ -201,7 +204,7 @@
 @adapter_config(context=IWorkflowPublicationSupport, provides=IWorkflowPublicationInfo)
 def workflow_content_publication_info_factory(context):
     """Workflow content info factory"""
-    return get_annotation_adapter(context, WORKFLOW_CONTENT_KEY, WorkflowContentPublicationInfo)
+    return get_annotation_adapter(context, WORKFLOW_CONTENT_KEY, IWorkflowPublicationInfo)
 
 
 @subscriber(IObjectClonedEvent)