diff --git a/base_usability/__init__.py b/base_usability/__init__.py index a0c9233..1baafee 100644 --- a/base_usability/__init__.py +++ b/base_usability/__init__.py @@ -3,3 +3,4 @@ from . import partner from . import mail from . import misc +from . import ir_sequence diff --git a/base_usability/ir_sequence.py b/base_usability/ir_sequence.py new file mode 100644 index 0000000..1d7f014 --- /dev/null +++ b/base_usability/ir_sequence.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Base Usability module for Odoo +# Copyright (C) 2016 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp import models, fields +from datetime import datetime +import pytz + + +class IrSequence(models.Model): + _inherit = 'ir.sequence' + + def _interpolation_dict_context(self, context=None): + if context is None: + context = {} + t = False + if context.get('force_sequence_date'): + date_str = context['force_sequence_date'] + if isinstance(date_str, (str, unicode)) and len(date_str) == 10: + t = fields.Date.from_string(date_str) + if not t: + t = datetime.now(pytz.timezone(context.get('tz') or 'UTC')) + sequences = { + 'year': '%Y', 'month': '%m', 'day': '%d', 'y': '%y', 'doy': '%j', + 'woy': '%W', + 'weekday': '%w', 'h24': '%H', 'h12': '%I', 'min': '%M', 'sec': '%S' + } + return { + key: t.strftime(sequence) + for key, sequence in sequences.iteritems()}