Package Products :: Package ZenRelations :: Module RelCopySupport |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenRelations.RelCopySupport

      1  ###########################################################################
      2  #
      3  # This program is part of Zenoss Core, an open source monitoring platform.
      4  # Copyright (C) 2007, Zenoss Inc.
      5  #
      6  # This program is free software; you can redistribute it and/or modify it
      7  # under the terms of the GNU General Public License version 2 as published by
      8  # the Free Software Foundation.
      9  #
     10  # For complete information please visit: http://www.zenoss.com/oss/
     11  #
     12  ###########################################################################
     13
     14  """MultiCopySupport
     15
     16  MultiCopySupport over rides manage_pasteObjects in CopySupport to handle
     17  cut/past for a MultiItem.  Semantics of cut/paste are to remove time from
     18  current container and put it in new container but other container mappings
     19  remain.
     20
     21  $Id: RelCopySupport.py,v 1.15 2004/04/13 22:02:18 edahl Exp $"""
     22
     23  __version__ = '$Revision: 1.15 $'
     24
     25  import sys
     26  import types
     27  from cgi import escape
     28
     29  # base class for RelCopyContainer
     30  from OFS.ObjectManager import checkValidId
     31  from OFS.CopySupport import CopyContainer, eNotSupported
     32
     33  from webdav.Lockable import ResourceLockedError
     34
     35  from Acquisition import aq_base
     36  from AccessControl import getSecurityManager
     37
     38  from OFS import Moniker
     39  from OFS.CopySupport import CopyError, _cb_decode, eInvalid, eNotFound, eNoData
     40
     41  from App.Dialogs import MessageDialog
     42
     43  from Products.ZenRelations.Exceptions import *
     44
     45 -class RelCopyContainer(CopyContainer):
     46
     47 -    def manage_renameObject(self, id, new_id, REQUEST=None):
     48          """Rename a particular sub-object"""
     49          try:
     50              checkValidId(self, new_id)
     51          except:
     52              raise CopyError, sys.exc_info()[1]
     53
     54          ob=self._getOb(id)
     55          if ob.wl_isLocked():
     56              raise ResourceLockedError(
     57                      'Object "%s" is locked via WebDAV' % ob.getId())
     58          if not ob.cb_isMoveable():
     59              raise CopyError, eNotSupported % escape(id)
     60          self._verifyObjectPaste(ob)
     61
     62          try:
     63              ob._notifyOfCopyTo(self, op=2) # -EAD add rename to semantics
     64          except:
     65              raise CopyError, sys.exc_info()[1]
     66          self._delObject(id)
     67          ob = aq_base(ob)
     68          ob._setId(new_id)
     69
     70          # Note - because a rename always keeps the same context, we
     71          # can just leave the ownership info unchanged.
     72          self._setObject(new_id, ob, set_owner=0)
     73          ob = self._getOb(new_id)
     74          ob._postCopy(self, op=1)
     75
     76          if REQUEST is not None:
     77              return self.manage_main(self, REQUEST, update_menu=1)
     78          return None
     79
     80
     81 -    def manage_linkObjects(self, ids = None, cb_copy_data=None, REQUEST=None):
     82          """link objects to relationship"""
     83          try:
     84              relName = self._getRelName(ids)
     85              oblist = self._getSourceObjects(cb_copy_data, REQUEST)
     86              for obj in oblist:
     87                  self.manage_addRelation(relName, obj)
     88          except ZenRelationsError, e:
     89              if REQUEST: return MessageDialog(title = "Relationship Link Error",
     90                                  message = str(e), action = "manage_main")
     91              else: raise
     92          if REQUEST: return self.manage_main(self, REQUEST)
     93
     94
     95
     96 -    def manage_unlinkObjects(self, ids = None, cb_copy_data=None, REQUEST=None):
     97          """unlink objects from relationship"""
     98          from Products.ZenUtils.Utils import unused
     99          unused(cb_copy_data)
    100          try:
    101              relName = self._getRelName(ids)
    102              self.manage_removeRelation(relName)
    103          except ZenRelationsError, e:
    104              if REQUEST:return MessageDialog(title = "Relationship Unlink Error",
    105                                  message = str(e), action = "manage_main")
    106              else: raise
    107          if REQUEST: return self.manage_main(self, REQUEST)
    108
    109
    110
    111 -    def _verifyObjectPaste(self, object, validate_src=1):
    112          """
    113          check to see if this object is allowed to be pasted into this path
    114          """
    115          pathres = getattr(object, 'relationshipManagerPathRestriction', None)
    116          if (pathres and '/'.join(self.getPhysicalPath()).find(pathres) == -1):
    117              raise CopyError, MessageDialog(title='Not Supported',
    118                    message='The object <EM>%s</EM> can not be pasted into' \
    119                            ' the path <EM>%s</EM>' %
    120                             (object.id, '/'.join(self.getPhysicalPath())),
    121                    action='manage_main')
    122          CopyContainer._verifyObjectPaste(self,object,validate_src)
    123
    124
    125 -    def _getRelName(self, ids):
    126          """
    127          Return our relationship name from the UI.
    128          If there is more than one id defined raise because only one
    129          target relationship can be defined.  If no ids are defined
    130          check to see that we are a ToManyRelationship and return self.id.
    131          """
    132          if not ids:
    133              if self.meta_type == "ToManyRelationship":
    134                  return self.getId()
    135              else:
    136                  raise ZenRelationsError("No relation name defined")
    137          if type(ids) == types.StringType: return ids
    138          if len(ids) > 1:
    139              raise ZenRelationsError("You can only link to one relationship!")
    140          return ids[0]
    141
    142
    143 -    def _verifyObjectLink(self):
    144          """
    145          When linking check that the user has "Copy or Move" permission
    146          on the relation.  Can't use _verifyObjectPaste because there
    147          is an empty all_meta_types on ToManyRelations which causes it
    148          to falsely fail.
    149          """
    150          if not getSecurityManager().checkPermission('Copy or Move', self):
    151              message = ('You do not possess the "Copy or Move" permission in '
    152                  'the context of the container into which you are '
    153                  'pasting, thus you are not able to perform '
    154                  'this operation.')
    155              raise CopyError, MessageDialog(title = 'Insufficient Privileges',
    156                                  message = message, action = 'manage_main')
    157
    158
    159
    160
    161 -    def _getSourceObjects(self, cb_copy_data, REQUEST):
    162          """get the source objects to link"""
    163          cp=None
    164          if cb_copy_data is not None:
    165              cp=cb_copy_data
    166          else:
    167              if REQUEST and REQUEST.has_key('__cp'):
    168                  cp=REQUEST['__cp']
    169          if cp is None:
    170              raise CopyError, eNoData
    171
    172          try:    cp=_cb_decode(cp)
    173          except: raise CopyError, eInvalid
    174
    175          oblist=[]
    176          app = self.getPhysicalRoot()
    177
    178          for mdata in cp[1]:
    179              m = Moniker.loadMoniker(mdata)
    180              try: ob = m.bind(app)
    181              except: raise CopyError, eNotFound
    182              self._verifyObjectLink()
    183              oblist.append(ob)
    184          return oblist
    185