Transition.py 4.59 KB
Newer Older
1 2 3 4
##############################################################################
#
# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
#                    Romain Courteaud <romain@nexedi.com>
5
#               2015 Wenjie Zheng <wenjie.zheng@tiolive.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

wenjie.zheng's avatar
wenjie.zheng committed
29 30
import sys

31
from AccessControl import ClassSecurityInfo
32
from Acquisition import aq_base
wenjie.zheng's avatar
wenjie.zheng committed
33 34 35 36 37
from copy import deepcopy
from Products.CMFCore.Expression import Expression
from Products.CMFCore.utils import getToolByName
from Products.DCWorkflow.DCWorkflow import ObjectDeleted, ObjectMoved
from Products.DCWorkflow.Guard import Guard
38 39
from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5Type.Accessor.Base import _evaluateTales
40
from Products.ERP5Type.Globals import PersistentMapping
wenjie.zheng's avatar
wenjie.zheng committed
41
from Products.ERP5Type.id_as_reference import IdAsReferenceMixin
42
from Products.ERP5Type.patches.DCWorkflow import ValidationFailed
wenjie.zheng's avatar
wenjie.zheng committed
43 44 45 46
from Products.ERP5Type.patches.WorkflowTool import WorkflowHistoryList
from Products.ERP5Type.Utils import convertToUpperCase, convertToMixedCase
from Products.ERP5Type.XMLObject import XMLObject
from zLOG import LOG, ERROR, DEBUG, WARNING
47 48 49 50

TRIGGER_AUTOMATIC = 0
TRIGGER_USER_ACTION = 1
TRIGGER_WORKFLOW_METHOD = 2
51

52
class Transition(IdAsReferenceMixin("transition_", "prefix"), XMLObject):
53 54 55 56 57 58 59 60 61
  """
  A ERP5 Transition.
  """

  meta_type = 'ERP5 Transition'
  portal_type = 'Transition'
  add_permission = Permissions.AddPortalContent
  isPortalContent = 1
  isRADContent = 1
62 63 64 65 66 67 68
  trigger_type = TRIGGER_USER_ACTION #zwj: type is int 0, 1, 2
  guard = None
  actbox_name = ''
  actbox_url = ''
  actbox_icon = ''
  actbox_category = 'workflow'
  var_exprs = None  # A mapping.
69
  default_reference = ''
70 71 72 73 74 75 76 77 78 79
  # Declarative security
  security = ClassSecurityInfo()
  security.declareObjectProtected(Permissions.AccessContentsInformation)

  # Declarative properties
  property_sheets = (
             PropertySheet.Base,
             PropertySheet.XMLObject,
             PropertySheet.CategoryCore,
             PropertySheet.DublinCore,
80
             PropertySheet.Reference,
81 82 83
             PropertySheet.Transition,
  )

84 85 86 87 88 89 90 91 92
  def getGuardSummary(self):
    res = None
    if self.guard is not None:
      res = self.guard.getSummary()
    return res

  def getGuard(self):
    if self.guard is None:
      self.generateGuard()
wenjie.zheng's avatar
wenjie.zheng committed
93
    return self.guard
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

  def generateGuard(self):
    if self.trigger_type == TRIGGER_USER_ACTION:
      if self.guard == None:
        self.guard = Guard(permissions=self.getPermissionList(),
                      roles=self.getRoleList(),
                      groups=self.getGroupList(),
                      expr=self.getExpression())
      else:
        if self.guard.roles != self.getRoleList():
          self.guard.roles = self.getRoleList()
        if self.guard.permissions != self.getPermissionList():
          self.guard.permissions = self.getPermissionList()
        if self.guard.groups != self.getGroupList():
          self.guard.groups = self.getGroupList()
        if self.guard.expr != self.getExpression():
          self.guard.expr = self.getExpression()

112 113 114 115 116 117 118 119 120 121 122
  def _checkPermission(self, document):
    """
    Check if transition is allowed.
    """
    expr_value = self.getGuardExpression(evaluate=0)
    if expr_value is not None:
      # do not use 'getGuardExpression' to calculate tales because
      # it caches value which is bad. Instead do it manually
      value = _evaluateTales(document, expr_value)
    else:
      value = True
Rafael Monnerat's avatar
Rafael Monnerat committed
123
    #print "CALC", expr_value, '-->', value
124
    return value