TrashTool.py 7.77 KB
Newer Older
Aurel's avatar
Aurel committed
1 2 3
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
4
#                    Aurelien Calonne <aurel@nexedi.com>
Aurel's avatar
Aurel committed
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
#
# 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
35
from Products.ERP5.Document.BusinessTemplate import getChainByType
Aurel's avatar
Aurel committed
36 37 38 39 40 41
from zLOG import LOG
from DateTime import DateTime
from Acquisition import aq_base

class TrashTool(BaseTool):
  """
Vincent Pelletier's avatar
Vincent Pelletier committed
42
    TrashTool contains objects removed/replaced during installation of business templates.
Aurel's avatar
Aurel committed
43 44 45 46 47 48 49 50 51
  """
  title = 'Trash Tool'
  id = 'portal_trash'
  meta_type = 'ERP5 Trash Tool'
  portal_type = 'Trash Tool'
  allowed_types = ('ERP5 Trash Bin',)

  # Declarative Security
  security = ClassSecurityInfo()
52

Aurel's avatar
Aurel committed
53
  security.declareProtected(Permissions.ManagePortal, 'manage_overview' )
Aurel's avatar
Aurel committed
54
  manage_overview = DTMLFile( 'explainTrashTool', _dtmldir )
Aurel's avatar
Aurel committed
55 56 57 58 59 60 61 62 63 64 65 66 67

  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():
68
          backup_object_container = backup_object_container.newContent(portal_type='Trash Folder', id=path,
69
                                                                       is_indexable=0)
Aurel's avatar
Aurel committed
70 71 72 73 74 75
          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
76
      obj = None
Aurel's avatar
Aurel committed
77 78 79
      if object_id not in backup_object_container.objectIds():
        # export object
        object_path = container_path + [object_id]
80
        obj = self.unrestrictedTraverse(object_path)
81 82
        if obj is None:
          # object doesn't exist any longer
83
          pass
84
        else:
85 86 87 88 89 90
          connection = obj._p_jar
          o = obj
          while connection is None:
            o = o.aq_parent
            connection=o._p_jar
          copy = connection.exportFile(obj._p_oid)
91 92 93 94 95 96 97 98 99
          # import object in trash
          connection = backup_object_container._p_jar
          o = backup_object_container
          while connection is None:
            o = o.aq_parent
            connection=o._p_jar
          copy.seek(0)
          backup = connection.importFile(copy)
          try:
100
            backup.isIndexable = 0
101 102 103 104 105 106 107
            backup_object_container._setObject(object_id, backup)
          except AttributeError:
            # XXX we can go here due to formulator because attribute field_added
            # doesn't not exists on parent if it is a Trash Folder and not a Form
            # so object is not backup
            LOG("Trash Tool backupObject", 100, "Can't backup object %s" %(object_id))
            pass
108 109 110 111 112 113 114 115 116

    keep_sub = kw.get('keep_subobjects', 0)
    subobjects_dict = {}

    if not keep_sub:
      # export subobjects
      if save:
        obj = backup_object_container._getOb(object_id)
      else:
Alexandre Boeglin's avatar
Alexandre Boeglin committed
117
        object_path = container_path + [object_id]
118
        obj = self.unrestrictedTraverse(object_path)
119 120
      if obj is None:
        pass
121
      # in case of portal types, export properties instead of subobjects
122
      elif getattr(obj, 'meta_type', None) == 'ERP5 Type Information':
123 124 125 126 127 128 129 130 131 132 133
        subobjects_dict = {}
        subobjects_dict['allowed_content_type_list'] = getattr(obj, 'allowed_content_types', []) or []
        subobjects_dict['hidden_content_type_list'] = getattr(obj, 'hidden_content_type_list', []) or []
        subobjects_dict['property_sheet_list'] = getattr(obj, 'property_sheet_list', []) or []
        subobjects_dict['base_category_list'] = getattr(obj, 'base_category_list', []) or []
        subobjects_dict['roles_list'] =  getattr(obj, '_roles', []) or []
        action_list = obj.listActions() or []
        subobjects_dict['action_list'] = []
        for action in action_list:
          subobjects_dict['action_list'].append(action._getCopy(obj))
        wf_chain = getChainByType(self.getPortalObject())[1]
134 135
        if wf_chain.has_key('chain_%s' % object_id):
          subobjects_dict['workflow_chain'] = wf_chain['chain_%s' % object_id]
136 137
        else:
          subobjects_dict['workflow_chain'] = ''
138 139 140 141 142 143 144 145
      else:
        for subobject_id in list(obj.objectIds()):
          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
            obj.manage_delObjects([subobject_id])
Aurel's avatar
Aurel committed
146 147 148 149 150 151 152 153 154
    return subobjects_dict

  def newTrashBin(self, bt_title='trash', bt=None):
    """
      Create a new trash bin at upgrade of bt
    """
    # construct date
    date = DateTime()
    start_date = date.strftime('%Y-%m-%d')
155 156 157 158 159 160 161 162 163

    def getBaseTrashId():
      ''' A little function to get an id without leading underscore
      '''
      base_id = '%s' % start_date
      if bt_title not in ('', None):
        base_id = '%s_%s' % (bt_title, base_id)
      return base_id

Aurel's avatar
Aurel committed
164 165 166
    # generate id
    trash_ids = self.objectIds()
    n = 0
167 168 169 170
    new_trash_id = getBaseTrashId()
    while new_trash_id in trash_ids:
      n += 1
      new_trash_id = '%s_%s' % (getBaseTrashId(), n)
Aurel's avatar
Aurel committed
171
    # create trash bin
172 173 174 175 176 177
    trashbin = self.newContent( portal_type     = 'Trash Bin'
                              , id              = new_trash_id
                              , title           = bt_title
                              , start_date      = start_date
                              , causality_value = bt
                              )
Aurel's avatar
Aurel committed
178 179 180 181 182 183
    return trashbin

  def getTrashBinObjectsList(self, trashbin):
    """
      Return a list of trash objects for a given trash bin
    """
184
    def getChildObjects(obj):
Aurel's avatar
Aurel committed
185
      object_list = []
186 187 188 189 190
      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
191
      if len(childObjects) > 0:
192
        for o in childObjects:
193
          object_list.extend(getChildObjects(o))
Aurel's avatar
Aurel committed
194
      else:
195
        object_list.append(obj)
Aurel's avatar
Aurel committed
196
      return object_list
197

Aurel's avatar
Aurel committed
198 199 200 201 202
    list = getChildObjects(trashbin)
    list.sort()
    return list

InitializeClass(TrashTool)