[FIX] website_sale: cached available pricelists

The result of the method `_get_pl` is cached:
```
@tools.ormcache('uid', 'country_code', 'show_visible', 'website_pl', 'current_pl', 'all_pl')
```

But, the result depends as well on the partner pricelist set
in the partner form (or even the pricelist used correctly in his cart),
and, unfortunately, this value is not among this method parameters.

To force the recompute of the method when the partner pricelist
change, one of the method arguments (mentioned above) must change.

To serve this purpose, another method is created, adding
the partner pricelist and the order pricelist among its
parameter, to force the recomputation of the available pricelists
when there is a change in the partner or order pricelist.

The old method is kept for retro-compatibility purposes,
and basically call the new one without the two last arguments

For information, the pricelist in the cookie is set only when
the user choose a pricelist different than the default one
among the available pricelists.

opw-670898
This commit is contained in:
Denis Ledoux
2016-03-14 10:48:30 +01:00
parent a395a54a7c
commit afe1d20dac
+20 -8
View File
@@ -156,8 +156,8 @@ class website(orm.Model):
string='Price list available for this Ecommerce/Website'), string='Price list available for this Ecommerce/Website'),
} }
@tools.ormcache('uid', 'country_code', 'show_visible', 'website_pl', 'current_pl', 'all_pl') @tools.ormcache('uid', 'country_code', 'show_visible', 'website_pl', 'current_pl', 'all_pl', 'partner_pl', 'order_pl')
def _get_pl(self, cr, uid, country_code, show_visible, website_pl, current_pl, all_pl): def _get_pl_partner_order(self, cr, uid, country_code, show_visible, website_pl, current_pl, all_pl, partner_pl=False, order_pl=False):
""" Return the list of pricelists that can be used on website for the current user. """ Return the list of pricelists that can be used on website for the current user.
:param str country_code: code iso or False, If set, we search only price list available for this country :param str country_code: code iso or False, If set, we search only price list available for this country
@@ -166,6 +166,8 @@ class website(orm.Model):
:param int current_pl: The current pricelist used on the website :param int current_pl: The current pricelist used on the website
(If not selectable but the current pricelist we had this pricelist anyway) (If not selectable but the current pricelist we had this pricelist anyway)
:param list all_pl: List of all pricelist available for this website :param list all_pl: List of all pricelist available for this website
:param int partner_pl: the partner pricelist
:param int order_pl: the current cart pricelist
:returns: list of pricelist ids :returns: list of pricelist ids
""" """
@@ -175,12 +177,12 @@ class website(orm.Model):
groups = self.pool['res.country.group'].search(cr, uid, [('country_ids.code', '=', country_code)]) groups = self.pool['res.country.group'].search(cr, uid, [('country_ids.code', '=', country_code)])
for cgroup in self.pool['res.country.group'].browse(cr, uid, groups): for cgroup in self.pool['res.country.group'].browse(cr, uid, groups):
for pll in cgroup.website_pricelist_ids: for pll in cgroup.website_pricelist_ids:
if not show_visible or pll.selectable or pll.pricelist_id.id == current_pl: if not show_visible or pll.selectable or pll.pricelist_id.id in (current_pl, order_pl):
pcs.append(pll.pricelist_id) pcs.append(pll.pricelist_id)
if not pcs: # no pricelist for this country, or no GeoIP if not pcs: # no pricelist for this country, or no GeoIP
pcs = [pll.pricelist_id for pll in all_pl pcs = [pll.pricelist_id for pll in all_pl
if not show_visible or pll.selectable or pll.pricelist_id.id == current_pl] if not show_visible or pll.selectable or pll.pricelist_id.id in (current_pl, order_pl)]
partner = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid).partner_id partner = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid).partner_id
if not pcs or partner.property_product_pricelist.id != website_pl: if not pcs or partner.property_product_pricelist.id != website_pl:
@@ -189,6 +191,10 @@ class website(orm.Model):
pcs = sorted(set(pcs), key=lambda pl: pl.name) pcs = sorted(set(pcs), key=lambda pl: pl.name)
return [pl.id for pl in pcs] return [pl.id for pl in pcs]
@tools.ormcache('uid', 'country_code', 'show_visible', 'website_pl', 'current_pl', 'all_pl')
def _get_pl(self, cr, uid, country_code, show_visible, website_pl, current_pl, all_pl):
return self._get_pl_partner_order(cr, uid, country_code, show_visible, website_pl, current_pl, all_pl)
def get_pricelist_available(self, cr, uid, show_visible=False, context=None): def get_pricelist_available(self, cr, uid, show_visible=False, context=None):
""" Return the list of pricelists that can be used on website for the current user. """ Return the list of pricelists that can be used on website for the current user.
Country restrictions will be detected with GeoIP (if installed). Country restrictions will be detected with GeoIP (if installed).
@@ -206,10 +212,15 @@ class website(orm.Model):
website_id = self.search(cr, uid, [], context=context) website_id = self.search(cr, uid, [], context=context)
website = self.browse(cr, uid, website_id, context=context) website = self.browse(cr, uid, website_id, context=context)
isocountry = request.session.geoip and request.session.geoip.get('country_code') or False isocountry = request.session.geoip and request.session.geoip.get('country_code') or False
pl_ids = self._get_pl(cr, uid, isocountry, show_visible, partner = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context).partner_id
website.user_id.sudo().partner_id.property_product_pricelist.id, order_pl = partner.last_website_so_id and partner.last_website_so_id.state == 'draft' and partner.last_website_so_id.pricelist_id
request.session.get('website_sale_current_pl'), partner_pl = partner.property_product_pricelist
website.website_pricelist_ids) pl_ids = self._get_pl_partner_order(cr, uid, isocountry, show_visible,
website.user_id.sudo().partner_id.property_product_pricelist.id,
request.session.get('website_sale_current_pl'),
website.website_pricelist_ids,
partner_pl=partner_pl and partner_pl.id or None,
order_pl=order_pl and order_pl.id or None)
return self.pool['product.pricelist'].browse(cr, uid, pl_ids, context=context) return self.pool['product.pricelist'].browse(cr, uid, pl_ids, context=context)
def is_pricelist_available(self, cr, uid, pl_id, context=None): def is_pricelist_available(self, cr, uid, pl_id, context=None):
@@ -427,6 +438,7 @@ class website_pricelist(osv.Model):
# we change the config of website price list to force to recompute. # we change the config of website price list to force to recompute.
website = self.pool['website'] website = self.pool['website']
website._get_pl.clear_cache(website) website._get_pl.clear_cache(website)
website._get_pl_partner_order.clear_cache(website)
def create(self, cr, uid, data, context=None): def create(self, cr, uid, data, context=None):
res = super(website_pricelist, self).create(cr, uid, data, context=context) res = super(website_pricelist, self).create(cr, uid, data, context=context)