Commit 60b45a95 authored by Jean-Paul Smets's avatar Jean-Paul Smets

Updated to provide mixin used by ERP5Site for example.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@256 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 9c5443bd
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
from Acquisition import aq_base, aq_self from Acquisition import aq_base, aq_self
import ExtensionClass
from Products.CMFCore.utils import _getAuthenticatedUser from Products.CMFCore.utils import _getAuthenticatedUser
...@@ -37,7 +38,6 @@ from Products.ERP5Type import PropertySheet, Permissions ...@@ -37,7 +38,6 @@ from Products.ERP5Type import PropertySheet, Permissions
from Products.BTreeFolder2.CMFBTreeFolder import CMFBTreeFolder from Products.BTreeFolder2.CMFBTreeFolder import CMFBTreeFolder
import threading
import os import os
from zLOG import LOG from zLOG import LOG
...@@ -52,7 +52,106 @@ def dummyFilter(object,REQUEST=None): ...@@ -52,7 +52,106 @@ def dummyFilter(object,REQUEST=None):
def dummyTestAfter(object,REQUEST=None): def dummyTestAfter(object,REQUEST=None):
return [] return []
class Folder( CopyContainer, CMFBTreeFolder, Base): class FolderMixIn(ExtensionClass.Base):
# Declarative security
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.View)
security.declareProtected(Permissions.AddPortalContent, 'newContent')
def newContent(self, id=None, portal_type=None, id_group=None, default=None, method=None, **kw):
"""
Creates a new content
"""
if id is None:
new_id = str(self.generateNewId(id_group = id_group, default=default, method=method))
else:
new_id = str(id)
if portal_type is None: portal_type = self.allowedContentTypes()[0].id
self.portal_types.constructContent(type_name=portal_type,
container=self,
id=new_id,
**kw)
new_instance = self[new_id]
new_instance.flushActivity(invoke=1)
return new_instance
security.declareProtected(Permissions.DeletePortalContent, 'deleteContent')
def deleteContent(self, id):
# id is string or list
# XXX should raise error if not
if type(id) is type(''):
self._delObject(id)
elif type(id) is type([]) or type(id) is type(()):
for my_id in id:
self._delObject(my_id)
# Automatic ID Generation method
security.declareProtected(Permissions.View, 'generateNewId')
def generateNewId(self,id_group=None,default=None,method=None):
"""
Generate a new Id which has not been taken yet in this folder.
Eventually increment the id number until an available id
can be found
Permission is view because we may want to add content to a folder
without changing the folder content itself. XXXXXXXXXXX
XXXXXXXXXXXX
"""
my_id = None
if id_group is None:
id_group = self.getIdGroup()
if id_group is None or id_group=='None':
try:
my_id = int(self.getLastId())
except:
my_id = 1
while self.hasContent(str(my_id)):
my_id = my_id + 1
self.setLastId(str(my_id))
else:
my_id = self.portal_ids.generateNewId(id_group=id_group,default=default,method=method)
return str(my_id)
security.declareProtected(Permissions.View, 'hasContent')
def hasContent(self,id):
return id in self.objectIds()
# Get the content
security.declareProtected(Permissions.View, 'searchFolder')
def searchFolder(self, **kw):
"""
Search the content of a folder by calling
the portal_catalog.
"""
if not kw.has_key('parent_uid'): #WHY ????
kw['parent_uid'] = self.uid
kw2 = {}
# Remove useless matter before calling the
# catalog. In particular, consider empty
# strings as None values
for cname in kw.keys():
if kw[cname] != '' and kw[cname]!=None:
kw2[cname] = kw[cname]
# The method to call to search the folder
# content has to be called z_search_folder
method = self.portal_catalog.portal_catalog
return method(**kw2)
# Count objects in the folder
security.declarePrivate('_count')
def _count(self, **kw):
"""
Returns the number of items in the folder.
"""
# PERFORMANCE PROBLEM
# This should be improved in order to use
# SQL counting
return len(self.searchFolder(**kw))
class Folder( CopyContainer, CMFBTreeFolder, Base, FolderMixIn):
""" """
A Folder is a subclass of Base but not of XMLObject. A Folder is a subclass of Base but not of XMLObject.
Folders are not considered as documents and are therefore Folders are not considered as documents and are therefore
...@@ -163,99 +262,9 @@ be a problem).""" ...@@ -163,99 +262,9 @@ be a problem)."""
# Then insert the object # Then insert the object
CMFBTreeFolder._setObject(self, id, object, roles=roles, user=user, set_owner=set_owner) CMFBTreeFolder._setObject(self, id, object, roles=roles, user=user, set_owner=set_owner)
security.declareProtected(Permissions.AddPortalContent, 'newContent') security.declareProtected(Permissions.View, 'hasContent')
def newContent(self, id=None, portal_type=None, id_group=None, default=None, method=None, **kw): def hasContent(self,id):
""" return self.hasObject(id)
Creates a new content
"""
if id is None:
new_id = str(self.generateNewId(id_group = id_group, default=default, method=method))
else:
new_id = str(id)
if portal_type is None: portal_type = self.allowedContentTypes()[0].id
self.portal_types.constructContent(type_name=portal_type,
container=self,
id=new_id,
**kw)
new_instance = self[new_id]
new_instance.flushActivity(invoke=1)
return new_instance
security.declareProtected(Permissions.DeletePortalContent, 'deleteContent')
def deleteContent(self, id):
# id is string or list
# XXX should raise error if not
if type(id) is type(''):
self._delObject(id)
elif type(id) is type([]) or type(id) is type(()):
for my_id in id:
self._delObject(my_id)
# Automatic ID Generation method
security.declareProtected(Permissions.View, 'generateNewId')
def generateNewId(self,id_group=None,default=None,method=None):
"""
Generate a new Id which has not been taken yet in this folder.
Eventually increment the id number until an available id
can be found
Permission is view because we may want to add content to a folder
without changing the folder content itself. XXXXXXXXXXX
XXXXXXXXXXXX
"""
my_id = None
if id_group is None:
id_group = self.getIdGroup()
if id_group is None or id_group=='None':
try:
my_id = int(self.getLastId())
except:
my_id = 1
l = threading.Lock()
l.acquire()
try:
while self.hasObject(str(my_id)):
my_id = my_id + 1
self.setLastId(str(my_id))
finally:
l.release()
else:
my_id = self.portal_ids.generateNewId(id_group=id_group,default=default,method=method)
return str(my_id)
# Get the content
security.declareProtected(Permissions.View, 'searchFolder')
def searchFolder(self, **kw):
"""
Search the content of a folder by calling
the portal_catalog.
"""
if not kw.has_key('parent_uid'): #WHY ????
kw['parent_uid'] = self.uid
kw2 = {}
# Remove useless matter before calling the
# catalog. In particular, consider empty
# strings as None values
for cname in kw.keys():
if kw[cname] != '' and kw[cname]!=None:
kw2[cname] = kw[cname]
# The method to call to search the folder
# content has to be called z_search_folder
method = self.portal_catalog.portal_catalog
return method(**kw2)
# Count objects in the folder
security.declarePrivate('_count')
def _count(self, **kw):
"""
Returns the number of items in the folder.
"""
# PERFORMANCE PROBLEM
# This should be improved in order to use
# SQL counting
return len(self.searchFolder(**kw))
security.declareProtected( Permissions.ModifyPortalContent, 'exportAll' ) security.declareProtected( Permissions.ModifyPortalContent, 'exportAll' )
def exportAll(self,dir=None): def exportAll(self,dir=None):
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
# Hide internal implementation # Hide internal implementation
from Globals import InitializeClass from Globals import InitializeClass
from Products.ERP5Type.Document.Folder import Folder import Folder as ERP5Folder
# Default constructor for Folder # Default constructor for Folder
# Can be overriden by adding a method addFolder in class Folder # Can be overriden by adding a method addFolder in class Folder
def addFolder(folder, id, REQUEST=None, **kw): def addFolder(folder, id, REQUEST=None, **kw):
o = Folder(id) o = ERP5Folder.Folder(id)
folder._setObject(id, o) folder._setObject(id, o)
if kw is not None: o.__of__(folder)._edit(force_update=1, **kw) if kw is not None: o.__of__(folder)._edit(force_update=1, **kw)
# contentCreate already calls reindex 3 times ... # contentCreate already calls reindex 3 times ...
...@@ -14,4 +14,4 @@ def addFolder(folder, id, REQUEST=None, **kw): ...@@ -14,4 +14,4 @@ def addFolder(folder, id, REQUEST=None, **kw):
if REQUEST is not None: if REQUEST is not None:
REQUEST['RESPONSE'].redirect( 'manage_main' ) REQUEST['RESPONSE'].redirect( 'manage_main' )
InitializeClass(Folder) InitializeClass(ERP5Folder.Folder)
...@@ -397,11 +397,14 @@ def initializeProduct( context, this_module, global_hook, ...@@ -397,11 +397,14 @@ def initializeProduct( context, this_module, global_hook,
product_name = this_module.__name__.split('.')[-1] product_name = this_module.__name__.split('.')[-1]
# Define content classes from document_classes # Define content classes from document_classes
#LOG('Begin initializeProduct %s %s' % (document_module, document_classes),0,'')
extra_content_classes = [] extra_content_classes = []
if document_module is not None: if document_module is not None:
for module_name in document_classes: for module_name in document_classes:
#LOG('Inspecting %s %s' % (document_module, module_name),0,'')
candidate = getattr(document_module, module_name) candidate = getattr(document_module, module_name)
candidate = getattr(candidate, module_name) candidate = getattr(candidate, module_name)
#LOG('Found %s' % candidate,0,'')
if hasattr(candidate, 'isPortalContent'): if hasattr(candidate, 'isPortalContent'):
if candidate.isPortalContent == 1: if candidate.isPortalContent == 1:
extra_content_classes += [candidate] extra_content_classes += [candidate]
......
...@@ -40,7 +40,7 @@ document_classes = generateInitFiles(this_module, globals(), generate_document=0 ...@@ -40,7 +40,7 @@ document_classes = generateInitFiles(this_module, globals(), generate_document=0
# Import rest of the code and finish installation # Import rest of the code and finish installation
from Products.ERP5Type.Utils import initializeProduct from Products.ERP5Type.Utils import initializeProduct
import Document, Interface, PropertySheet, ZopePatch, StateChangeInfoPatch, \ import Interface, PropertySheet, ZopePatch, StateChangeInfoPatch, \
CMFCorePatch CMFCorePatch
#CMFCorePatch, FormulatorPatch #CMFCorePatch, FormulatorPatch
...@@ -52,9 +52,8 @@ def initialize( context ): ...@@ -52,9 +52,8 @@ def initialize( context ):
# Define documents, classes, constructors and tools # Define documents, classes, constructors and tools
object_classes = () object_classes = ()
content_constructors = () content_constructors = ()
content_classes = (Base.Base, Document.Folder, XMLObject.XMLObject,) content_classes = (Base.Base, XMLObject.XMLObject,)
portal_tools = (ClassTool.ClassTool, ) portal_tools = (ClassTool.ClassTool, )
document_classes = () # Specific to ERP5Type
# Do initialization step # Do initialization step
initializeProduct(context, this_module, globals(), initializeProduct(context, this_module, globals(),
document_module = Document, document_module = Document,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment