Back to index

plone3  3.1.7
Classes | Functions | Variables
borg.localrole.workspace Namespace Reference

Classes

class  WorkspaceLocalRoleManager

Functions

def manage_addWorkspaceLocalRoleManager
def clra_cache_key
def store_on_request

Variables

tuple manage_addWorkspaceLocalRoleManagerForm

Function Documentation

def borg.localrole.workspace.clra_cache_key (   method,
  self,
  user,
  obj,
  object_roles 
)
The cache key needs to include all arguments when caching allowed
    local roles, but the key function also needs to decide whether
    `volatile.cache` can cache or not by checking if it's possible to
    get a request instance from the object.

    To test we'll nee an adaptable object, a user and the method which
    results' we'd like to cache:

      >>> from zope.interface import implements, Interface
      >>> class DummyObject(object):
      ...     implements(Interface)
      >>> obj = DummyObject()

      >>> from borg.localrole.tests import DummyUser
      >>> john = DummyUser('john')

      >>> rm = WorkspaceLocalRoleManager('rm', 'A Role Manager')
      >>> fun = rm.__class__.checkLocalRolesAllowed

    The dummy object doesn't have an acquired request, so no caching
    can be done:

      >>> clra_cache_key(fun, 'me', john, obj, ['foo', 'bar'])
      Traceback (most recent call last):
      ...
      DontCache

    So let's add one and try again.  Before we also need to mark it as
    being annotatable, which normally happens elsewhere:

      >>> from ZPublisher.HTTPRequest import HTTPRequest
      >>> request = HTTPRequest('', dict(HTTP_HOST='nohost:8080'), {})

      >>> from Products.Five.zcml import load_config
      >>> import zope.component
      >>> import zope.annotation
      >>> load_config('meta.zcml', zope.component)
      >>> load_config('configure.zcml', zope.annotation)
      >>> from zope.interface import classImplements
      >>> from zope.annotation.interfaces import IAttributeAnnotatable
      >>> classImplements(HTTPRequest, IAttributeAnnotatable)

      >>> obj.REQUEST = request
      >>> clra_cache_key(fun, 'hmm', john, obj, ['foo', 'bar'])
      ('john', ..., ('foo', 'bar'))

    If the objects happens to have a `getPhysicalPath` method, that should
    be used instead of the hash:

      >>> class DummyObjectWithPath(DummyObject):
      ...     def getPhysicalPath(self):
      ...         return '42!'
      >>> obj = DummyObjectWithPath()
      >>> obj.REQUEST = request
      >>> clra_cache_key(fun, 'hmm', john, obj, ['foo', 'bar'])
      ('john', '42!', ('foo', 'bar'))

    Now let's check if the results of a call to `checkLocalRolesAllowed`
    is indeed cached, i.e. is the request was annotated correctly.  First
    try to log the method invocation, though.  As monkey patching in
    something between the original method and the already applied cache
    decorator is tricky, we abuse `_get_userfolder`, which is called
    first thing in `checkLocalRolesAllowed`:

      >>> original = rm._get_userfolder
      >>> def logger(self, *args, **kw):
      ...     print 'checkLocalRolesAllowed called...'
      ...     return original(self, *args, **kw)
      >>> rm._get_userfolder = logger

      >>> print rm.checkLocalRolesAllowed(john, obj, ['foo', 'bar'])
      checkLocalRolesAllowed called...
      None
      >>> IAnnotations(request)
      {"borg.localrole.workspace.checkLocalRolesAllowed:('john', '42!', ('foo', 'bar'))": None}

    Calling the method a second time should directly return the cached
    value, i.e. the logger shouldn't print anything:

      >>> print rm.checkLocalRolesAllowed(john, obj, ['foo', 'bar'])
      None

Definition at line 36 of file workspace.py.

