Skip to content

Commit

Permalink
Group computation of qty_available
Browse files Browse the repository at this point in the history
Stats with 6700 products:

Before: 570s
After: 28s
  • Loading branch information
guewen committed Aug 21, 2017
1 parent 6e08589 commit 22926c7
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions connector_prestashop/models/product_template/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from collections import defaultdict

from openerp import _, exceptions, api, fields, models
from openerp.addons.decimal_precision import decimal_precision as dp

Expand Down Expand Up @@ -109,17 +111,23 @@ class PrestashopProductTemplate(models.Model):
digits_compute=dp.get_precision('Product Price'),
)

RECOMPUTE_QTY_STEP = 1000

@api.multi
def recompute_prestashop_qty(self):
for product_binding in self:
new_qty = product_binding._prestashop_qty()
if product_binding.quantity != new_qty:
product_binding.quantity = new_qty
# group products by backend
backends = defaultdict(self.browse)
for product in self:
backends[product.backend_id] |= product

for backend, products in backends.iteritems():
products._recompute_prestashop_qty_backend(backend)
return True

def _prestashop_qty(self):
root_location = (self.backend_id.stock_location_id or
self.backend_id.warehouse_id.lot_stock_id)
@api.multi
def _recompute_prestashop_qty_backend(self, backend):
root_location = (backend.stock_location_id or
backend.warehouse_id.lot_stock_id)
locations = self.env['stock.location'].search([
('id', 'child_of', root_location.id),
('prestashop_synchronized', '=', True),
Expand All @@ -140,8 +148,16 @@ def _prestashop_qty(self):
_('No internal location found to compute the product '
'quantity.')
)
self_c = self.with_context(location=locations.ids, compute_child=False)
return self_c.qty_available
self_loc = self.with_context(location=locations.ids,
compute_child=False)
for product in self_loc:
new_qty = product._prestashop_qty()
if product.quantity != new_qty:
product.quantity = new_qty
return True

def _prestashop_qty(self):
return self.qty_available


@prestashop
Expand Down

0 comments on commit 22926c7

Please sign in to comment.