Updated workflow publication interface
authorThierry Florac <thierry.florac@onf.fr>
Wed, 12 Jul 2017 13:58:09 +0200
changeset 30 d1fbec754b21
parent 29 265cbf1a941e
child 31 ed8cb2f52adc
Updated workflow publication interface
src/pyams_workflow/content.py
src/pyams_workflow/interfaces/__init__.py
--- a/src/pyams_workflow/content.py	Wed Jun 07 10:54:51 2017 +0200
+++ b/src/pyams_workflow/content.py	Wed Jul 12 13:58:09 2017 +0200
@@ -19,17 +19,20 @@
 # import interfaces
 from pyams_security.interfaces import ISecurityManager
 from pyams_workflow.interfaces import IWorkflowManagedContent, IWorkflowPublicationInfo, IWorkflow, IWorkflowVersions, \
-    IWorkflowPublicationSupport, IObjectClonedEvent, IWorkflowState
+    IWorkflowPublicationSupport, IObjectClonedEvent, IWorkflowState, VersionError, VERSION_DISPLAY, \
+    DISPLAY_FIRST_VERSION, DISPLAY_CURRENT_VERSION
 from zope.annotation.interfaces import IAnnotations
+from zope.dublincore.interfaces import IZopeDublinCore
 
 # import packages
 from persistent import Persistent
 from pyams_utils.adapter import adapter_config
-from pyams_utils.date import format_datetime
+from pyams_utils.date import format_datetime, format_date, SH_DATE_FORMAT
 from pyams_utils.registry import get_utility, query_utility
 from pyams_utils.request import check_request
-from pyams_utils.timezone import gmtime, tztime
+from pyams_utils.timezone import gmtime, tztime, GMT
 from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
 from pyams_workflow.versions import WorkflowHistoryItem
 from pyramid.events import subscriber
 from pyramid.threadlocal import get_current_registry
@@ -38,10 +41,43 @@
 from zope.lifecycleevent import ObjectCreatedEvent
 from zope.location import locate
 from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
 from pyams_workflow import _
 
 
+@vocabulary_config(name='PyAMS content publication date')
+class WorkflowContentDisplayedDateVocabulary(SimpleVocabulary):
+    """Workflow content displayed date vocabulary"""
+
+    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))
+        # check for current version
+        current_version_label = request.localizer.translate(VERSION_DISPLAY[DISPLAY_CURRENT_VERSION])
+        info = IWorkflowPublicationInfo(context, None)
+        if info is not None and info.publication_effective_date:
+            current_version_label = '{1} (= {0})'.format(current_version_label.lower(),
+                                                         format_date(info.publication_effective_date,
+                                                                     format=SH_DATE_FORMAT))
+        terms.append(SimpleTerm(DISPLAY_CURRENT_VERSION, title=current_version_label))
+        super(WorkflowContentDisplayedDateVocabulary, self).__init__(terms)
+
+
 @implementer(IWorkflowPublicationInfo)
 class WorkflowContentPublicationInfo(Persistent, Contained):
     """Workflow content info"""
@@ -50,7 +86,9 @@
     publisher = FieldProperty(IWorkflowPublicationInfo['publisher'])
     _first_publication_date = FieldProperty(IWorkflowPublicationInfo['first_publication_date'])
     _publication_effective_date = FieldProperty(IWorkflowPublicationInfo['publication_effective_date'])
+    push_end_date = FieldProperty(IWorkflowPublicationInfo['push_end_date'])
     _publication_expiration_date = FieldProperty(IWorkflowPublicationInfo['publication_expiration_date'])
+    displayed_publication_date = FieldProperty(IWorkflowPublicationInfo['displayed_publication_date'])
 
     @property
     def publication_date(self):
@@ -79,10 +117,21 @@
 
     @publication_effective_date.setter
     def publication_effective_date(self, value):
