Back to index

plone3  3.1.7
adapter.py
Go to the documentation of this file.
00001 ## -*- coding: utf-8 -*-
00002 ## Copyright (C) 2008 Ingeniweb
00003 
00004 ## This program is free software; you can redistribute it and/or modify
00005 ## it under the terms of the GNU General Public License as published by
00006 ## the Free Software Foundation; either version 2 of the License, or
00007 ## (at your option) any later version.
00008 
00009 ## This program is distributed in the hope that it will be useful,
00010 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ## GNU General Public License for more details.
00013 
00014 ## You should have received a copy of the GNU General Public License
00015 ## along with this program; see the file COPYING. If not, write to the
00016 ## Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 __version__ = "$Revision: $"
00019 # $Source: $
00020 # $Id: $
00021 __docformat__ = 'restructuredtext'
00022 
00023 from zope.interface import Interface, implementer
00024 from zope.component import adapter
00025 
00026 from Products.CMFPlacefulWorkflow.PlacefulWorkflowTool import WorkflowPolicyConfig_id
00027 from Products.CMFPlacefulWorkflow.interfaces import IPlacefulMarker
00028 from Acquisition import aq_base, aq_parent, aq_inner
00029 from Products.CMFCore.utils import getToolByName
00030 from Products.CMFPlone.utils import base_hasattr
00031 from Products.CMFPlone.interfaces import IWorkflowChain
00032 from Products.CMFPlone.workflow import ToolWorkflowChain
00033 
00034 @adapter(Interface, IPlacefulMarker)
00035 @implementer(IWorkflowChain)
00036 def PlacefulWorkflowChain(ob, tool):
00037     """Monkey-patched by CMFPlacefulWorkflow to look for placeful workflow configurations.
00038 
00039     Goal: find a workflow chain in a policy
00040 
00041     Steps:
00042     1. ask the object if it contains a policy
00043     2. if it does, ask him for a chain
00044     3. if there's no chain for the type the we loop on the parent
00045     4. if the parent is the portal object or None we stop and ask
00046        portal_workflow
00047     """
00048     if isinstance(ob, basestring):
00049         # We are not in an object, then we can only get default from
00050         # portal_workflow
00051         return ToolWorkflowChain(ob, tool)
00052 
00053     elif hasattr(aq_base(ob), 'getPortalTypeName'):
00054         portal_type = ob.getPortalTypeName()
00055     else:
00056         portal_type = None
00057 
00058     if portal_type is None or ob is None:
00059         return ()
00060 
00061     # Inspired by implementation in CPSWorkflowTool.py of CPSCore 3.9.0
00062     # Workflow needs to be determined by true containment not context
00063     # so we loop over the actual containers
00064     chain = None
00065     wfpolicyconfig = None
00066     current_ob = aq_inner(ob)
00067     # start_here is used to check 'In policy': We check it only in the
00068     # first folder
00069     start_here = True
00070     portal = aq_base(getToolByName(tool, 'portal_url').getPortalObject())
00071     while chain is None and current_ob is not None:
00072         if base_hasattr(current_ob, WorkflowPolicyConfig_id):
00073             wfpolicyconfig = getattr(current_ob, WorkflowPolicyConfig_id)
00074             chain = wfpolicyconfig.getPlacefulChainFor(portal_type,
00075                                                        start_here=start_here)
00076             if chain is not None:
00077                 return chain
00078 
00079         elif aq_base(current_ob) is portal:
00080             break
00081         start_here = False
00082         current_ob = aq_inner(aq_parent(current_ob))
00083 
00084     # fallback on the default mechanism
00085     return ToolWorkflowChain(ob, tool)