From 91138e2883ffaa0b21988730392d58e622cdde7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Pigeon?= Date: Sat, 9 Sep 2017 17:36:43 +0200 Subject: [PATCH] [FIX] get rif off really useless stock_field on binding models causing performance issues --- connector_lengow/models/product.py | 44 +++++++++++++----------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/connector_lengow/models/product.py b/connector_lengow/models/product.py index fe1c775..1a4b91d 100644 --- a/connector_lengow/models/product.py +++ b/connector_lengow/models/product.py @@ -32,9 +32,6 @@ class LengowProductProduct(models.Model): string='Product', required=True, ondelete='restrict') - lengow_qty = fields.Float(string='Computed Stock Quantity', - help="Last computed quantity to send " - "on Lengow.") active = fields.Boolean(default=True) lengow_price = fields.Float('Price', compute='_compute_price', @@ -57,24 +54,6 @@ def export_products(self, catalogue_id): res = exporter.run(catalogue_id) return res - @api.multi - def compute_lengow_qty(self): - for product in self: - if product.catalogue_id.product_stock_field_id: - stock_field = product.catalogue_id.product_stock_field_id.name - else: - stock_field = 'virtual_available' - - location = self.env['stock.location'] - if self.env.context.get('location'): - location = location.browse(self.env.context['location']) - else: - location = product.catalogue_id.warehouse_id.lot_stock_id - - product.sudo().write({'lengow_qty': - product.with_context(location=location.id) - [stock_field]}) - @api.multi def _compute_price(self): for product in self: @@ -203,7 +182,8 @@ def BRAND(self, record): @mapping def QUANTITY(self, record): - qty = int(record.lengow_qty) + stock_field = record.env.context.get('stock_field') + qty = int(record[stock_field]) return {'QUANTITY': qty} @mapping @@ -281,13 +261,27 @@ def run(self, catalogue=None): adapter = self.component(usage='backend.adapter') csvFile = StringIO() csvRows = [adapter.getCSVHeader()] - products = catalogue.binded_product_ids - products.compute_lengow_qty() + + location = self.env['stock.location'] + if self.env.context.get('location'): + location = location.browse(self.env.context['location']) + else: + location = catalogue.warehouse_id.lot_stock_id + if catalogue.default_lang_id: lang = catalogue.default_lang_id.code else: lang = self.env.user.lang or 'en_US' - for product in products.with_context(lang=lang): + + if catalogue.product_stock_field_id: + stock_field = catalogue.product_stock_field_id.name + else: + stock_field = 'virtual_available' + + products = catalogue.binded_product_ids.with_context( + lang=lang, location=location.id, stock_field=stock_field) + + for product in products: self._lengow_record = product map_record = self._map_data() vals = self.export(map_record)