From bc218140deb9720926f7dbde54696fe329d64cc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 1 Jan 2019 15:47:23 +0100 Subject: [PATCH] Add ir.calendar for month and day This allows to replace time_locale and use the translation infrastructure instead. So it can be customized using the translation override or derivative languages. This provides also a standard base to modules that needs calendar month and day. issue7815 review56451002 --- CHANGELOG | 2 + purchase.py | 49 ++++++++++++------- tests/test_stock_supply_day.py | 4 +- view/product_supplier_weekday_form.xml | 4 +- view/product_supplier_weekday_tree.xml | 2 +- ...product_supplier_weekday_tree_editable.xml | 2 +- 6 files changed, 39 insertions(+), 24 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index fdedf72051..ded3672c69 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Use ir.calendar.day + Version 5.0.0 - 2018-10-01 * Bug fixes (see mercurial logs for details) * Remove support for Python 2.7 diff --git a/purchase.py b/purchase.py index 2b9ef3d684..29d8e3146b 100644 --- a/purchase.py +++ b/purchase.py @@ -2,7 +2,8 @@ # this repository contains the full copyright notices and license terms. import datetime from trytond.model import ModelView, ModelSQL, fields -from trytond.pool import PoolMeta +from trytond.pool import PoolMeta, Pool +from trytond.transaction import Transaction __all__ = ['ProductSupplier', 'ProductSupplierDay'] @@ -16,8 +17,8 @@ class ProductSupplier(metaclass=PoolMeta): date = super(ProductSupplier, self).compute_supply_date(date=date) earlier_date = None if date != datetime.date.max: - for day in self.weekdays: - weekday = int(day.weekday) + for weekday in self.weekdays: + weekday = weekday.day.index diff = weekday - date.weekday() if diff < 0: diff += 7 @@ -30,8 +31,8 @@ class ProductSupplier(metaclass=PoolMeta): def compute_purchase_date(self, date): later_date = None - for day in self.weekdays: - weekday = int(day.weekday) + for weekday in self.weekdays: + weekday = weekday.day.index diff = (date.weekday() - weekday) % 7 new_date = date - datetime.timedelta(diff) if later_date and later_date >= new_date: @@ -47,21 +48,31 @@ class ProductSupplierDay(ModelSQL, ModelView): __name__ = 'purchase.product_supplier.day' product_supplier = fields.Many2One('purchase.product_supplier', 'Supplier', required=True, ondelete='CASCADE') - weekday = fields.Selection([ - ('0', 'Monday'), - ('1', 'Tuesday'), - ('2', 'Wednesday'), - ('3', 'Thursday'), - ('4', 'Friday'), - ('5', 'Saturday'), - ('6', 'Sunday'), - ], 'Week Day', required=True, sort=False) + day = fields.Many2One('ir.calendar.day', "Day", required=True) + + @classmethod + def __register__(cls, module_name): + pool = Pool() + Day = pool.get('ir.calendar.day') + day = Day.__table__() + transaction = Transaction() + table = cls.__table__() + + super().__register__(module_name) + table_h = cls.__table_handler__(module_name) + + # Migration from 5.0: replace weekday by day + if table_h.column_exist('weekday'): + cursor = transaction.connection.cursor() + update = transaction.connection.cursor() + cursor.execute(*day.select([day.id, day.index])) + for day_id, index in cursor: + update.execute(*table.update( + [table.day], [day_id], + where=table.weekday == str(index))) + table_h.drop_column('weekday') @classmethod def __setup__(cls): super(ProductSupplierDay, cls).__setup__() - cls._order.insert(0, ('weekday', 'ASC')) - - @staticmethod - def default_weekday(): - return '0' + cls._order.insert(0, ('day', 'ASC')) diff --git a/tests/test_stock_supply_day.py b/tests/test_stock_supply_day.py index 1d42f09e31..320c4c69cd 100644 --- a/tests/test_stock_supply_day.py +++ b/tests/test_stock_supply_day.py @@ -104,6 +104,7 @@ class StockSupplyDayTestCase(ModuleTestCase): Account = pool.get('account.account') ProductSupplier = pool.get('purchase.product_supplier') ProductSupplierDay = pool.get('purchase.product_supplier.day') + Day = pool.get('ir.calendar.day') uom_category, = UomCategory.create([{'name': 'Test'}]) uom, = Uom.create([{ @@ -144,9 +145,10 @@ class StockSupplyDayTestCase(ModuleTestCase): 'lead_time': lead_time, }]) if weekday is not None: + day, = Day.search([('index', '=', weekday)]) ProductSupplierDay.create([{ 'product_supplier': product_supplier.id, - 'weekday': str(weekday), + 'day': day.id, }]) return product_supplier diff --git a/view/product_supplier_weekday_form.xml b/view/product_supplier_weekday_form.xml index 4f845aa2bc..5918074ee3 100644 --- a/view/product_supplier_weekday_form.xml +++ b/view/product_supplier_weekday_form.xml @@ -5,6 +5,6 @@ this repository contains the full copyright notices and license terms. -->