[Zope] Creating Document with random num as ID?

Pavlos Christoforou pavlos@gaaros.msrc.sunysb.edu
Sat, 17 Apr 1999 17:56:52 -0400 (EDT)


On Sat, 17 Apr 1999, Craig Allen wrote:

> I guess I'll have to answer it myself.<g>
> 
> I realized I could make javascript generate a random number as part of
> the request.
> This doesn't solve the need for uniqueness but with a large enough range
> and anticipating a low volume of requests I should be able to devise a
> workaround.
> 
> If anyone can tell me how the unique randon keys could/should be
> generated within Zope, that would be preferable, and I'd learn
> something.

If the randomness of the keys is not a requirement (ie you just want to
generate unique ids, then add a property of type integer in the containing
folder and increment it everytime you need a new id. Assuming for
instance, thet the integer property is called unique_id, then the
following dtml will acvieve what you want:

<!--# call "manage_changeProperties(unique_id=unique_id+1)"-->
<!--# call 
"manage_addFolder("Folder"+_.str(unique_id),
"Folder"+_.str(unique_id),0,0,REQUEST)"-->

The above will add a new folder with id and title(Folder1, Folder2, etc)

If you do want to use a random id then you can use an
external method:

import whrandom

def random_id(self):
    '''random_id(self)

    returns a random id of type Folder<random number>"""

    id='Folder'+str(whrandom.randint(1,1000))
    while id in self.objectIds():
	id='Folder'+str(whrandom.randint(1,1000)
    return id	


Pavlos