changeset c90d41cf3ccb in modules/stock_supply_day:default
details: 
https://hg.tryton.org/modules/stock_supply_day?cmd=changeset;node=c90d41cf3ccb
description:
        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
diffstat:

 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 +-
 view/product_supplier_weekday_tree_editable.xml |   2 +-
 6 files changed, 39 insertions(+), 24 deletions(-)

diffs (145 lines):

diff -r 31593be41d70 -r c90d41cf3ccb CHANGELOG
--- a/CHANGELOG Mon Dec 31 15:36:49 2018 +0100
+++ b/CHANGELOG Tue Jan 01 15:47:23 2019 +0100
@@ -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 -r 31593be41d70 -r c90d41cf3ccb purchase.py
--- a/purchase.py       Mon Dec 31 15:36:49 2018 +0100
+++ b/purchase.py       Tue Jan 01 15:47:23 2019 +0100
@@ -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 @@
         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 @@
 
     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 @@
     __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 -r 31593be41d70 -r c90d41cf3ccb tests/test_stock_supply_day.py
--- a/tests/test_stock_supply_day.py    Mon Dec 31 15:36:49 2018 +0100
+++ b/tests/test_stock_supply_day.py    Tue Jan 01 15:47:23 2019 +0100
@@ -104,6 +104,7 @@
         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 @@
                         '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 -r 31593be41d70 -r c90d41cf3ccb view/product_supplier_weekday_form.xml
--- a/view/product_supplier_weekday_form.xml    Mon Dec 31 15:36:49 2018 +0100
+++ b/view/product_supplier_weekday_form.xml    Tue Jan 01 15:47:23 2019 +0100
@@ -5,6 +5,6 @@
     <label name="product_supplier"/>
     <field name="product_supplier"/>
     <newline/>
-    <label name="weekday"/>
-    <field name="weekday"/>
+    <label name="day"/>
+    <field name="day" widget="selection"/>
 </form>
diff -r 31593be41d70 -r c90d41cf3ccb view/product_supplier_weekday_tree.xml
--- a/view/product_supplier_weekday_tree.xml    Mon Dec 31 15:36:49 2018 +0100
+++ b/view/product_supplier_weekday_tree.xml    Tue Jan 01 15:47:23 2019 +0100
@@ -3,5 +3,5 @@
 this repository contains the full copyright notices and license terms. -->
 <tree>
     <field name="product_supplier"/>
-    <field name="weekday"/>
+    <field name="day" widget="selection"/>
 </tree>
diff -r 31593be41d70 -r c90d41cf3ccb 
view/product_supplier_weekday_tree_editable.xml
--- a/view/product_supplier_weekday_tree_editable.xml   Mon Dec 31 15:36:49 
2018 +0100
+++ b/view/product_supplier_weekday_tree_editable.xml   Tue Jan 01 15:47:23 
2019 +0100
@@ -3,5 +3,5 @@
 this repository contains the full copyright notices and license terms. -->
 <tree editable="bottom">
     <field name="product_supplier"/>
-    <field name="weekday" expand="1"/>
+    <field name="day" expand="1" widget="selection"/>
 </tree>

Reply via email to