From d280def3e750cfaaea475e6e3f1ad2e73d70202e Mon Sep 17 00:00:00 2001 From: mreficent Date: Mon, 28 Oct 2019 15:19:51 +0100 Subject: [PATCH 1/2] [FIX] name_get method should also search for customerinfo This commit also includes _name_search customization from product_supplierinfo_for_customer_sale, because it makes sense to put in here instead of in the other module. --- .../models/product_product.py | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/product_supplierinfo_for_customer/models/product_product.py b/product_supplierinfo_for_customer/models/product_product.py index 71002c92b046..426762447d4b 100644 --- a/product_supplierinfo_for_customer/models/product_product.py +++ b/product_supplierinfo_for_customer/models/product_product.py @@ -10,6 +10,129 @@ class ProductProduct(models.Model): _inherit = 'product.product' + @api.multi + def name_get(self): + + def _name_get(d): + name = d.get('name', '') + code = self._context.get('display_default_code', True) and d.get( + 'default_code', False) or False + if code: + name = '[%s] %s' % (code, name) + return d['id'], name + + partner_id = self._context.get('partner_id') + if partner_id: + partner_ids = [partner_id, self.env['res.partner'].browse( + partner_id).commercial_partner_id.id] + else: + partner_ids = [] + + self.check_access_rights("read") + self.check_access_rule("read") + + res = [] + + # Prefetch the fields used by the `name_get` + self.sudo().read( + ['name', 'default_code', 'product_tmpl_id', + 'attribute_value_ids', 'attribute_line_ids'], load=False) + + product_template_ids = self.sudo().mapped('product_tmpl_id').ids + + if partner_ids: + supplier_info = self.env['product.supplierinfo'].sudo().search([ + ('product_tmpl_id', 'in', product_template_ids), + ('name', 'in', partner_ids), + ]) + customer_info = self.env['product.customerinfo'].sudo().search([ + ('product_tmpl_id', 'in', product_template_ids), + ('name', 'in', partner_ids), + ]) + # Prefetch the fields used by the `name_get` + supplier_info.sudo().read( + ['product_tmpl_id', 'product_id', + 'product_name', 'product_code'], load=False) + customer_info.sudo().read( + ['product_tmpl_id', 'product_id', + 'product_name', 'product_code'], load=False) + partner_info_by_template = {} + for r in supplier_info: + partner_info_by_template.setdefault( + r.product_tmpl_id, []).append(r) + for r in customer_info: + partner_info_by_template.setdefault( + r.product_tmpl_id, []).append(r) + for product in self.sudo(): + variable_attributes = product.attribute_line_ids.filtered( + lambda l: len(l.value_ids) > 1).mapped('attribute_id') + variant = product.attribute_value_ids._variant_name( + variable_attributes) + + product_name = variant and "%s (%s)" % (product.name, variant + ) or product.name + partners = [] + if partner_ids: + product_partner_info = partner_info_by_template.get( + product.product_tmpl_id, []) + partners = [x for x in product_partner_info + if x.product_id and x.product_id == product] + if not partners: + partners = [ + x for x in product_partner_info if not x.product_id] + if partners: + for c in partners: + partner_variant = c.product_name and ( + variant and "%s (%s)" % (c.product_name, variant + ) or c.product_name + ) or False + mydict = { + 'id': product.id, + 'name': partner_variant or product_name, + 'default_code': c.product_code or product.default_code, + } + temp = _name_get(mydict) + if temp not in res: + res.append(temp) + else: + mydict = { + 'id': product.id, + 'name': product_name, + 'default_code': product.default_code, + } + res.append(_name_get(mydict)) + return res + + @api.model + def _name_search(self, name='', args=None, operator='ilike', limit=100, + name_get_uid=None): + res = super(ProductProduct, self)._name_search( + name, args=args, operator=operator, limit=limit, + name_get_uid=name_get_uid) + if not limit or len(res) >= limit: + limit = (limit - len(res)) if limit else False + if (not name and limit or not self._context.get('partner_id') or + len(res) >= limit): + return res + limit -= len(res) + customerinfo_ids = self.env['product.customerinfo']._search( + [('name', '=', self._context.get('partner_id')), '|', + ('product_code', operator, name), + ('product_name', operator, name)], limit=limit, + name_get_uid=name_get_uid) + if not customerinfo_ids: + return res + res_templates = self.browse( + [product_id for product_id, _name in res] + ).mapped('product_tmpl_id') + product_tmpls = self.env['product.customerinfo'].browse( + customerinfo_ids).mapped('product_tmpl_id') - res_templates + product_ids = self._search( + [('product_tmpl_id', 'in', product_tmpls.ids)], limit=limit, + name_get_uid=name_get_uid) + res.extend(self.browse(product_ids).name_get()) + return res + @api.multi def _get_price_from_customerinfo(self, partner_id): self.ensure_one() From 3d7ec4edf46e297071c8799cc9e3767d2add5353 Mon Sep 17 00:00:00 2001 From: mreficent Date: Mon, 28 Oct 2019 19:52:35 +0100 Subject: [PATCH 2/2] [FIX] superhack: avoid verbatim copy of name_get method --- .../models/__init__.py | 1 + .../models/product_product.py | 91 +------------------ .../models/product_supplierinfo.py | 38 ++++++++ 3 files changed, 41 insertions(+), 89 deletions(-) create mode 100644 product_supplierinfo_for_customer/models/product_supplierinfo.py diff --git a/product_supplierinfo_for_customer/models/__init__.py b/product_supplierinfo_for_customer/models/__init__.py index b8c3c61ce210..db3a32ab57fc 100644 --- a/product_supplierinfo_for_customer/models/__init__.py +++ b/product_supplierinfo_for_customer/models/__init__.py @@ -1,5 +1,6 @@ from . import pricelist from . import product_customerinfo +from . import product_supplierinfo from . import product_product from . import product_template from . import res_partner diff --git a/product_supplierinfo_for_customer/models/product_product.py b/product_supplierinfo_for_customer/models/product_product.py index 426762447d4b..48bcfb164b5d 100644 --- a/product_supplierinfo_for_customer/models/product_product.py +++ b/product_supplierinfo_for_customer/models/product_product.py @@ -12,95 +12,8 @@ class ProductProduct(models.Model): @api.multi def name_get(self): - - def _name_get(d): - name = d.get('name', '') - code = self._context.get('display_default_code', True) and d.get( - 'default_code', False) or False - if code: - name = '[%s] %s' % (code, name) - return d['id'], name - - partner_id = self._context.get('partner_id') - if partner_id: - partner_ids = [partner_id, self.env['res.partner'].browse( - partner_id).commercial_partner_id.id] - else: - partner_ids = [] - - self.check_access_rights("read") - self.check_access_rule("read") - - res = [] - - # Prefetch the fields used by the `name_get` - self.sudo().read( - ['name', 'default_code', 'product_tmpl_id', - 'attribute_value_ids', 'attribute_line_ids'], load=False) - - product_template_ids = self.sudo().mapped('product_tmpl_id').ids - - if partner_ids: - supplier_info = self.env['product.supplierinfo'].sudo().search([ - ('product_tmpl_id', 'in', product_template_ids), - ('name', 'in', partner_ids), - ]) - customer_info = self.env['product.customerinfo'].sudo().search([ - ('product_tmpl_id', 'in', product_template_ids), - ('name', 'in', partner_ids), - ]) - # Prefetch the fields used by the `name_get` - supplier_info.sudo().read( - ['product_tmpl_id', 'product_id', - 'product_name', 'product_code'], load=False) - customer_info.sudo().read( - ['product_tmpl_id', 'product_id', - 'product_name', 'product_code'], load=False) - partner_info_by_template = {} - for r in supplier_info: - partner_info_by_template.setdefault( - r.product_tmpl_id, []).append(r) - for r in customer_info: - partner_info_by_template.setdefault( - r.product_tmpl_id, []).append(r) - for product in self.sudo(): - variable_attributes = product.attribute_line_ids.filtered( - lambda l: len(l.value_ids) > 1).mapped('attribute_id') - variant = product.attribute_value_ids._variant_name( - variable_attributes) - - product_name = variant and "%s (%s)" % (product.name, variant - ) or product.name - partners = [] - if partner_ids: - product_partner_info = partner_info_by_template.get( - product.product_tmpl_id, []) - partners = [x for x in product_partner_info - if x.product_id and x.product_id == product] - if not partners: - partners = [ - x for x in product_partner_info if not x.product_id] - if partners: - for c in partners: - partner_variant = c.product_name and ( - variant and "%s (%s)" % (c.product_name, variant - ) or c.product_name - ) or False - mydict = { - 'id': product.id, - 'name': partner_variant or product_name, - 'default_code': c.product_code or product.default_code, - } - temp = _name_get(mydict) - if temp not in res: - res.append(temp) - else: - mydict = { - 'id': product.id, - 'name': product_name, - 'default_code': product.default_code, - } - res.append(_name_get(mydict)) + res = super(ProductProduct, self.with_context(customerinfo=True)).\ + name_get() return res @api.model diff --git a/product_supplierinfo_for_customer/models/product_supplierinfo.py b/product_supplierinfo_for_customer/models/product_supplierinfo.py new file mode 100644 index 000000000000..4f772c56e2e0 --- /dev/null +++ b/product_supplierinfo_for_customer/models/product_supplierinfo.py @@ -0,0 +1,38 @@ +# Copyright 2019 Eficent Business and IT Consulting Services S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from odoo import api, models + + +class ProductSupplierInfo(models.Model): + _inherit = "product.supplierinfo" + + @api.model + def search(self, args, offset=0, limit=None, order=None, count=False): + res = super(ProductSupplierInfo, self).search( + args, offset=offset, limit=limit, order=order, count=count) + if self.env.context.get('customerinfo') and \ + self._name == 'product.supplierinfo': + limit2 = limit-len(res) if limit else limit + res2 = self.env['product.customerinfo'].search( + args, offset=offset, limit=limit2, order=order, count=count) + res2 = res2.read( + list(self.env['product.supplierinfo']._fields.keys())) + for result in res2: + res += self.env['product.supplierinfo'].new(result) + return res + + @api.multi + def read(self, fields=None, load='_classic_read'): + if self.env.context.get('customerinfo') and \ + self._name == 'product.supplierinfo': + has_ids = self.filtered( + lambda x: x.id in x._ids and isinstance(x.id, (int,))) + new_ids = self.filtered( + lambda x: x.id in x._ids and not isinstance(x.id, (int,))) + return super(ProductSupplierInfo, has_ids).read( + fields=fields, load=load) + [ + {f: x[f] for f in x._fields if ( + f in fields if fields else True)} for x in new_ids] + else: + return super(ProductSupplierInfo, self).read( + fields=fields, load=load)