"Sequence and tree editable migration to 6.0 [SKIP CI]."

This commit is contained in:
Sergio Morillo 2021-06-08 16:33:01 +02:00
parent 05e1b94d5f
commit e99da27744
5 changed files with 51 additions and 42 deletions

69
ir.py
View file

@ -5,12 +5,14 @@ from trytond.model import ModelSQL, ModelView, MatchMixin, fields
from trytond.pool import PoolMeta, Pool from trytond.pool import PoolMeta, Pool
from trytond.pyson import Eval, And, Bool from trytond.pyson import Eval, And, Bool
from trytond.transaction import Transaction from trytond.transaction import Transaction
from trytond.i18n import gettext
from trytond.ir.sequence import MissingError
from trytond import backend from trytond import backend
from sql import Literal, For from sql import Literal, For
__all__ = ['Sequence', 'SequencePeriod', 'SequenceStrict'] __all__ = ['Sequence', 'SequencePeriod', 'SequenceStrict']
sql_sequence = backend.get('Database').has_sequence() sql_sequence = backend.Database.has_sequence()
class Sequence(metaclass=PoolMeta): class Sequence(metaclass=PoolMeta):
@ -21,16 +23,6 @@ class Sequence(metaclass=PoolMeta):
depends=['type'], depends=['type'],
order=[('start_date', 'ASC')]) order=[('start_date', 'ASC')])
@classmethod
def __setup__(cls):
super(Sequence, cls).__setup__()
cls._error_messages.update({
'missing_date':
'Must define a Date value in context for Sequence "%s".',
'missing_period':
'Cannot find a valid period for date "%s" in Sequence "%s".'
})
@classmethod @classmethod
def write(cls, sequences, values, *args): def write(cls, sequences, values, *args):
super(Sequence, cls).write(sequences, values, *args) super(Sequence, cls).write(sequences, values, *args)
@ -39,30 +31,29 @@ class Sequence(metaclass=PoolMeta):
for period in sequence.periods: for period in sequence.periods:
period.update_sql_sequence() period.update_sql_sequence()
@classmethod def get_id(self, _lock=False):
def get_id(cls, domain, _lock=False): cls = self.__class__
if isinstance(domain, cls):
domain = domain.id
if isinstance(domain, int):
domain = [('id', '=', domain)]
with Transaction().set_context(user=False, _check_access=False): with Transaction().set_context(user=False, _check_access=False):
with Transaction().set_user(0): with Transaction().set_user(0):
try: try:
sequence, = cls.search(domain, limit=1) sequence = cls(self.id)
except TypeError: except TypeError:
cls.raise_user_error('missing') raise MissingError(gettext('ir.msg_sequence_missing'))
date = Transaction().context.get('date') date = Transaction().context.get('date')
if sequence.periods: if sequence.periods:
if not date: if not date:
cls.raise_user_error('missing_date', sequence.rec_name) raise MissingError(gettext(
'ir_sequence_period.sequence.msg_missing_date',
sequence=sequence.rec_name))
pattern = sequence._get_period_pattern() pattern = sequence._get_period_pattern()
for period in sequence.periods: for period in sequence.periods:
if period.match(pattern): if period.match(pattern):
return period.get_id(_lock=_lock) return period.get(_lock=_lock)
cls.raise_user_error('missing_period', (date, raise MissingError(gettext(
sequence.rec_name)) 'ir_sequence_period.sequence.msg_missing_date',
return super(Sequence, cls).get_id(domain, _lock=_lock) date=date,
sequence=sequence.rec_name))
return super(Sequence, cls).get(_lock=_lock)
return '' return ''
def _get_period_pattern(self): def _get_period_pattern(self):
@ -181,8 +172,9 @@ class SequencePeriod(ModelSQL, ModelView, MatchMixin):
transaction.database.sequence_delete( transaction.database.sequence_delete(
transaction.connection, self._sql_sequence_name) transaction.connection, self._sql_sequence_name)
def get_id(self, _lock=False): def get(self, _lock=False):
Sequence = Pool().get('ir.sequence') Sequence = Pool().get('ir.sequence')
cls = self.__class__
if _lock: if _lock:
transaction = Transaction() transaction = Transaction()
@ -199,27 +191,30 @@ class SequencePeriod(ModelSQL, ModelView, MatchMixin):
cursor.execute(*query) cursor.execute(*query)
date = Transaction().context.get('date') date = Transaction().context.get('date')
return '%s%s%s' % ( return '%s%s%s' % (
Sequence._process(self.prefix or self.sequence.prefix, date=date), Sequence._process(self.prefix or self.sequence.prefix,
self._get_sequence(), date=date),
Sequence._process(self.suffix or self.sequence.suffix, date=date), cls._get_sequence(self),
Sequence._process(self.suffix or self.sequence.suffix,
date=date),
) )
def _get_sequence(self): @classmethod
if self.sequence.type == 'incremental': def _get_sequence(cls, period):
if sql_sequence and not self._strict: if period.sequence.type == 'incremental':
if sql_sequence and not cls._strict:
cursor = Transaction().connection.cursor() cursor = Transaction().connection.cursor()
cursor.execute('SELECT nextval(\'"%s"\')' cursor.execute('SELECT nextval(\'"%s"\')'
% self._sql_sequence_name) % period._sql_sequence_name)
number_next, = cursor.fetchone() number_next, = cursor.fetchone()
else: else:
# Pre-fetch number_next # Pre-fetch number_next
number_next = self.number_next_internal number_next = period.number_next_internal
self.write([self], { cls.write([period], {
'number_next_internal': (number_next 'number_next_internal': (number_next
+ self.sequence.number_increment), + period.sequence.number_increment),
}) })
return '%%0%sd' % self.sequence.padding % number_next return '%%0%sd' % period.sequence.padding % number_next
else: else:
raise NotImplementedError() raise NotImplementedError()

