changeset 7a23a9b7c56d in modules/customs:default
details: https://hg.tryton.org/modules/customs?cmd=changeset&node=7a23a9b7c56d
description:
        Use ir.calendar.month as selection for tariff_code's month fields

        issue11892
        review418721003
diffstat:

 CHANGELOG                 |   2 +
 customs.py                |  76 ++++++++++++++++++++++++++++++----------------
 tests/test_module.py      |  12 ++++--
 view/tariff_code_form.xml |   4 +-
 4 files changed, 61 insertions(+), 33 deletions(-)

diffs (185 lines):

diff -r 2a8527d41c92 -r 7a23a9b7c56d CHANGELOG
--- a/CHANGELOG Fri Nov 18 00:18:36 2022 +0100
+++ b/CHANGELOG Mon Nov 21 10:12:40 2022 +0100
@@ -1,3 +1,5 @@
+* Use ir.calendar.month for tariff code months
+
 Version 6.6.0 - 2022-10-31
 --------------------------
 * Bug fixes (see mercurial logs for details)
diff -r 2a8527d41c92 -r 7a23a9b7c56d customs.py
--- a/customs.py        Fri Nov 18 00:18:36 2022 +0100
+++ b/customs.py        Mon Nov 21 10:12:40 2022 +0100
@@ -5,28 +5,13 @@
 
 from sql import Null
 
+from trytond import backend
 from trytond.model import (
     DeactivableMixin, MatchMixin, ModelSQL, ModelView, fields)
 from trytond.modules.product import price_digits
 from trytond.pool import Pool
 from trytond.pyson import Bool, Eval, If
-
-# Use 2 chars numbering to allow string comparison
-MONTHS = [
-    (None, ''),
-    ('01', 'January'),
-    ('02', 'February'),
-    ('03', 'March'),
-    ('04', 'April'),
-    ('05', 'May'),
-    ('06', 'June'),
-    ('07', 'July'),
-    ('08', 'August'),
-    ('09', 'September'),
-    ('10', 'October'),
-    ('11', 'November'),
-    ('12', 'December'),
-    ]
+from trytond.transaction import Transaction
 
 
 class TariffCode(DeactivableMixin, ModelSQL, ModelView, MatchMixin):
@@ -38,30 +23,26 @@
     description = fields.Char('Description', translate=True)
     country = fields.Many2One('country.country', 'Country')
     # TODO country group
