src/pyams_workflow/content.py
changeset 0 73acbfc13577
child 1 a222398c5920
equal deleted inserted replaced
-1:000000000000 0:73acbfc13577
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 from datetime import datetime
       
    18 
       
    19 # import interfaces
       
    20 from pyams_security.interfaces import IPrincipalInfo
       
    21 from pyams_workflow.interfaces import IWorkflowManagedContent, IWorkflowPublicationInfo, IWorkflow, IWorkflowVersions, \
       
    22     IWorkflowVersion, IWorkflowPublicationSupport
       
    23 from zope.annotation.interfaces import IAnnotations
       
    24 
       
    25 # import packages
       
    26 from persistent import Persistent
       
    27 from pyams_utils.adapter import adapter_config
       
    28 from pyams_utils.registry import query_utility
       
    29 from pyams_utils.request import check_request
       
    30 from pyams_utils.timezone import gmtime
       
    31 from pyams_utils.traversing import get_parent
       
    32 from pyramid.threadlocal import get_current_registry
       
    33 from zope.container.contained import Contained
       
    34 from zope.interface import implementer
       
    35 from zope.lifecycleevent import ObjectCreatedEvent
       
    36 from zope.location import locate
       
    37 from zope.schema.fieldproperty import FieldProperty
       
    38 
       
    39 
       
    40 @implementer(IWorkflowPublicationInfo)
       
    41 class WorkflowContentPublicationInfo(Persistent, Contained):
       
    42     """Workflow content info"""
       
    43 
       
    44     _state_date = FieldProperty(IWorkflowPublicationInfo['state_date'])
       
    45     _state_principal = FieldProperty(IWorkflowPublicationInfo['state_principal'])
       
    46     _publication_date = FieldProperty(IWorkflowPublicationInfo['publication_date'])
       
    47     _first_publication_date = FieldProperty(IWorkflowPublicationInfo['first_publication_date'])
       
    48     _publication_effective_date = FieldProperty(IWorkflowPublicationInfo['publication_effective_date'])
       
    49     _publication_expiration_date = FieldProperty(IWorkflowPublicationInfo['publication_expiration_date'])
       
    50 
       
    51     @property
       
    52     def state_date(self):
       
    53         return self._state_date
       
    54 
       
    55     @state_date.setter
       
    56     def state_date(self, value):
       
    57         self._state_date = gmtime(value)
       
    58 
       
    59     @property
       
    60     def state_principal(self):
       
    61         return self._state_principal
       
    62 
       
    63     @state_principal.setter
       
    64     def state_principal(self, value):
       
    65         if IPrincipalInfo.providedBy(value):
       
    66             value = value.id
       
    67         self._state_principal = value
       
    68 
       
    69     @property
       
    70     def publication_date(self):
       
    71         return self._publication_date
       
    72 
       
    73     @publication_date.setter
       
    74     def publication_date(self, value):
       
    75         self._publication_date = gmtime(value)
       
    76 
       
    77     @property
       
    78     def first_publication_date(self):
       
    79         return self._first_publication_date
       
    80 
       
    81     @property
       
    82     def publication_effective_date(self):
       
    83         return self._publication_effective_date
       
    84 
       
    85     @publication_effective_date.setter
       
    86     def publication_effective_date(self, value):
       
    87         self._publication_effective_date = gmtime(value)
       
    88         if value and ((self._first_publication_date is None) or
       
    89                       (self._first_publication_date > self._publication_effective_date)):
       
    90             self._first_publication_date = self._publication_effective_date
       
    91 
       
    92     @property
       
    93     def publication_expiration_date(self):
       
    94         return self._publication_expiration_date
       
    95 
       
    96     @publication_expiration_date.setter
       
    97     def publication_expiration_date(self, value):
       
    98         self._publication_expiration_date = gmtime(value)
       
    99 
       
   100     def reset(self):
       
   101         self._publication_date = None
       
   102         self._first_publication_date = None
       
   103         self._publication_effective_date = None
       
   104         self._publication_expiration_date = None
       
   105 
       
   106     def is_published(self):
       
   107         # check is parent is published
       
   108         parent = get_parent(self.__parent__, IWorkflowPublicationSupport, allow_context=False)
       
   109         if (parent is not None) and not IWorkflowPublicationInfo(parent).is_published():
       
   110             return False
       
   111         # associated workflow?
       
   112         wf_name = IWorkflowManagedContent(self.__parent__).workflow_name
       
   113         if not wf_name:
       
   114             return True
       
   115         # published state(s) in workflow?
       
   116         workflow = query_utility(IWorkflow, name=wf_name)
       
   117         if (workflow is None) or not workflow.published_states:
       
   118             return False
       
   119         # check content versions
       
   120         versions = IWorkflowVersions(self.__parent__)
       
   121         if not (versions or versions.get_versions(workflow.published_states)):
       
   122             return False
       
   123         now = datetime.utcnow()
       
   124         return (self.publication_effective_date is not None) and \
       
   125                (self.publication_effective_date <= now) and \
       
   126                ((self.publication_expiration_date is None) or
       
   127                 (self.publication_expiration_date >= now))
       
   128 
       
   129     def is_visible(self, request=None):
       
   130         # associated workflow?
       
   131         wf_name = IWorkflowManagedContent(self.__parent__).workflow_name
       
   132         if not wf_name:
       
   133             return True
       
   134         # check workflow?
       
   135         workflow = query_utility(IWorkflow, name=wf_name)
       
   136         if workflow is None:
       
   137             return False
       
   138         if workflow.view_permission:
       
   139             if request is None:
       
   140                 request = check_request()
       
   141             if not request.has_permission(workflow.view_permission, context=self.__parent__):
       
   142                 return False
       
   143         return self.is_published()
       
   144 
       
   145 
       
   146 WORKFLOW_CONTENT_KEY = 'pyams_workflow.content_info'
       
   147 
       
   148 
       
   149 @adapter_config(context=IWorkflowPublicationSupport, provides=IWorkflowPublicationInfo)
       
   150 def WorkflowContentPublicationInfoFactory(context):
       
   151     """Workflow content info factory"""
       
   152     annotations = IAnnotations(context)
       
   153     info = annotations.get(WORKFLOW_CONTENT_KEY)
       
   154     if info is None:
       
   155         info = annotations[WORKFLOW_CONTENT_KEY] = WorkflowContentPublicationInfo()
       
   156         registry = get_current_registry()
       
   157         registry.notify(ObjectCreatedEvent(info))
       
   158         locate(info, context)
       
   159     return info