TrashTool.py 5.68 KB
Newer Older
Aurel's avatar
Aurel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
#                    Aurlien Calonne <aurel@nexedi.com>
#
# 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.
#
##############################################################################


from AccessControl import ClassSecurityInfo
from Globals import InitializeClass, DTMLFile
from Products.ERP5Type.Tool.BaseTool import BaseTool
from Products.ERP5Type import Permissions
from Products.ERP5 import _dtmldir
from zLOG import LOG
from DateTime import DateTime
from Acquisition import aq_base

class TrashTool(BaseTool):
  """
    TrashTool manage removed object from installation of BusinessTemplates
  """
  title = 'Trash Tool'
  id = 'portal_trash'
  meta_type = 'ERP5 Trash Tool'
  portal_type = 'Trash Tool'
  allowed_types = ('ERP5 Trash Bin',)

  # Declarative Security
  security = ClassSecurityInfo()
  
  security.declareProtected(Permissions.ManagePortal, 'manage_overview' )
  manage_overview = DTMLFile( 'explainRuleTool', _dtmldir )

  def backupObject(self, trashbin, container_path, object_id, save, **kw):
    """
      Backup an object in a trash bin
    """
#     LOG('Trash : backup object', 0, str((container_path, object_id)))
    if save:
      # recreate path of the backup object if necessary
      backup_object_container = trashbin
      for path in container_path:
        if 'portal' in path:
          path += '_items'
        if path not in backup_object_container.objectIds():
Aurel's avatar
Aurel committed
67
          backup_object_container = backup_object_container.newContent(portal_type='Trash Folder', id=path, isIndexable=0)
Aurel's avatar
Aurel committed
68 69 70 71 72 73 74 75 76
          backup_object_container.edit(isHidden=1)
        else:
          backup_object_container = backup_object_container._getOb(path)
      # backup the object
      # here we choose export/import to copy because cut/paste
      # do too many things and check for what we want to do
      if object_id not in backup_object_container.objectIds():
        # export object
        object_path = container_path + [object_id]
77 78
        obj = self.unrestrictedTraverse(object_path)
        copy = obj._p_jar.exportFile(obj._p_oid)
Aurel's avatar
Aurel committed
79 80
        # import object in trash
        connection = backup_object_container._p_jar
81
        o = backup_object_container
Aurel's avatar
Aurel committed
82
        while connection is None:
83 84
          o = o.aq_parent
          connection=o._p_jar
Aurel's avatar
Aurel committed
85 86
        copy.seek(0)
        backup = connection.importFile(copy)
Aurel's avatar
Aurel committed
87
        backup.isIndexable = 0
Aurel's avatar
Aurel committed
88 89 90 91 92 93 94
        backup_object_container._setObject(object_id, backup)
        
    keep_sub = kw.get('keep_subobjects', 0)
    subobjects_dict = {}
    if not keep_sub:
      # export subobjects
      if save:
95
        obj = backup_object_container._getOb(object_id)
Aurel's avatar
Aurel committed
96 97
      else:
        object_path = container_path + [object_id]
98 99
        obj = self.unrestrictedTraverse(object_path)
      for subobject_id in list(obj.objectIds()):
Aurel's avatar
Aurel committed
100 101 102 103 104
        subobject_path = object_path + [subobject_id]
        subobject = self.unrestrictedTraverse(subobject_path)
        subobject_copy = subobject._p_jar.exportFile(subobject._p_oid)
        subobjects_dict[subobject_id] = subobject_copy
        if save: # remove subobjecs from backup object
105
          obj.manage_delObjects([subobject_id])
Aurel's avatar
Aurel committed
106 107 108 109 110 111 112
#     LOG('return subobject dict', 0, subobjects_dict)
    return subobjects_dict

  def newTrashBin(self, bt_title='trash', bt=None):
    """
      Create a new trash bin at upgrade of bt
    """
113
#     LOG('new Trash bin for', 0, bt_title)
Aurel's avatar
Aurel committed
114 115 116 117 118 119 120 121 122 123 124
    # construct date
    date = DateTime()
    start_date = date.strftime('%Y-%m-%d')
    # generate id
    trash_ids = self.objectIds()
    n = 0
    new_trash_id = bt_title+'_'+start_date
    while  new_trash_id in trash_ids:
      n = n + 1
      new_trash_id = '%s_%s' %(bt_title+'_'+start_date, n)
    # create trash bin
125
#     LOG('creating trash bin with id', 0, new_trash_id)
Aurel's avatar
Aurel committed
126
    trashbin = self.newContent(portal_type='Trash Bin', id=new_trash_id, title=bt_title, start_date=start_date, causality_value=bt)
127
#     LOG('trash item created', 0, trashbin)
Aurel's avatar
Aurel committed
128 129 130 131 132 133 134
    return trashbin
  

  def getTrashBinObjectsList(self, trashbin):
    """
      Return a list of trash objects for a given trash bin
    """
135
    def getChildObjects(obj):
Aurel's avatar
Aurel committed
136
      object_list = []
137 138 139 140 141
      if hasattr(aq_base(obj), 'objectValues'):
        childObjects = obj.objectValues()
      if hasattr(aq_base(obj), 'isHidden'):
        if not obj.isHidden:
          object_list.append(obj)
Aurel's avatar
Aurel committed
142
      if len(childObjects) > 0:
143 144
        for o in childObjects:
          object_list.extend(getChildObjects(o))          
Aurel's avatar
Aurel committed
145
      else:
146
        object_list.append(obj)
Aurel's avatar
Aurel committed
147 148 149 150 151 152 153 154 155
      return object_list
              
    list = getChildObjects(trashbin)
    list.sort()
    return list



InitializeClass(TrashTool)