← Back to team overview

openerp-community-reviewer team mailing list archive

[Merge] lp:~camptocamp/stock-logistic-barcode/7.0-product_multi_ean-migr into lp:stock-logistic-barcode

 

Guewen Baconnier @ Camptocamp has proposed merging lp:~camptocamp/stock-logistic-barcode/7.0-product_multi_ean-migr into lp:stock-logistic-barcode with lp:~camptocamp/stock-logistic-barcode/7.0-fix_shortut_imports-afe as a prerequisite.

Commit message:
Migrate product_multi_ean for version 7.0. Made the migration script working without using a dummy column

Requested reviews:
  Stock and Logistic Core Editors (stock-logistic-core-editors)

For more details, see:
https://code.launchpad.net/~camptocamp/stock-logistic-barcode/7.0-product_multi_ean-migr/+merge/209661

Migrate the module product_multi_ean (most of the work has been done by Alexandre in the prerequisite MP).

Prerequisite: https://code.launchpad.net/~camptocamp/stock-logistic-barcode/7.0-fix_shortut_imports-afe/+merge/208752
-- 
https://code.launchpad.net/~camptocamp/stock-logistic-barcode/7.0-product_multi_ean-migr/+merge/209661
Your team Stock and Logistic Core Editors is requested to review the proposed merge of lp:~camptocamp/stock-logistic-barcode/7.0-product_multi_ean-migr into lp:stock-logistic-barcode.
=== modified file 'product_multi_ean/__openerp__.py'
--- product_multi_ean/__openerp__.py	2014-03-06 12:31:39 +0000
+++ product_multi_ean/__openerp__.py	2014-03-06 12:31:39 +0000
@@ -2,7 +2,7 @@
 ##############################################################################
 #
 #    Author:  Author Guewen Baconnier
-#    Copyright 2012 Camptocamp SA
+#    Copyright 2012-2014 Camptocamp SA
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU Affero General Public License as
@@ -20,27 +20,27 @@
 ##############################################################################
 
 {'name': 'Multiple EAN13 on products',
- 'version': '1.1',
+ 'version': '1.2',
  'author': 'Camptocamp',
  'maintainer': 'Camptocamp',
  'category': 'Warehouse',
  'complexity': "normal",  # easy, normal, expert
- 'depends': ['base', 'product'],
+ 'depends': ['base',
+             'product',
+             ],
  'description': """
+Multiple EAN13 on products
+==========================
+
 Allow Multiple EAN13 on products.
 A list of EAN13 is available for each product with a priority, so a
 main ean13 code is defined.
 """,
  'website': 'http://www.camptocamp.com',
- 'init_xml': [],
- 'update_xml': [
-                'product_view.xml',
-                'security/ir.model.access.csv'],
- 'demo_xml': [],
- 'tests': [],
- 'installable': False,
- 'images': ['/static/src/images/image'],
+ 'data': ['product_view.xml',
+          'security/ir.model.access.csv',
+          ],
+ 'installable': True,
  'auto_install': False,
  'license': 'AGPL-3',
- 'application': True
 }

=== modified file 'product_multi_ean/product_ean.py'
--- product_multi_ean/product_ean.py	2014-03-06 12:31:39 +0000
+++ product_multi_ean/product_ean.py	2014-03-06 12:31:39 +0000
@@ -18,11 +18,15 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
+import logging
 
 from openerp.osv import orm, fields
 from openerp.addons.product import product
 
 
+_logger = logging.getLogger(__name__)
+
+
 class ProductEan13(orm.Model):
     _name = 'product.ean13'
     _description = "List of EAN13 for a product."
@@ -59,43 +63,30 @@
         return super(ProductEan13, self).create(cr, uid, vals, context=context)
 
 
