Allow to customize the substitutions used on sequence

issue4422
review24241002
This commit is contained in:
Sergi Almacellas Abellana
2016-05-24 11:16:24 +02:00
parent 1f9a834c9b
commit 74dcfeb6a6
3 changed files with 49 additions and 11 deletions
+1
View File
@@ -1,3 +1,4 @@
* Allow to customize the substitutions used on sequence
* Allow PYSON in tree_invisible attribute
Version 4.0.0 - 2016-05-02
+16 -10
View File
@@ -296,20 +296,26 @@ class Sequence(ModelSQL, ModelView):
cursor.execute('DROP SEQUENCE "%s"'
% self._sql_sequence_name)
@staticmethod
def _process(string, date=None):
@classmethod
def _process(cls, string, date=None):
return Template(string or '').substitute(
**cls._get_substitutions(date))
@classmethod
def _get_substitutions(cls, date):
'''
Returns a dictionary with the keys and values of the substitutions
available to format the sequence
'''
pool = Pool()
Date = pool.get('ir.date')
if not date:
date = Date.today()
year = datetime_strftime(date, '%Y')
month = datetime_strftime(date, '%m')
day = datetime_strftime(date, '%d')
return Template(string or '').substitute(
year=year,
month=month,
day=day,
)
return {
'year': datetime_strftime(date, '%Y'),
'month': datetime_strftime(date, '%m'),
'day': datetime_strftime(date, '%d'),
}
@staticmethod
def _timestamp(sequence):
+32 -1
View File
@@ -1,14 +1,45 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from dateutil.relativedelta import relativedelta
import unittest
from .test_tryton import ModuleTestCase
from trytond.pool import Pool
from trytond.transaction import Transaction
from .test_tryton import ModuleTestCase, with_transaction
class IrTestCase(ModuleTestCase):
'Test ir module'
module = 'ir'
@with_transaction()
def test_sequence_substitutions(self):
'Test Sequence Substitutions'
pool = Pool()
Sequence = pool.get('ir.sequence')
SequenceType = pool.get('ir.sequence.type')
Date = pool.get('ir.date')
try:
Group = pool.get('res.group')
groups = Group.search([])
except KeyError:
groups = []
sequence_type = SequenceType(name='Test', code='test', groups=groups)
sequence_type.save()
sequence = Sequence(name='Test Sequence', code='test')
sequence.save()
self.assertEqual(Sequence.get_id(sequence.id), '1')
today = Date.today()
sequence.prefix = '${year}'
sequence.save()
self.assertEqual(Sequence.get_id(sequence.id),
'%s2' % str(today.year))
next_year = today + relativedelta(years=1)
with Transaction().set_context(date=next_year):
self.assertEqual(Sequence.get_id(sequence.id),
'%s3' % str(next_year.year))
def suite():
return unittest.TestLoader().loadTestsFromTestCase(IrTestCase)