-        self._publication_effective_date = gmtime(value)
-        if value and ((self._first_publication_date is None) or
-                      (self._first_publication_date > self._publication_effective_date)):
-            self._first_publication_date = self._publication_effective_date
+        value = gmtime(value)
+        if value:
+            if (self._first_publication_date is None) or (self._first_publication_date > value):
+                self._first_publication_date = value
+            IZopeDublinCore(self.__parent__).effective = value
+        elif self._publication_effective_date:
+            del IZopeDublinCore(self.__parent__)._mapping['Date.Effective']
+        self._publication_effective_date = value
+
+    @property
+    def push_end_date_index(self):
+        value = self.push_end_date
+        if value is None:
+            value = datetime(9999, 12, 31, 11, 59, 59, 999999, GMT)
+        return value
 
     @property
     def publication_expiration_date(self):
@@ -90,13 +139,19 @@
 
     @publication_expiration_date.setter
     def publication_expiration_date(self, value):
-        self._publication_expiration_date = gmtime(value)
+        value = gmtime(value)
+        if value:
+            IZopeDublinCore(self.__parent__).expires = value
+        elif self._publication_expiration_date:
+            del IZopeDublinCore(self.__parent__)._mapping['Date.Expires']
+        self._publication_expiration_date = value
 
     def reset(self):
         self._publication_date = None
         self._first_publication_date = None
         self._publication_effective_date = None
         self._publication_expiration_date = None
+        self.push_end_date = None
 
     def is_published(self):
         # check is parent is published
--- a/src/pyams_workflow/interfaces/__init__.py	Wed Jun 07 10:54:51 2017 +0200
+++ b/src/pyams_workflow/interfaces/__init__.py	Wed Jul 12 13:58:09 2017 +0200
@@ -26,6 +26,7 @@
 from zope.interface import implementer, invariant, Interface, Attribute, Invalid
 from zope.lifecycleevent import ObjectCreatedEvent
 from zope.schema import Choice, Datetime, Set, TextLine, Text, List, Object, Int, Bool
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
 from pyams_workflow import _
 
@@ -381,6 +382,16 @@
                              required=True)
 
 
+DISPLAY_FIRST_VERSION = 'first'
+DISPLAY_CURRENT_VERSION = 'current'
+
+VERSION_DISPLAY = {DISPLAY_FIRST_VERSION: _("Display first version date"),
+                   DISPLAY_CURRENT_VERSION: _("Display current version date")}
+
+VERSION_DISPLAY_VOCABULARY = SimpleVocabulary([SimpleTerm(v, title=t)
+                                               for v, t in VERSION_DISPLAY.items()])
+
+
 class IWorkflowPublicationInfo(Interface):
     """Workflow content publication info"""
 
@@ -405,6 +416,25 @@
                                           description=_("Date from which content will be visible"),
                                           required=False)
 
+    push_end_date = Datetime(title=_("Push end date"),
+                             description=_("Some contents can be pushed by components to front-office pages; if you "
+                                           "set a date here, this content will not be pushed anymore passed this "
+                                           "date, but will still be available via search engine or direct links"),
+                             required=False)
+
+    push_end_date_index = Attribute("Push end date value used by catalog indexes")
+
+    @invariant
+    def check_push_end_date(self):
+        if self.push_end_date is not None:
+            if self.publication_effective_date is None:
+                raise Invalid(_("Can't define push end date without publication start date!"))
+            if self.publication_effective_date >= self.push_end_date:
+                raise Invalid(_("Push end date must be defined after publication start date!"))
+            if self.publication_expiration_date is not None:
+                if self.publication_expiration_date < self.push_end_date:
+                    raise Invalid(_("Push end date must be null or defined before publication end date!"))
+
     publication_expiration_date = Datetime(title=_("Publication end date"),
                                            description=_("Date past which content will not be visible"),
                                            required=False)
@@ -417,6 +447,14 @@
             if self.publication_effective_date >= self.publication_expiration_date:
                 raise Invalid(_("Publication end date must be defined after publication start date!"))
 
+    displayed_publication_date = Choice(title=_("Displayed publication date"),
+                                        description=_("The matching date will be displayed in front-office"),
+                                        vocabulary='PyAMS content publication date',
+                                        default=DISPLAY_FIRST_VERSION,
+                                        required=True)
+
+    visible_publication_date = Attribute("Visible publication date")
+
     def reset(self):
         """Reset all publication info (used by clone features)"""