diff --git a/product_multi_image/__init__.py b/product_multi_image/__init__.py
index 5dd39148298..3f02c6d16dc 100644
--- a/product_multi_image/__init__.py
+++ b/product_multi_image/__init__.py
@@ -21,3 +21,60 @@
##############################################################################
from . import models
+from openerp import SUPERUSER_ID
+import imghdr
+import base64
+import unicodedata
+import re
+try:
+ import slugify as slugify_lib
+except ImportError:
+ slugify_lib = None
+
+def get_slug(name):
+ if slugify_lib:
+ try:
+ return slugify_lib.slugify(name)
+ except TypeError:
+ pass
+ uni = unicodedata.normalize('NFKD', name).encode(
+ 'ascii', 'ignore').decode('ascii')
+ slug = re.sub(r'[\W_]', ' ', uni).strip().lower()
+ slug = re.sub(r'[-\s]+', '-', slug)
+ return slug
+
+def get_from_product_medium(cr):
+# product_template_obj = registry['product.template']
+# products_ids = product_template_obj.search(
+# cr, SUPERUSER_ID, [('product_images', '=', False),
+# ('image_medium', '!=', False)])
+ cr.execute("SELECT id, image_medium, name "
+ "FROM product_template "
+ "WHERE image_medium is not null and"
+ " ID not in (SELECT product_tmpl_id"
+ " FROM product_images)")
+ products = cr.fetchall()
+ for product in products:
+ extension = imghdr.what('', base64.b64decode(
+ product[1]))
+ extension = '.{}'.format(extension or 'jpe')
+ cr.execute("INSERT INTO product_images(file_db_store,name,extension,"
+ "product_tmpl_id,link)"
+ "VALUES('{}','{}','{}',{},False)".format(
+ product[1], get_slug(product[2]), extension,
+ product[0]))
+
+
+def get_from_product_images(cr, registry):
+ old_images_obj = registry['product.images']
+ old_images_ids = old_images_obj.search(cr, SUPERUSER_ID, [])
+ products_list = []
+ for o in old_images_obj.browse(cr, SUPERUSER_ID, old_images_ids):
+ products_list.append(o.product_tmpl_id.id)
+ image_type = 'url' if o.link else 'db'
+ cr.execute("INSERT INTO product_image(file_db_store, "
+ "name,extension,url,comments,product_id,type)"
+ "VALUES('{}','{}','{}','{}','{}',{},'{}')".format(
+ o.file_db_store, get_slug(o.name), o.extension, o.url,
+ o.comments, o.product_tmpl_id.id, image_type))
+
diff --git a/product_multi_image/__openerp__.py b/product_multi_image/__openerp__.py
index 42391153644..ee8dc58502c 100644
--- a/product_multi_image/__openerp__.py
+++ b/product_multi_image/__openerp__.py
@@ -35,4 +35,6 @@
'views/res_company_view.xml'
],
'installable': True,
+ "pre_init_hook": "get_from_product_medium",
+ "post_init_hook": "get_from_product_images",
}
diff --git a/product_multi_image/models/product_image.py b/product_multi_image/models/product_image.py
index 55d5766fa11..33f62cca053 100644
--- a/product_multi_image/models/product_image.py
+++ b/product_multi_image/models/product_image.py
@@ -116,6 +116,9 @@ def _check_filestore(self):
product_id = fields.Many2one(
comodel_name='product.template', string='Product', required=True,
ondelete='cascade')
+ products = fields.Many2many(comodel_name='product.product',
+ relation='image_product_product_rel',
+ store=True, string='Variants')
def _make_pretty(self, name):
return name.replace('_', ' ').capitalize()
diff --git a/product_multi_image/models/product_product.py b/product_multi_image/models/product_product.py
index 990e0dbff60..4682a6b444d 100644
--- a/product_multi_image/models/product_product.py
+++ b/product_multi_image/models/product_product.py
@@ -20,11 +20,28 @@
#
##############################################################################
import logging
+import imghdr
+import base64
from openerp import models, fields, api, _
_logger = logging.getLogger(__name__)
+class Product(models.Model):
+ _inherit = "product.product"
+
+ @api.one
+ @api.depends("product_tmpl_id.image_ids.products")
+ def _get_images(self):
+ self.images = self.product_tmpl_id.image_ids.filtered(
+ lambda x: self in x.products)
+
+ images = fields.Many2many(comodel_name="product.image",
+ relation='product_product_image_rel',
+ compute='_get_images',
+ string="Images")
+
+
class ProductProduct(models.Model):
_inherit = "product.template"
@@ -40,13 +57,17 @@ def _get_main_image(self):
self.image_small = self.image_ids[0].image_small
def _set_image(self, image):
- if self.image:
+ if image:
+ extension = imghdr.what('', base64.b64decode(image))
+ extension = '.{}'.format(extension or 'jpg')
if self.image_ids:
self.image_ids[0].write({'type': 'db',
- 'file_db_store': image})
+ 'file_db_store': image,
+ 'extension': extension})
else:
self.image_ids = [(0, 0, {'type': 'db',
'file_db_store': image,
+ 'extension': extension,
'name': _('Main image')})]
elif self.image_ids:
self.image_ids[0].unlink()
diff --git a/product_multi_image/views/product_image_view.xml b/product_multi_image/views/product_image_view.xml
index 113906c2ce6..635ba358ff4 100644
--- a/product_multi_image/views/product_image_view.xml
+++ b/product_multi_image/views/product_image_view.xml
@@ -37,6 +37,11 @@
readonly="True"
nolabel="1" />
+
+
+
+
diff --git a/product_multi_image/views/product_product_view.xml b/product_multi_image/views/product_product_view.xml
index 8c9524e9ba1..4569e43ee2c 100644
--- a/product_multi_image/views/product_product_view.xml
+++ b/product_multi_image/views/product_product_view.xml
@@ -16,6 +16,17 @@
+
+
+ product.product.images.m2m
+ product.product
+
+
+
+
+
+
+