-    start_month = fields.Selection(MONTHS, 'Start Month', sort=False,
+    start_month = fields.Many2One('ir.calendar.month', "Start Month",
         states={
             'required': Eval('end_month') | Eval('start_day'),
             })
     start_day = fields.Integer('Start Day',
         domain=['OR',
-            ('start_day', '<=', If(Eval('start_month').in_(
-                        ['01', '03', '05', '07', '08', '10', '12']), 31,
-                    If(Eval('start_month') == '02', 29, 30))),
             ('start_day', '=', None),
+            [('start_day', '>=', 1), ('start_day', '<=', 31)],
             ],
         states={
             'required': Bool(Eval('start_month')),
             })
-    end_month = fields.Selection(MONTHS, 'End Month', sort=False,
+    end_month = fields.Many2One('ir.calendar.month', "End Month",
         states={
             'required': Eval('start_month') | Eval('end_day'),
             })
     end_day = fields.Integer('End Day',
         domain=['OR',
-            ('end_day', '<=', If(Eval('end_month').in_(
-                        ['01', '03', '05', '07', '08', '10', '12']), 31,
-                    If(Eval('end_month') == '02', 29, 30))),
             ('end_day', '=', None),
+            [('end_day', '>=', 1), ('end_day', '<=', 31)],
             ],
         states={
             'required': Bool(Eval('end_month')),
@@ -74,13 +55,54 @@
         super(TariffCode, cls).__setup__()
         cls._order.insert(0, ('code', 'ASC'))
 
+    @classmethod
+    def __register__(cls, module_name):
+        transaction = Transaction()
+        cursor = transaction.connection.cursor()
+        pool = Pool()
+        Month = pool.get('ir.calendar.month')
+        sql_table = cls.__table__()
+        month = Month.__table__()
+        table_h = cls.__table_handler__(module_name)
+
+        # Migration from 6.6: use ir.calendar
+        migrate_calendar = False
+        if (backend.TableHandler.table_exist(cls._table)
+                and table_h.column_exist('start_month')
+                and table_h.column_exist('end_month')):
+            migrate_calendar = (
+                table_h.column_is_type('start_month', 'VARCHAR')
+                or table_h.column_is_type('end_month', 'VARCHAR'))
+            if migrate_calendar:
+                table_h.column_rename('start_month', '_temp_start_month')
+                table_h.column_rename('end_month', '_temp_end_month')
+
+        super().__register__(module_name)
+
+        table_h = cls.__table_handler__(module_name)
+
+        # Migration from 6.6: use ir.calendar
+        if migrate_calendar:
+            update = transaction.connection.cursor()
+            cursor.execute(*month.select(month.id, month.index))
+            for month_id, index in cursor:
+                str_index = f'{index:02d}'
+                update.execute(*sql_table.update(
+                        [sql_table.start_month], [month_id],
+                        where=sql_table._temp_start_month == str_index))
+                update.execute(*sql_table.update(
+                        [sql_table.end_month], [month_id],
+                        where=sql_table._temp_end_month == str_index))
+            table_h.drop_column('_temp_start_month')
+            table_h.drop_column('_temp_end_month')
+
     def match(self, pattern):
         if 'date' in pattern:
             pattern = pattern.copy()
             date = pattern.pop('date')
             if self.start_month and self.end_month:
-                start = (int(self.start_month), self.start_day)
-                end = (int(self.end_month), self.end_day)
+                start = (self.start_month.index, self.start_day)
+                end = (self.end_month.index, self.end_day)
                 date = (date.month, date.day)
                 if start <= end:
                     if not (start <= date <= end):
diff -r 2a8527d41c92 -r 7a23a9b7c56d tests/test_module.py
--- a/tests/test_module.py      Fri Nov 18 00:18:36 2022 +0100
+++ b/tests/test_module.py      Mon Nov 21 10:12:40 2022 +0100
@@ -20,12 +20,16 @@
         Template = pool.get('product.template')
         Product_TariffCode = pool.get(
             'product-customs.tariff.code')
+        Month = pool.get('ir.calendar.month')
+
+        june, = Month.search([('index', '=', 6)])
+        august, = Month.search([('index', '=', 8)])
 
         # Test start <= end
         tariff1 = Tariff(code='170390')
         tariff2 = Tariff(code='17039099',
-                start_month='06', start_day=20,
-                end_month='08', end_day=20)
+                start_month=june, start_day=20,
+                end_month=august, end_day=20)
         Tariff.save([tariff1, tariff2])
         template = Template(tariff_codes=[
                 Product_TariffCode(tariff_code=tariff2),
@@ -40,8 +44,8 @@
             self.assertEqual(template.get_tariff_code(pattern), result)
 
         # Test start > end
-        tariff2.start_month = '08'
-        tariff2.end_month = '06'
+        tariff2.start_month = august
+        tariff2.end_month = june
         tariff2.save()
 
         for pattern, result in [
diff -r 2a8527d41c92 -r 7a23a9b7c56d view/tariff_code_form.xml
--- a/view/tariff_code_form.xml Fri Nov 18 00:18:36 2022 +0100
+++ b/view/tariff_code_form.xml Mon Nov 21 10:12:40 2022 +0100
@@ -15,11 +15,11 @@
     <label id="from" string="From"/>
     <group id="start" col="2">
         <field name="start_day"/>
-        <field name="start_month"/>
+        <field name="start_month" widget="selection"/>
     </group>
     <label id="to" string="To"/>
     <group id="end" col="2">
         <field name="end_day"/>
-        <field name="end_month"/>
+        <field name="end_month" widget="selection"/>
     </group>
 </form>

Reply via email to