Commit 18394f75 authored by Vincent Pelletier's avatar Vincent Pelletier

Import randint function.

Define _generateRandomId and _generateNextId which are id generation functions.
Remove extraneous Xs. 3 is enough.
Add a string property to Folder named id_generator.
Modify generateNewId to use the new property.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@9295 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 731aa046
...@@ -45,6 +45,7 @@ except ImportError: ...@@ -45,6 +45,7 @@ except ImportError:
from Products.BTreeFolder2.CMFBTreeFolder import CMFBTreeFolder from Products.BTreeFolder2.CMFBTreeFolder import CMFBTreeFolder
from AccessControl import getSecurityManager from AccessControl import getSecurityManager
from Products.ERP5Type import Permissions from Products.ERP5Type import Permissions
from random import randint
import os import os
...@@ -113,6 +114,30 @@ class FolderMixIn(ExtensionClass.Base, CopyContainer): ...@@ -113,6 +114,30 @@ class FolderMixIn(ExtensionClass.Base, CopyContainer):
raise TypeError, 'deleteContent only accepts string or list, '\ raise TypeError, 'deleteContent only accepts string or list, '\
'not %s' % type(id) 'not %s' % type(id)
def _generateRandomId(self):
"""
Generate a random Id.
10000 factor makes the odd to generate an already existing Id of 1 out
of 10000, not depending on the number of objects present in this folder.
len(self)+1 to make sure generation works on an empty Folder.
"""
return '%X' % (randint(1, 10000 * (len(self) + 1)), )
def _generateNextId(self):
"""
Get the last generated Id, increment it until no object with generated
Id exist, then save the Id.
"""
try:
my_id = int(self.getLastId())
except TypeError:
my_id = 1
while self.hasContent(str(my_id)):
my_id = my_id + 1
my_id = str(my_id)
self._setLastId(my_id) # Make sure no reindexing happens
return my_id
# Automatic ID Generation method # Automatic ID Generation method
security.declareProtected(Permissions.View, 'generateNewId') security.declareProtected(Permissions.View, 'generateNewId')
def generateNewId(self,id_group=None,default=None,method=None): def generateNewId(self,id_group=None,default=None,method=None):
...@@ -122,24 +147,23 @@ class FolderMixIn(ExtensionClass.Base, CopyContainer): ...@@ -122,24 +147,23 @@ class FolderMixIn(ExtensionClass.Base, CopyContainer):
can be found can be found
Permission is view because we may want to add content to a folder Permission is view because we may want to add content to a folder
without changing the folder content itself. XXXXXXXXXXX without changing the folder content itself.
XXXXXXXXXXXX XXX
""" """
my_id = None my_id = None
if id_group is None: if id_group is None:
id_group = self.getIdGroup() id_group = self.getIdGroup()
if id_group is None or id_group=='None': if id_group in (None, 'None'):
try: idGenerator = getattr(self, self.getIdGenerator(), None)
my_id = int(self.getLastId()) if idGenerator is None:
except TypeError: idGenerator = self._generateNextId
my_id = 1 my_id = idGenerator()
while self.hasContent(str(my_id)): while self.hasContent(my_id):
my_id = my_id + 1 my_id = _generateNextId()
self._setLastId(str(my_id)) # Make sure no reindexing happens
else: else:
my_id = self.portal_ids.generateNewId(id_group=id_group,default=default,method=method) my_id = str(self.portal_ids.generateNewId(id_group=id_group,default=default,method=method))
return str(my_id) return my_id
security.declareProtected(Permissions.View, 'hasContent') security.declareProtected(Permissions.View, 'hasContent')
def hasContent(self,id): def hasContent(self,id):
......
...@@ -44,5 +44,10 @@ class Folder: ...@@ -44,5 +44,10 @@ class Folder:
'type' : 'string', 'type' : 'string',
'default' : '1', 'default' : '1',
'mode' : 'w'}, 'mode' : 'w'},
{ 'id' : 'id_generator',
'description' : 'The name of the function to use to generate the Id of a new contained object. Default behaviour (when this value is not set or the function cannot be found) is to increment last_id and update it.',
'type' : 'string',
'default' : '',
'mode' : 'w'},
) )
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