00036 
00037 def clra_cache_key(method, self, user, obj, object_roles):
00038     """ The cache key needs to include all arguments when caching allowed
00039         local roles, but the key function also needs to decide whether
00040         `volatile.cache` can cache or not by checking if it's possible to
00041         get a request instance from the object.
00042 
00043         To test we'll nee an adaptable object, a user and the method which
00044         results' we'd like to cache:
00045 
00046           >>> from zope.interface import implements, Interface
00047           >>> class DummyObject(object):
00048           ...     implements(Interface)
00049           >>> obj = DummyObject()
00050 
00051           >>> from borg.localrole.tests import DummyUser
00052           >>> john = DummyUser('john')
00053 
00054           >>> rm = WorkspaceLocalRoleManager('rm', 'A Role Manager')
00055           >>> fun = rm.__class__.checkLocalRolesAllowed
00056 
00057         The dummy object doesn't have an acquired request, so no caching
00058         can be done:
00059 
00060           >>> clra_cache_key(fun, 'me', john, obj, ['foo', 'bar'])
00061           Traceback (most recent call last):
00062           ...
00063           DontCache
00064 
00065         So let's add one and try again.  Before we also need to mark it as
00066         being annotatable, which normally happens elsewhere:
00067 
00068           >>> from ZPublisher.HTTPRequest import HTTPRequest
00069           >>> request = HTTPRequest('', dict(HTTP_HOST='nohost:8080'), {})
00070 
00071           >>> from Products.Five.zcml import load_config
00072           >>> import zope.component
00073           >>> import zope.annotation
00074           >>> load_config('meta.zcml', zope.component)
00075           >>> load_config('configure.zcml', zope.annotation)
00076           >>> from zope.interface import classImplements
00077           >>> from zope.annotation.interfaces import IAttributeAnnotatable
00078           >>> classImplements(HTTPRequest, IAttributeAnnotatable)
00079 
00080           >>> obj.REQUEST = request
00081           >>> clra_cache_key(fun, 'hmm', john, obj, ['foo', 'bar'])
00082           ('john', ..., ('foo', 'bar'))
00083 
00084         If the objects happens to have a `getPhysicalPath` method, that should
00085         be used instead of the hash:
00086 
00087           >>> class DummyObjectWithPath(DummyObject):
00088           ...     def getPhysicalPath(self):
00089           ...         return '42!'
00090           >>> obj = DummyObjectWithPath()
00091           >>> obj.REQUEST = request
00092           >>> clra_cache_key(fun, 'hmm', john, obj, ['foo', 'bar'])
00093           ('john', '42!', ('foo', 'bar'))
00094 
00095         Now let's check if the results of a call to `checkLocalRolesAllowed`
00096         is indeed cached, i.e. is the request was annotated correctly.  First
00097         try to log the method invocation, though.  As monkey patching in
00098         something between the original method and the already applied cache
00099         decorator is tricky, we abuse `_get_userfolder`, which is called
00100         first thing in `checkLocalRolesAllowed`:
00101 
00102           >>> original = rm._get_userfolder
00103           >>> def logger(self, *args, **kw):
00104           ...     print 'checkLocalRolesAllowed called...'
00105           ...     return original(self, *args, **kw)
00106           >>> rm._get_userfolder = logger
00107 
00108           >>> print rm.checkLocalRolesAllowed(john, obj, ['foo', 'bar'])
00109           checkLocalRolesAllowed called...
00110           None
00111           >>> IAnnotations(request)
00112           {"borg.localrole.workspace.checkLocalRolesAllowed:('john', '42!', ('foo', 'bar'))": None}
00113 
00114         Calling the method a second time should directly return the cached
00115         value, i.e. the logger shouldn't print anything:
00116 
00117           >>> print rm.checkLocalRolesAllowed(john, obj, ['foo', 'bar'])
00118           None
00119 
00120     """
00121     request = aq_get(obj, 'REQUEST', None)
00122     if IAnnotations(request, None) is None:
00123         raise DontCache
00124     try:
00125         oid = obj.getPhysicalPath()
00126     except AttributeError:
00127         oid = id(obj)
00128     return (user.getId(), oid, tuple(object_roles))

def borg.localrole.workspace.manage_addWorkspaceLocalRoleManager (   dispatcher,
  id,
  title = None,
  REQUEST = None 
)
Add a WorkspaceLocalRoleManager to a Pluggable Authentication Services.

Definition at line 24 of file workspace.py.

00024 
00025 def manage_addWorkspaceLocalRoleManager(dispatcher, id, title=None, REQUEST=None):
00026     """Add a WorkspaceLocalRoleManager to a Pluggable Authentication Services."""
00027     wlrm = WorkspaceLocalRoleManager(id, title)
00028     dispatcher._setObject(wlrm.getId(), wlrm)
00029 
00030     if REQUEST is not None:
00031         REQUEST.RESPONSE.redirect(
00032                 '%s/manage_workspace?manage_tabs_message=WorkspaceLocalRoleManager+added.'
00033                 % dispatcher.absolute_url())
00034 
00035 
# memoize support for `checkLocalRolesAllowed`

Here is the caller graph for this function:

def borg.localrole.workspace.store_on_request (   method,
  self,
  user,
  obj,
  object_roles 
)
helper for caching local roles on the request 

Definition at line 129 of file workspace.py.

00129 
00130 def store_on_request(method, self, user, obj, object_roles):
00131     """ helper for caching local roles on the request """
00132     return IAnnotations(aq_get(obj, 'REQUEST'))
00133 


Variable Documentation

Initial value:
00001 PageTemplateFile(
00002         "zmi/WorkspaceLocalRoleManagerForm.pt", globals(),
00003         __name__="manage_addWorkspaceRoleManagerForm")

Definition at line 20 of file workspace.py.