-class ProductProductEan(orm.Model):
-    """ Inherit the model product.product in order to create the m2o link
-        This class definition has to be before the others below to ensure that
-        the table is created before the migration
-    """
-    _inherit = 'product.product'
-    _columns = {
-        'ean13_ids': fields.one2many('product.ean13', 'product_id', 'EAN13'),
-        }
-
-
-class ProductProductMigration(orm.Model):
-    """ Inherit the model product.product in order to migrate the ean13
-        column in the m2o table product_ean13
-        This class definition has to be before the others below to ensure that
-        the migration is done before the creation of the ean13 fields.function
-    """
-    _inherit = 'product.product'
-    _columns = {
-        'legacy_ean13': fields.char('Original EAN13', size=13, oldname='ean13'),
-        }
-
-    def init(self, cr):
-        cr.execute("INSERT INTO "
-                   "product_ean13 (name, product_id, sequence) "
-                   "SELECT p.legacy_ean13, p.id, 1 "
-                   "FROM product_product p WHERE "
-                   "legacy_ean13 IS NOT NULL "
-                   "AND NOT EXISTS "
-                   "(SELECT id FROM product_ean13 "
-                   " WHERE name = p.legacy_ean13 "
-                   "       AND product_id = p.id)")
-
-
 class ProductProduct(orm.Model):
     _inherit = 'product.product'
 
+    def _auto_init(self, cr, context=None):
+        sql = ("SELECT data_type "
+               "FROM information_schema.columns "
+               "WHERE table_name = 'product_product' "
+               "AND column_name = 'ean13' ")
+        cr.execute(sql)
+        column = cr.fetchone()
+        if column[0] == 'character varying':
+            # module was not installed, the column will be replaced by
+            _logger.info('migrating the EAN13')
+            cr.execute("INSERT INTO "
+                       "product_ean13 (name, product_id, sequence) "
+                       "SELECT p.ean13, p.id, 1 "
+                       "FROM product_product p WHERE "
+                       "p.ean13 IS NOT NULL ")
+            # drop the field otherwise the function field will
+            # not be computed
+            cr.execute("ALTER TABLE product_product "
+                       "DROP ean13")
+        return super(ProductProduct, self)._auto_init(cr, context=context)
+
     def _get_main_ean13(self, cr, uid, ids, _field_name, _arg, context):
         values = {}
         for product in self.browse(cr, uid, ids, context=context):
@@ -115,7 +106,7 @@
 
     def _write_ean(self, cr, uid, product_id, _name, value, _arg, context=None):
         product = self.browse(cr, uid, product_id, context=context)
-        if not value in [ean.name for ean in product.ean13_ids]:
+        if value and value not in [ean.name for ean in product.ean13_ids]:
             self.pool.get('product.ean13').create(
                 cr, uid,
                 {'name': value, 'product_id': product.id},
@@ -123,6 +114,7 @@
         return True
 
     _columns = {
+        'ean13_ids': fields.one2many('product.ean13', 'product_id', 'EAN13'),
         'ean13': fields.function(
             _get_main_ean13,
             fnct_inv=_write_ean,

=== modified file 'product_multi_ean/product_view.xml'
--- product_multi_ean/product_view.xml	2012-11-26 15:34:01 +0000
+++ product_multi_ean/product_view.xml	2014-03-06 12:31:39 +0000
@@ -5,21 +5,22 @@
             <field name="name">product.normal.form.multi.ean13</field>
             <field name="model">product.product</field>
             <field name="inherit_id" ref="product.product_normal_form_view" />
-            <field name="type">form</field>
             <field name="arch" type="xml">
-                <group colspan="2" col="2" name="misc" position="inside">
-                    <field groups="base.group_extended" name="ean13_ids" select="2">
+                <xpath expr="/form//div[@name='ean']/preceding-sibling::label[1]" position="replace">
+                </xpath>
+                <xpath expr="/form//div[@name='ean']" position="replace">
+                    <field name="ean13_ids">
                         <form string="EAN13">
-                            <field name="sequence"/>
                             <field name="name"/>
                         </form>
                         <tree string="EAN13">
-                            <field name="sequence"/>
+                            <field name="sequence" widget="handle"/>
                             <field name="name"/>
                         </tree>
                     </field>
-                </group>
+                    <field name="ean13" invisible="1"/>
+                </xpath>
             </field>
         </record>
     </data>
-</openerp>
\ No newline at end of file
+</openerp>


Follow ups