Back to index

plone3  3.1.7
VersionPolicies.py
Go to the documentation of this file.
00001 #########################################################################
00002 # Copyright (c) 2004, 2005 Alec Mitchell
00003 # All Rights Reserved.
00004 #
00005 # This file is part of CMFEditions.
00006 #
00007 # CMFEditions is free software; you can redistribute it and/or modify
00008 # it under the terms of the GNU General Public License as published by
00009 # the Free Software Foundation; either version 2 of the License, or
00010 # (at your option) any later version.
00011 #
00012 # CMFEditions is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 # GNU General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU General Public License
00018 # along with CMFEditions; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00020 #########################################################################
00021 """Default Version Policy implementations.
00022 
00023 $Id$
00024 """
00025 
00026 from AccessControl import ClassSecurityInfo
00027 from OFS.SimpleItem import SimpleItem
00028 from Products.CMFCore.utils import getToolByName
00029 from Products.CMFEditions.interfaces.IVersionPolicy import IVersionPolicy
00030 
00031 
00032 class VersionPolicy(SimpleItem):
00033     """A simple class for storing version policy information"""
00034 
00035     __implements__ = (SimpleItem.__implements__,
00036                       IVersionPolicy
00037                       )
00038     security = ClassSecurityInfo()
00039 
00040     def __init__(self, obj_id, title, **kw):
00041         self.id = obj_id
00042         self.title = title
00043 
00044     security.declarePublic('Title')
00045     def Title(self):
00046         return self.title
00047 
00048 
00049 class ATVersionOnEditPolicy(VersionPolicy):
00050     """A policy that implements version creation on edit for AT types,
00051        requires a custom edit_macros.pt and a controller script called
00052        update_version_on_edit.  This policy automatically adds and removes
00053        the controller script from the AT edit controller chain on install."""
00054     FC_ACTION_LIST = ({'template': 'atct_edit',
00055                        'status': 'success',
00056                        'action': 'traverse_to',
00057                        'expression': 'string:update_version_before_edit',
00058                        'context':None,
00059                        'button':None},
00060                       {'template': 'atct_edit',
00061                        'status': 'success',
00062                        'action': 'traverse_to',
00063                        'expression': 'string:add_reference',
00064                        'context':None,
00065                        'button':'form_add'},
00066                       {'template': 'atct_edit',
00067                        'status': 'success',
00068                        'action': 'traverse_to',
00069                        'expression': 'string:go_back',
00070                        'context':None,
00071                        'button':'cancel'},
00072                       {'template': 'validate_integrity',
00073                        'status': 'success',
00074                        'action': 'traverse_to',
00075                        'expression': 'string:update_version_on_edit',
00076                        'context':None,
00077                        'button':None},)
00078 
00079     def setupPolicyHook(self, portal, **kw):
00080         add_form_controller_overrides(portal, self.FC_ACTION_LIST)
00081 
00082     def removePolicyHook(self, portal, **kw):
00083         remove_form_controller_overrides(portal, self.FC_ACTION_LIST)
00084 
00085 
00086 # Helper methods
00087 def add_form_controller_overrides(portal, actions):
00088     fc = getToolByName(portal, 'portal_form_controller', None)
00089     if fc is not None:
00090         for action in actions:
00091             fc.addFormAction(action['template'],
00092                         action['status'],
00093                         action['context'],
00094                         action['button'],
00095                         action['action'],
00096                         action['expression'])
00097 
00098 
00099 def remove_form_controller_overrides(portal, actions):
00100     fc = getToolByName(portal, 'portal_form_controller', None)
00101     # Fake a request because form controller needs one to delete actions
00102     fake_req = DummyRequest()
00103     i = 0
00104     for fc_act in fc.listFormActions(1):
00105         for action in actions:
00106             if (action['template'] == fc_act.getObjectId() and
00107                     action['status'] == fc_act.getStatus() and
00108                     action['context'] == fc_act.getContextType() and
00109                     action['button'] == fc_act.getButton() and
00110                     action['action'] == fc_act.getActionType() and
00111                     action['expression'] == fc_act.getActionArg()):
00112                 fake_req.form['del_id_%s'%i]=True
00113                 fake_req.form['old_object_id_%s'%i]=action['template'] or ''
00114                 fake_req.form['old_context_type_%s'%i]=action['context'] or ''
00115                 fake_req.form['old_button_%s'%i]=action['button'] or ''
00116                 fake_req.form['old_status_%s'%i]=action['status'] or ''
00117         i = i+1
00118     # Use the private method because the public one does a redirect
00119     fc._delFormActions(fc.actions,fake_req)
00120 
00121 # Fake request class to satisfy formcontroller removal policy
00122 class DummyRequest(dict):
00123     def __init__(self):
00124         self.form = {}