What is The Id Tool
The Id Tool allows to generate many kinds of ids. It can be used in order to generate several sequences of strings or numbers.
How to Use a new sequence
First you must define a name for your new sequence, it can be any string. Let's say you want a sequences for all your orders, you may decide to use the name "order_reference" for your sequence.
By default the id tool can automatically generate simple sequence of integers by increasing them one by one. You can set a default value, it will be used in order to initialize your sequence.
1 new_id = context.portal_ids.generateNewId(id_group='order_reference',default=4)
You may call this method each time you need it, each time it will returns a number bigger and bigger :
first time : 4 second time : 5 third time : 6 and so on...
The method parameter
It is possible to specify a method in order to generate the next element in the sequence. This method receive will get as argument the previous element in the sequence and must returns the next one.
Here an example :
1 def generateNewDeliveryReference(previous_value):
2 (reference,number)=previous_value.split('-')
3 new_number = int(number)+2
4 return '%s-%s' % (reference,new_number)
5
6 for i in range(0,5):
7 print context.portal_ids.generateNewId(default='DELIVERY-0',id_group='delivery_reference',
8 method = generateNewDeliveryReference)
9 return printed
Here is the output :
DELIVERY-2 DELIVERY-4 DELIVERY-6 DELIVERY-8 DELIVERY-10
Get the last generated Id
There is a method called getLastGeneratedId which allows to retrieve the last generated id for a sequence
1 last_id = context.portal_ids.getLastGeneratedId(id_group='toto',default=3)
Reset a sequence
It is possible to set a new value for the last generated id with the method called setLastGeneratedId
1 context.portal_ids.setLastGeneratedId(6,id_group='toto')