zsqlbrain.py 3.34 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL. All Rights Reserved.
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################

import string
import Acquisition
Sebastien Robin's avatar
Sebastien Robin committed
17
import sys
Yoshinori Okuji's avatar
Yoshinori Okuji committed
18
from ZODB.POSException import ConflictError
Jean-Paul Smets's avatar
Jean-Paul Smets committed
19

20 21 22
from AccessControl import ClassSecurityInfo
from AccessControl.SecurityInfo import allow_class

Jean-Paul Smets's avatar
Jean-Paul Smets committed
23 24 25
from zLOG import LOG

class  ZSQLBrain(Acquisition.Implicit):
26 27
  security = ClassSecurityInfo()
  security.declareObjectPublic()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
28

29 30 31 32 33 34
  def __init__(self) :
    """Init the brain and make sure path was retrieved from the RDB"""
    if 'path' not in dir(self) and 'PATH' not in dir(self):
      raise ValueError, "Unable to use ZSQLBrain if ZSQL Method does "\
                        "not retrieves the `path` column from catalog table."

Yoshinori Okuji's avatar
Yoshinori Okuji committed
35 36 37
  def _aq_dynamic(self, name):
    """Acquire an attribute from a real object.
    """
38 39
    if name.startswith('__') :
      return None
Yoshinori Okuji's avatar
Yoshinori Okuji committed
40 41
    o = self.getObject()
    return getattr(o, name, None)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
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 67 68 69 70 71 72 73 74 75 76 77 78 79

#   def __getattr__(self, key):
#     return "toto"
#     if hasattr(self, key):
#       return self.__dict__[key]
#     elif hasattr(ZSQLBrain, key):
#       return ZSQLBrain.__dict__[key]
#     else:
#       if self.o_self is None:
#         self.o_self = self.getObject()
#       if self.o_self is not None:
#         try:
#           result = self.o_self.getProperty(key)
#           self.__dict__[key] = result
#         except:
#           result = 'Can not evaluate attribute: %s' % cname_id
#       else:
#           result = 'Object does not exist'
#       return result

  def getURL(self):
    return self.path

  def getUrl(self):
    return self.path

  def getPath(self):
    return self.path

  def getUid(self):
    return self.uid

  getRID = getUid

  def getObject(self, REQUEST=None):
    """Try to return the object for this record"""
    try:
      obj = self.aq_parent.unrestrictedTraverse(self.getPath())
80
      if obj is None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
81 82 83 84
        if REQUEST is None:
          REQUEST = self.REQUEST
        obj = self.aq_parent.portal_catalog.resolve_url(self.getPath(), REQUEST)
      return obj
Yoshinori Okuji's avatar
Yoshinori Okuji committed
85 86
    except ConflictError:
      raise
Jean-Paul Smets's avatar
Jean-Paul Smets committed
87
    except:
Sebastien Robin's avatar
Sebastien Robin committed
88
      LOG("ZCatalog WARNING",0,"Could not access object path %s" % self.getPath(), error=sys.exc_info() )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
89 90 91 92 93 94 95
      return None

  def absolute_url(self):
    """
      returns the path stored in the Catalog
    """
    return self.path
96 97

  def resolve_url(self, path, REQUEST):
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    """
      Taken from ZCatalog

      Attempt to resolve a url into an object in the Zope
      namespace. The url may be absolute or a catalog path
      style url. If no object is found, None is returned.
      No exceptions are raised.
    """
    script=REQUEST.script
    if string.find(path, script) != 0:
      path='%s/%s' % (script, path)
    try:
      return REQUEST.resolve_url(path)
    except ConflictError:
      raise
    except:
      pass
115

116
allow_class(ZSQLBrain)