Sequence bugfixes

bzr revid: pinky-74587d6b1576845a10cccd975d060caee314e9d6
This commit is contained in:
pinky 2006-12-18 12:25:45 +00:00
parent 6c1f76d424
commit c7bd4bef76
1 changed files with 14 additions and 21 deletions

View File

@ -26,7 +26,6 @@
# #
############################################################################## ##############################################################################
import threading
import time import time
from osv import fields,osv from osv import fields,osv
@ -61,33 +60,27 @@ class ir_sequence(osv.osv):
'padding' : lambda *a : 0, 'padding' : lambda *a : 0,
} }
sequence_semaphore = threading.Semaphore()
def _process(self, s): def _process(self, s):
return (s or '') % {'year':time.strftime('%Y'), 'month': time.strftime('%m'), 'day':time.strftime('%d')} return (s or '') % {'year':time.strftime('%Y'), 'month': time.strftime('%m'), 'day':time.strftime('%d')}
def get_id(self, cr, uid, sequence_id, test='id=%d'): def get_id(self, cr, uid, sequence_id, test='id=%d'):
self.sequence_semaphore.acquire() try:
cr.execute('select id,number_next,number_increment,prefix,suffix,padding from ir_sequence where '+test+' and active=True', (sequence_id,)) cr.execute('lock table ir_sequence')
res = cr.dictfetchone() cr.execute('select id,number_next,number_increment,prefix,suffix,padding from ir_sequence where '+test+' and active=True', (sequence_id,))
if res: res = cr.dictfetchone()
cr.execute('update ir_sequence set number_next=number_next+number_increment where id=%d and active=True', (res['id'],)) if res:
self.sequence_semaphore.release() cr.execute('update ir_sequence set number_next=number_next+number_increment where id=%d and active=True', (res['id'],))
if res['number_next']: if res['number_next']:
return self._process(res['prefix']) + '%%0%sd' % res['padding'] % res['number_next'] + self._process(res['suffix']) return self._process(res['prefix']) + '%%0%sd' % res['padding'] % res['number_next'] + self._process(res['suffix'])
else: else:
return self._process(res['prefix']) + self._process(res['suffix']) return self._process(res['prefix']) + self._process(res['suffix'])
else: cr.commit()
self.sequence_semaphore.release() except:
cr.rollback()
return False
return False return False
def get(self, cr, uid, code): def get(self, cr, uid, code):
return self.get_id(cr, uid, code, test='code=%s') return self.get_id(cr, uid, code, test='code=%s')
def set(self, cr, uid, code, next_number):
self.sequence_semaphore.acquire()
cr.execute('update ir_sequence set number_next=%d where code=%s and active=True', (next_number, code,))
self.sequence_semaphore.release()
return True
ir_sequence() ir_sequence()