From 74dcfeb6a6bde10f0085bdcfa87af0b39c25bfcf Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Tue, 24 May 2016 11:16:24 +0200 Subject: [PATCH] Allow to customize the substitutions used on sequence issue4422 review24241002 --- CHANGELOG | 1 + trytond/ir/sequence.py | 26 ++++++++++++++++---------- trytond/tests/test_ir.py | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 2f11c26416..3b4ba1ae73 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/trytond/ir/sequence.py b/trytond/ir/sequence.py index 19a0637a90..7b2c3744fc 100644 --- a/trytond/ir/sequence.py +++ b/trytond/ir/sequence.py @@ -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): diff --git a/trytond/tests/test_ir.py b/trytond/tests/test_ir.py index 70793973ed..e743f07a5b 100644 --- a/trytond/tests/test_ir.py +++ b/trytond/tests/test_ir.py @@ -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)