Back to index

plone3  3.1.7
Classes | Functions | Variables
CMFEditions.utilities Namespace Reference

Classes

class  KwAsAttributes

Functions

def dereference
def generateId
def wrap

Variables

string STUB_OBJECT_PREFIX = '_CMFEditionsTempId'

Function Documentation

def CMFEditions.utilities.dereference (   obj = None,
  history_id = None,
  zodb_hook = None 
)
Dereference an object.

Works with either an obj or a history_id or both.

If only a history_id is used, then a 'zodb_hook' is required to obtain
the uid tool.

Returns a tuple consisting of the derefrenced object and
the unique id of the object: ``(obj, uid)``

If an object or history_id cannot be found None will be returned for
one or both values.

Definition at line 42 of file utilities.py.

00042 
00043 def dereference(obj=None, history_id=None, zodb_hook=None):
00044     """Dereference an object.
00045 
00046     Works with either an obj or a history_id or both.
00047 
00048     If only a history_id is used, then a 'zodb_hook' is required to obtain
00049     the uid tool.
00050 
00051     Returns a tuple consisting of the derefrenced object and
00052     the unique id of the object: ``(obj, uid)``
00053 
00054     If an object or history_id cannot be found None will be returned for
00055     one or both values.
00056     """
00057 
00058     if zodb_hook is None:
00059         # try to use the reference as zodb hook
00060         zodb_hook = obj
00061 
00062     portal_uidhandler = getToolByName(zodb_hook, 'portal_historyidhandler')
00063 
00064     if history_id is None:
00065         if obj is None:
00066             raise TypeError, "This method requires either an obj or a history_id"
00067         else:
00068             history_id = portal_uidhandler.queryUid(obj, None)
00069     elif obj is None:
00070         try:
00071             obj = portal_uidhandler.queryObject(history_id, None)
00072         except AttributeError:
00073             # We may get an attribute error in some cases, just return None
00074             pass
00075 
00076     return obj, history_id
00077 

Here is the call graph for this function:

Here is the caller graph for this function:

def CMFEditions.utilities.generateId (   parent,
  prefix = '',
  volatile = False 
)
Generate an unused id (optionaly a volatile one).

Definition at line 78 of file utilities.py.

00078 
00079 def generateId(parent, prefix='', volatile=False):
00080     """Generate an unused id (optionaly a volatile one).
00081     """
00082     existingIds = parent.objectIds()
00083     idTemplate = '%s%s_%%s' % (volatile * '__v_', prefix + STUB_OBJECT_PREFIX)
00084     while 1:
00085         id =  idTemplate % random.randrange(1000000)
00086         if id not in existingIds:
00087             return id
00088 

Here is the caller graph for this function:

def CMFEditions.utilities.wrap (   obj,
  parent 
)
Copy the context and containment from one object to another.

This is needed to allow acquiring attributes. Containment and context
is setup only in direction to the parents but not from the parent
to itself. So doing the following raises an ``AttributeError``::

    getattr(wrapped.aq_parent, tempAttribute)

Definition at line 89 of file utilities.py.

00089 
00090 def wrap(obj, parent):
00091     """Copy the context and containment from one object to another.
00092 
00093     This is needed to allow acquiring attributes. Containment and context
00094     is setup only in direction to the parents but not from the parent
00095     to itself. So doing the following raises an ``AttributeError``::
00096 
00097         getattr(wrapped.aq_parent, tempAttribute)
00098     """
00099     # be sure the obj is unwraped before wrapping it (argh, having
00100     # caused pulling my hair out until I realized it is wrapped)
00101     obj = aq_base(obj).__of__(parent)
00102 
00103     # set containment temporarly
00104     tempAttribute = generateId(parent, volatile=True)
00105     changed = parent._p_changed
00106     setattr(parent, tempAttribute, obj)
00107     wrapped = getattr(parent, tempAttribute)
00108     delattr(parent, tempAttribute)
00109     parent._p_changed = changed
00110 
00111     return wrapped

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

string CMFEditions.utilities.STUB_OBJECT_PREFIX = '_CMFEditionsTempId'

Definition at line 32 of file utilities.py.