Commit 37ffe3a9 authored by Jérome Perrin's avatar Jérome Perrin

use random.getrandbits for IdTool.generateNewLongId, the previous...

use random.getrandbits for IdTool.generateNewLongId, the previous implementation was not compatible with zope >= 2.8


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@24452 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent bda81766
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
# #
############################################################################## ##############################################################################
import random
from Acquisition import aq_base from Acquisition import aq_base
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
from Globals import InitializeClass, DTMLFile, PersistentMapping from Globals import InitializeClass, DTMLFile, PersistentMapping
...@@ -129,13 +130,10 @@ class IdTool(BaseTool): ...@@ -129,13 +130,10 @@ class IdTool(BaseTool):
'generateNewLongId') 'generateNewLongId')
def generateNewLongId(self, **kw): def generateNewLongId(self, **kw):
""" """
Returns the ZODB transation id to be used as an identifier. Returns a random 64bits long.
It's a 64bits number, so it can look ugly and/or huge to users. It's a 64bits number, so it can look ugly and/or huge to users.
""" """
tid = get_transaction()._id; return random.getrandbits(64)
# It's a 64 bits number, but sometimes it returns as a negative int... so
# make it positive again and add 2**63.
return (tid < 0) and (2**63 - tid) or tid;
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'getDictLengthIdsItems') 'getDictLengthIdsItems')
......
############################################################################## ##############################################################################
# # -*- coding: latin1 -*-
# Copyright (c) 2008 Nexedi SARL and Contributors. All Rights Reserved. # Copyright (c) 2008 Nexedi SARL and Contributors. All Rights Reserved.
# Aurlien Calonne <aurel@nexedi.com> # Aurlien Calonne <aurel@nexedi.com>
# #
...@@ -119,6 +119,11 @@ class TestIdTool(ERP5TypeTestCase): ...@@ -119,6 +119,11 @@ class TestIdTool(ERP5TypeTestCase):
default='A', default='A',
method=generateTestNumber)) method=generateTestNumber))
def test_generateNewLongId(self):
idtool = self.portal.portal_ids
# test with value stored into zodb
new_id = idtool.generateNewLongId()
self.assertTrue(isinstance(new_id, long))
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
......
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