[IMP] website_sale: restrict deleting/archiving pricelist for ecommerce

Before this commit, if none of the pricelist is available for the website
then frontend ecommerce will crash. This issue is arrived from #30675 PR.
The reason behind crash is, if there is no pricelist associated with
the website then the ecommerce will not get the currency and breaks the
frontend shop.

This commit will restrict user from making those changes which breaks the
frontend shop. We will ensure that atleast one pricelist is available for
each website.

task-1966811
Closes: #32534

Signed-off-by: Jérémy Kersten (jke) <jke@openerp.com>
This commit is contained in:
mgh-odoo
2019-04-10 06:49:37 +00:00
committed by Romain Derie
parent 5eaa4f2967
commit b993a2d9c8
2 changed files with 12 additions and 2 deletions
+9 -1
View File
@@ -2,7 +2,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models, tools, _
from odoo.exceptions import ValidationError
from odoo.exceptions import ValidationError, UserError
from odoo.addons import decimal_precision as dp
from odoo.addons.website.models import ir_http
from odoo.tools.translate import html_translate
@@ -55,12 +55,15 @@ class ProductPricelist(models.Model):
@api.multi
def write(self, data):
res = super(ProductPricelist, self).write(data)
if data.keys() & {'code', 'active', 'website_id', 'selectable'}:
self._check_website_pricelist()
self.clear_cache()
return res
@api.multi
def unlink(self):
res = super(ProductPricelist, self).unlink()
self._check_website_pricelist()
self.clear_cache()
return res
@@ -78,6 +81,11 @@ class ProductPricelist(models.Model):
res = res.filtered(lambda pl: pl._is_available_on_website(website.id))
return res
def _check_website_pricelist(self):
for website in self.env['website'].search([]):
if not website.pricelist_ids:
raise UserError(_("With this action, '%s' website would not have any pricelist available.") % (website.name))
@api.multi
def _is_available_on_website(self, website_id):
""" To be able to be used on a website, a pricelist should either:
@@ -65,6 +65,7 @@ class TestWebsitePriceList(TransactionCase):
self.addCleanup(patcher.stop)
def get_pl(self, show, current_pl, country):
self.website.invalidate_cache(['pricelist_ids'], [self.website.id])
pl_ids = self.website._get_pl_partner_order(
country,
show,
@@ -185,7 +186,7 @@ class TestWebsitePriceListAvailable(TransactionCase):
self.website2 = Website.create({'name': 'Website 2'})
# Remove existing pricelists and create new ones
Pricelist.search([]).write({'active': False})
existing_pricelists = Pricelist.search([])
self.backend_pl = Pricelist.create({
'name': 'Backend Pricelist',
'website_id': False,
@@ -230,6 +231,7 @@ class TestWebsitePriceListAvailable(TransactionCase):
'name': 'Website 2 Pricelist',
'website_id': self.website2.id,
})
existing_pricelists.write({'active': False})
simulate_frontend_context(self)