Back to index

plone3  3.1.7
Public Member Functions | Static Public Attributes | Private Member Functions
Archetypes.AllowedTypesByIface.AllowedTypesByIfaceMixin Class Reference

List of all members.

Public Member Functions

def allowedContentTypes
def invokeFactory

Static Public Attributes

tuple allowed_interfaces = ()

Private Member Functions

def _verifyObjectPaste

Detailed Description

An approach to restrict allowed content types in a container by
the interfaces they implement.

Notice that extending this class means surpassing allowed_content_types,
filter_content_types etc in the FTI, while we are still concerned about
security.

ATBIFolder is an example type that uses AllowedTypesByIfaceMixin:

>>> self.folder.invokeFactory('ATBIFolder', 'f')
'f'

>>> f = self.folder.f

f has an empty list of allowed_interfaces, so it doesn't allow anything
right now:

>>> f.allowedContentTypes()
[]

invokeFactory will fail:

>>> try:
...     f.invokeFactory('SimpleType', 'st')
... except ValueError:
...     print 'Right'
Right

Now we restrict allowed_interfaces to IBaseFolder:

>>> from Products.Archetypes.interfaces.base import *
>>> f.allowed_interfaces = (IBaseFolder,)

And try to add a SimpleType, which fails again:

>>> try:
...     f.invokeFactory('SimpleType', 'st')
... except ValueError:
...     print 'Right'
Right

SimpleFolder implements IBaseFolder:

>>> f.invokeFactory('SimpleFolder', 'sf')
'sf'

A content object only needs to implement one of allowed_interfaces:

>>> from Interface import Interface
>>> class SomeInterface(Interface): pass
>>> f.allowed_interfaces = (IBaseFolder, SomeInterface)
>>> f.invokeFactory('SimpleFolder', 'sf2')
'sf2'
>>> try:
...     f.invokeFactory('SimpleType', 'sf')
... except ValueError:
...     print 'Right'
Right

Definition at line 30 of file AllowedTypesByIface.py.


Member Function Documentation

def Archetypes.AllowedTypesByIface.AllowedTypesByIfaceMixin._verifyObjectPaste (   self,
  object,
  validate_src = 1 
) [private]
Overrides PortalFolder._verifyObjectPaste.

Definition at line 126 of file AllowedTypesByIface.py.

00126 
00127     def _verifyObjectPaste(self, object, validate_src=1):
00128         """Overrides PortalFolder._verifyObjectPaste."""
00129         # XXX: What we do here is trick
00130         #      PortalFolder._verifyObjectPaste in its check for
00131         #      allowed content types. We make our typeinfo temporarily
00132         #      unavailable.
00133         pt = getToolByName(self, 'portal_types')
00134         tmp_name = '%s_TMP' % self.portal_type
00135         ti = pt.getTypeInfo(self.portal_type)
00136         pt.manage_delObjects([self.portal_type])
00137         try:
00138             value = BaseFolder._verifyObjectPaste(self, object, validate_src)
00139         finally:
00140             pt._setObject(self.portal_type, ti)
00141         return value

Here is the call graph for this function:

Redefines CMF PortalFolder's allowedContentTypes.

Definition at line 97 of file AllowedTypesByIface.py.

00097 
00098     def allowedContentTypes(self):
00099         """Redefines CMF PortalFolder's allowedContentTypes."""
00100         at = getToolByName(self, 'archetype_tool')
00101         return at.listPortalTypesWithInterfaces(self.allowed_interfaces)

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.AllowedTypesByIface.AllowedTypesByIfaceMixin.invokeFactory (   self,
  type_name,
  id,
  RESPONSE = None,
  args,
  kwargs 
)
Invokes the portal_types tool.

Overrides PortalFolder.invokeFactory.

Definition at line 102 of file AllowedTypesByIface.py.

00102 
00103     def invokeFactory(self, type_name, id, RESPONSE = None, *args, **kwargs):
00104         """Invokes the portal_types tool.
00105 
00106         Overrides PortalFolder.invokeFactory."""
00107         pt = getToolByName(self, 'portal_types')
00108         at = getToolByName(self, 'archetype_tool')
00109         fti = None
00110         for t in listTypes():
00111             if t['portal_type'] == type_name:
00112                 fti = t
00113                 break
00114         
00115         if fti is None:
00116             raise ValueError, "Type %r not available." % type_name
00117 
00118         if not at.typeImplementsInterfaces(fti, self.allowed_interfaces):
00119             raise ValueError, "Type %r does not implement any of %s." % \
00120                   (type_name, self.allowed_interfaces)
00121 
00122         args = (type_name, self, id, RESPONSE) + args
00123         new_id = pt.constructContent(*args, **kwargs)
00124         if not new_id: new_id = id
00125         return new_id

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 95 of file AllowedTypesByIface.py.


The documentation for this class was generated from the following file: