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

# Source Code for Module Products.ZenRelations.PrimaryPathObjectManager

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
      4  #
      5  # This content is made available according to terms specified in
      6  # License.zenoss under the directory where your Zenoss product is installed.
      7  #
      8  ##############################################################################
      9
     10
     11  __doc__="""PrimaryPathObjectManager
     12
     13  $Id: RelationshipBase.py,v 1.26 2003/10/03 16:16:01 edahl Exp $"""
     14
     15  __version__ = "$Revision: 1.26 $"[11:-2]
     16
     17  import logging
     18  log = logging.getLogger("zen.PrimaryPathObjectManager")
     19
     20  # base classes for PrimaryPathObjectManager
     21  from RelCopySupport import RelCopyContainer
     22  from Acquisition import Implicit, aq_base
     23  from OFS.ObjectManager import ObjectManager
     24  from AccessControl.Role import RoleManager
     25  import App.Undo
     26
     27  from ZItem import ZItem
     28
     29
     30
     31  from Products.BTreeFolder2.BTreeFolder2 import BTreeFolder2
     32
     33  _MARKER = object()
     34
     35 -class PrimaryPathManager(ZItem, Implicit, RoleManager):
     36
     37 -    def getPrimaryPath(self, fromNode=None):
     38          """
     39          Return the primary path of this object by following __primary_parent__
     40          """
     41          ppath = []
     42          obj = aq_base(self)
     43          while True:
     44              ppath.append(obj.id)
     45              parent = getattr(obj, "__primary_parent__", None)
     46              if parent is None: break
     47              obj = parent
     48          ppath.reverse()
     49          basepath = getattr(obj, "zPrimaryBasePath", [])
     50          for i in range(len(basepath)-1,-1,-1): ppath.insert(0,basepath[i])
     51          try:
     52              idx = ppath.index(fromNode)
     53              ppath = ppath[idx+1:]
     54          except ValueError: pass
     55          return tuple(ppath)
     56
     57
     58 -    def getPrimaryId(self, fromNode=None):
     59          """Return the primary path in the form /zport/dmd/xyz"""
     60          pid = "/".join(self.getPrimaryPath(fromNode))
     61          if fromNode: pid = "/" + pid
     62          return pid
     63
     64
     65 -    def getPrimaryUrlPath(self, full=False):
     66          """Return the primary path as an absolute url"""
     67          objaq = self.primaryAq()
     68          if full: return objaq.absolute_url()
     69          return objaq.absolute_url_path()
     70
     71 -    def primaryAq(self):
     72          """Return self with is acquisition path set to primary path"""
     73          parent = getattr(self, "__primary_parent__", _MARKER)
     74          if parent is _MARKER: # dmd - no __primary_parent__
     75              base = self.getPhysicalRoot().zport
     76              return aq_base(self).__of__(base)
     77          if parent is None: # Deleted object
     78              raise KeyError(self.id)
     79          return aq_base(self).__of__(parent.primaryAq())
     80
     81 -    def getPrimaryParent(self):
     82          """Return our parent object by our primary path"""
     83          return self.__primary_parent__.primaryAq()
     84
     85
     86 -class PrimaryPathObjectManager(
     87              RelCopyContainer,
     88              ObjectManager,
     89              PrimaryPathManager,
     90              App.Undo.UndoSupport,
     91              ):
     92      """
     93      PrimaryPathObjectManager with basic Zope persistent classes.
     94      """
     95      manage_options = (ObjectManager.manage_options +
     96                        RoleManager.manage_options +
     97                        ZItem.manage_options)
     98
     99 -    def _setObject(self, id, obj, roles=None, user=None, set_owner=1):
    100          """Track __primary_parent__ when we are set into an object"""
    101          obj.__primary_parent__ = aq_base(self)
    102          return ObjectManager._setObject(self, id, obj, roles, user, set_owner)
    103
    104
    105 -    def _delObject(self, id, dp=1, suppress_events=False):
    106          """When deleted clear __primary_parent__."""
    107          obj = self._getOb(id, None)
    108          if obj is None:
    109              # Added this check because we are seeing stack traces in the UI.
    110              # We aren't 100% sure what is causing the object to disappear from
    111              # the ObjectManager. It could be that a different user had already
    112              # deleted it or that a single user had two brower tabs open. Ian saw
    113              # a case were the references on an object were wrong (getPrimaryId
    114              # pointed to the wrong location) but I'm not sure that is what is
    115              # causing this problem. -EAD
    116              log.warning(
    117              "Tried to delete object id '%s' but didn't find it on %s",
    118              id, self.getPrimaryId())
    119              return
    120          ObjectManager._delObject(self, id, dp, suppress_events)
    121          obj.__primary_parent__ = None
    122
    123
    124 -class PrimaryPathBTreeFolder2(BTreeFolder2):
    125      """
    126      BTreeFolder2 PrimaryPathObjectManager.
    127      """
    128 -    def _setObject(self, id, obj, roles=None, user=None, set_owner=1):
    129          """Track __primary_parent__ when we are set into an object"""
    130          obj.__primary_parent__ = aq_base(self)
    131          return ObjectManager._setObject(self, id, obj, roles, user, set_owner)
    132
    133
    134 -    def _delObject(self, id, dp=1, suppress_events=False):
    135          """When deleted clear __primary_parent__."""
    136          obj = self._getOb(id)
    137          ObjectManager._delObject(self, id, dp, suppress_events)
    138          obj.__primary_parent__ = None
    139