From c0d5db44c1c78dea70e686f40e6442eee01c8885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 19 May 2008 10:43:59 +0200 Subject: [PATCH] Use Template for sequence suffix and prefix and add check to validate it for issue13 --- trytond/ir/sequence.py | 29 ++++++++++++++++++++++++----- trytond/ir/sequence.xml | 6 +++--- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/trytond/ir/sequence.py b/trytond/ir/sequence.py index fbc59cab30..b70dd89ab0 100644 --- a/trytond/ir/sequence.py +++ b/trytond/ir/sequence.py @@ -1,5 +1,6 @@ import time from trytond.osv import fields, OSV, ExceptORM +from string import Template class SequenceType(OSV): @@ -26,6 +27,13 @@ class Sequence(OSV): number_increment = fields.Integer('Increment Number', required=True) padding = fields.Integer('Number padding', required=True) + def __init__(self): + super(Sequence, self).__init__() + self._constraints += [ + ('check_prefix_suffix', 'Invalid prefix/suffix', + ['prefix', 'suffix']), + ] + def default_active(self, cursor, user, context=None): return 1 @@ -42,12 +50,23 @@ class Sequence(OSV): cursor.execute('select code, name from ir_sequence_type') return cursor.fetchall() + def check_prefix_suffix(self, cursor, user, ids): + "Check prefix and suffix" + + for sequence in self.browse(cursor, user, ids): + try: + self._process(sequence.prefix) + self._process(sequence.suffix) + except: + return False + return True + def _process(self, string): - return (string or '') % { - 'year':time.strftime('%Y'), - 'month': time.strftime('%m'), - 'day':time.strftime('%d'), - } + return Template(string or '').substitute( + year=time.strftime('%Y'), + month=time.strftime('%m'), + day=time.strftime('%d'), + ) def get_id(self, cursor, user, sequence_id, test='id=%s'): cursor.execute('lock table ir_sequence') diff --git a/trytond/ir/sequence.xml b/trytond/ir/sequence.xml index 7736edd89b..eafb8d5676 100644 --- a/trytond/ir/sequence.xml +++ b/trytond/ir/sequence.xml @@ -25,11 +25,11 @@