View file

@ -2,11 +2,11 @@
msgid "" msgid ""
msgstr "Content-Type: text/plain; charset=utf-8\n" msgstr "Content-Type: text/plain; charset=utf-8\n"
msgctxt "error:ir.sequence:" msgctxt "model:ir.message,text:msg_missing_date"
msgid "Must define a Date value in context for Sequence \"%s\"." msgid "Must define a Date value in context for Sequence \"%s\"."
msgstr "Debe definir un valor de Fecha en contexto para la Secuencia \"%s\"." msgstr "Debe definir un valor de Fecha en contexto para la Secuencia \"%s\"."
msgctxt "error:ir.sequence:" msgctxt "model:ir.message,text:msg_missing_period"
msgid "Cannot find a valid period for date \"%s\" in Sequence \"%s\"." msgid "Cannot find a valid period for date \"%s\" in Sequence \"%s\"."
msgstr "No se ha podido encontrar un período válido para la fecha \"%s\" en la Secuencia \"%s\"." msgstr "No se ha podido encontrar un período válido para la fecha \"%s\" en la Secuencia \"%s\"."

13
message.xml Normal file
View file

@ -0,0 +1,13 @@
<?xml version="1.0"?>
<!-- The COPYRIGHT file at the top level of this repository contains the full
copyright notices and license terms. -->
<tryton>
<data grouped="1">
<record model="ir.message" id="msg_missing_date">
<field name="text">Must define a Date value in context for Sequence "%(sequence)s".</field>
</record>
<record model="ir.message" id="msg_missing_period">
<field name="text">Cannot find a valid period for date "%(date)s" in Sequence "%(sequence)s".</field>
</record>
</data>
</tryton>

View file

@ -22,12 +22,12 @@ class IrSequencePeriodTestCase(ModuleTestCase):
with Transaction().set_context(user=False, _check_access=False): with Transaction().set_context(user=False, _check_access=False):
with Transaction().set_user(0): with Transaction().set_user(0):
SequenceType.create([{ type, = SequenceType.create([{
'name': 'Tests', 'name': 'Tests',
'code': 'test'}]) }])
sequence, = Sequence.create([{ sequence, = Sequence.create([{
'name': 'Test incremental', 'name': 'Test incremental',
'code': 'test', 'sequence_type': type.id,
'prefix': '${year}/', 'prefix': '${year}/',
'suffix': '', 'suffix': '',
'type': 'incremental', 'type': 'incremental',

View file

@ -12,3 +12,4 @@ extras_depend:
xml: xml:
ir.xml ir.xml
message.xml