source: ZMS/trunk/_copysupport.py @ 1761

Revision 1761, 11.5 KB checked in by zmsdev, 8 weeks ago (diff)

applied minor performance-fixes (2)

Line 
1################################################################################
2# _copysupport.py
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; either version 2
7# of the License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17################################################################################
18
19# Imports.
20import copy
21import string
22import time
23import urllib
24from OFS import Moniker
25from OFS.CopySupport import _cb_decode, _cb_encode, absattr, CopyError, eNoData, eNotFound, eInvalid
26# Product Imports.
27import _globals
28
29
30################################################################################
31################################################################################
32###
33###   C l a s s   C o p y S u p p o r t
34###
35################################################################################
36################################################################################
37class CopySupport:
38
39    # --------------------------------------------------------------------------
40    #  CopySupport._get_cb_copy_data:
41    # --------------------------------------------------------------------------
42    def _get_cb_copy_data(self, cb_copy_data=None, REQUEST=None):
43      cp=None
44      if cb_copy_data is not None:
45        cp=cb_copy_data
46      else:
47        if REQUEST and REQUEST.has_key('__cp'):
48          cp=REQUEST['__cp']
49      if cp is None:
50        raise CopyError, eNoData
51     
52      try:
53        cp=_cb_decode(cp)
54      except:
55        _globals.writeError( self, '[_get_cb_copy_data]: eInvalid')
56        raise CopyError, eInvalid
57     
58      return cp
59
60    # --------------------------------------------------------------------------
61    #  CopySupport._get_obs:
62    # --------------------------------------------------------------------------
63    def _get_obs(self, cp):
64       
65        try:
66          cp=_cb_decode(cp)
67        except:
68          _globals.writeError( self, '[_get_obs]: eInvalid')
69          raise CopyError, eInvalid
70       
71        oblist=[]
72        op=cp[0]
73        app = self.getPhysicalRoot()
74       
75        for mdata in cp[1]:
76          m = Moniker.loadMoniker(mdata)
77          try:
78            ob = m.bind(app)
79          except:
80            _globals.writeError( self, '[_get_obs]: eNotFound')
81            raise CopyError, eNotFound
82          self._verifyObjectPaste(ob)
83          oblist.append(ob)
84       
85        return oblist
86
87    def cp_get_obs(self, REQUEST):
88      cp = self._get_cb_copy_data(cb_copy_data=None,REQUEST=REQUEST)
89      op = cp[0]
90      cp = (0,cp[1])
91      cp = _cb_encode(cp)
92      return self._get_obs( cp)
93
94
95    # --------------------------------------------------------------------------
96    #  CopySupport._get_ids:
97    # --------------------------------------------------------------------------
98    def _get_ids(self, cp):
99      return map(lambda ob: self._get_id(absattr(ob.id)),self._get_obs(cp))
100
101
102    # --------------------------------------------------------------------------
103    #  CopySupport._get_id:
104    #
105    #  Allow containers to override the generation of
106    #  object copy id by attempting to call its _get_id
107    #  method, if it exists.
108    # --------------------------------------------------------------------------
109    def _get_id(self, id):
110      return 'copy_of_%s'%id
111
112
113    # --------------------------------------------------------------------------
114    #  CopySupport._set_sort_ids:
115    #
116    #  Group all objects to be copied / moved at new position (given by _sort_id)
117    #  in correct sort-order.
118    # --------------------------------------------------------------------------
119    def _set_sort_ids(self, ids, op, REQUEST):
120      _globals.writeLog( self, "[_set_sort_ids]: %s"%self.absolute_url())
121     
122      copy_of_prefix = 'copy_of_'
123      sort_id = REQUEST.get('_sort_id',0) + 1
124      for ob in self.getChildNodes():
125        id = absattr(ob.id)
126        if id in ids or (op==1 and copy_of_prefix + id in ids):
127          setattr( ob, 'sort_id', _globals.format_sort_id(sort_id))
128          sort_id = sort_id + 1
129
130
131    # --------------------------------------------------------------------------
132    #  CopySupport._normalize_ids_after_copy:
133    # --------------------------------------------------------------------------
134    def _normalize_ids_after_copy(self, ids=[], forced=0, REQUEST=None):
135      _globals.writeLog( self, "[_normalize_ids_after_copy]: %s"%self.absolute_url())
136     
137      copy_of_prefix = 'copy_of_'
138      id_prefix = REQUEST.get( 'id_prefix')
139      REQUEST.set( 'id_prefix', None)
140      ob_ids = copy.copy(self.objectIds( self.dGlobalAttrs.keys()))
141      for ob in self.objectValues( self.dGlobalAttrs.keys()):
142        id = absattr(ob.id)
143        if forced or id in ids:
144          _globals.writeBlock( self, '[_normalize_ids_after_copy]: %s(%s)'%(id,ob.meta_id))
145         
146          if id_prefix:
147            id_prefix = _globals.id_prefix(id_prefix)
148            if id_prefix != _globals.id_prefix(id):
149              new_id = self.getNewId(id_prefix)
150              _globals.writeBlock( self, '[_normalize_ids_after_copy]: Rename %s(%s) to %s'%(id,ob.meta_id,new_id))
151              self.manage_renameObject(id=id,new_id=new_id)
152              self.initObjChildren(REQUEST)
153              id = new_id
154         
155          # Assign new id.
156          prefix = _globals.id_prefix(id)
157          if prefix.find(copy_of_prefix)==0:
158            prefix = prefix[len(copy_of_prefix):]
159          if prefix != id:
160            new_id = self.getNewId(prefix)
161            self.manage_renameObject(id=id,new_id=new_id)
162            self.initObjChildren(REQUEST)
163         
164          bk_lang = REQUEST.get('lang')
165          for lang in self.getLangIds():
166            REQUEST.set('lang',lang)
167            if forced==0:
168              # Object-State and Version-Manager.
169              if not ob.getAutocommit():
170                ob.setObjStateNew(REQUEST,reset=0)
171                ob.onChangeObj(REQUEST)
172            # Process referential integrity.
173            ob.onCopyRefObj(REQUEST)
174         
175          REQUEST.set('lang',bk_lang)
176         
177          # Process tree.
178          ob._normalize_ids_after_copy(ids=ids,forced=1,REQUEST=REQUEST)
179
180
181    # --------------------------------------------------------------------------
182    #  CopySupport._normalize_ids_after_move:
183    # --------------------------------------------------------------------------
184    def _normalize_ids_after_move(self, ids=[], forced=0, REQUEST=None):
185      _globals.writeLog( self, "[_normalize_ids_after_move]: %s"%self.absolute_url())
186     
187      copy_of_prefix = 'copy_of_'
188      id_prefix = REQUEST.get( 'id_prefix')
189      REQUEST.set( 'id_prefix', None)
190      ob_ids = copy.copy(self.objectIds( self.dGlobalAttrs.keys()))
191      for ob in self.objectValues( self.dGlobalAttrs.keys()):
192        id = absattr(ob.id)
193        if forced or (id in ids and not copy_of_prefix + id in ob_ids) or (copy_of_prefix + id in ids):
194          _globals.writeBlock( self, '[_normalize_ids_after_move]: %s(%s)'%(id,ob.meta_id))
195         
196          if id_prefix:
197            id_prefix = _globals.id_prefix(id_prefix)
198            if id_prefix != _globals.id_prefix(id):
199              new_id = self.getNewId(id_prefix)
200              _globals.writeBlock( self, '[_normalize_ids_after_move]: Rename %s(%s) to %s'%(id,ob.meta_id,new_id))
201              self.manage_renameObject(id=id,new_id=new_id)
202              self.initObjChildren(REQUEST)
203              id = new_id
204         
205          # Re-Assign old id.
206          if id.find(copy_of_prefix)==0:
207            try:
208              new_id = id[len(copy_of_prefix):]
209              self.manage_renameObject(id=id,new_id=new_id)
210              self.initObjChildren(REQUEST)
211            except:
212              pass
213           
214          bk_lang = REQUEST.get('lang')
215          for lang in self.getLangIds():
216            REQUEST.set('lang',lang)
217            if forced==0:
218              # Object-State and Version-Manager.
219              if not ob.getAutocommit():
220                ob.setObjStateModified(REQUEST)
221                ob.onChangeObj(REQUEST)
222            # Process referential integrity.
223            ob.onMoveRefObj(REQUEST)
224         
225          REQUEST.set('lang',bk_lang)
226         
227          # Process tree.
228          ob._normalize_ids_after_move(ids=ids,forced=1,REQUEST=REQUEST)
229
230
231    ############################################################################
232    # CopySupport.manage_cutObjects:
233    ############################################################################
234    def manage_cutObjects(self, ids=None, REQUEST=None):
235      """Put a reference to the objects named in ids in the clip board"""
236      _globals.writeLog( self, "[manage_pasteObjs]")
237      super( self.__class__, self).manage_cutObjects( ids, REQUEST)
238      # Return with message.
239      if REQUEST is not None:
240        message = ''
241        REQUEST.RESPONSE.redirect('manage_main?lang=%s&manage_tabs_message=%s'%(REQUEST['lang'],urllib.quote(message)))
242
243
244    ############################################################################
245    # CopySupport.manage_pasteObjs:
246    #
247    # Paste previously copied objects into the current object.
248    # If calling manage_pasteObjects from python code, pass
249    # the result of a previous call to manage_cutObjects or
250    # manage_copyObjects as the first argument.
251    ############################################################################
252    def manage_pasteObjs(self, REQUEST, RESPONSE=None):
253      """ CopySupport.manage_pasteObjs """
254      _globals.writeBlock( self, "[manage_pasteObjs]")
255      t0 = time.time()
256     
257      # Analyze request
258      cp=self._get_cb_copy_data(cb_copy_data=None,REQUEST=REQUEST)
259      op=cp[0]
260      cp = (0,cp[1])
261      cp = _cb_encode(cp)
262      ids = self._get_ids(cp)
263      oblist = self._get_obs(cp)
264     
265      # Paste objects.
266      self.manage_pasteObjects(cb_copy_data=None,REQUEST=REQUEST)
267     
268      # Sort order (I).
269      self._set_sort_ids(ids=ids,op=op,REQUEST=REQUEST)
270     
271      # Move objects.
272      if op==1:
273        self._normalize_ids_after_move(ids=ids,forced=0,REQUEST=REQUEST)
274      # Copy objects.
275      else:
276        self._normalize_ids_after_copy(ids=ids,forced=0,REQUEST=REQUEST)
277     
278      # Keep links (ref_by) synchron.
279      if self.getConfProperty('ZMS.InternalLinks.keepsynchron',0)==1:
280        obs  = _globals.objectTree( self)
281        for ob in obs:
282          self.synchronizeRefToObjs()
283          self.synchronizeRefByObjs()
284     
285      # Sort order (II).
286      self.normalizeSortIds()
287     
288      # Return with message.
289      if RESPONSE is not None:
290        message = self.getZMILangStr('MSG_PASTED')
291        message += ' (in '+str(int((time.time()-t0)*100.0)/100.0)+' secs.)'
292        RESPONSE.redirect('manage_main?lang=%s&manage_tabs_message=%s'%(REQUEST['lang'],urllib.quote(message)))
293
294################################################################################
Note: See TracBrowser for help on using the repository browser.