Skip to content
Browse files

[FIX] website, website_sale: create utility method to check if frontend

Before this commit, at multiple places to check if we were in a frontend
context we would check if `request and hasattr(request, 'website')` and
then access ``.

Now, we call an utility method to get that website if we are in a frontend
context. If not the method will return False.

This method will be usefull for incoming pricelist fixes & tests.
Indeed, we will have more check of that kind and this will be easier to mock as
mocking this method will be enough, instead of mocking every method doing that
if condition to simulate a frontend context (to do website tests in python
wihout having to go frontend with a tour).
  • Loading branch information...
rdeodoo committed Feb 12, 2019
1 parent 2942387 commit 25a8b507b130453439bcf727e5a9ed087418cc84
@@ -42,6 +42,23 @@ def sitemap_qs2dom(qs, route, field='name'):
return dom

def get_request_website():
""" Return the website set on `request` if called in a frontend context
(website=True on route).
This method can typically be used to check if we are in the frontend.
This method is easy to mock during python tests to simulate frontend
context, rather than mocking every method accessing
Don't import directly the method or it won't be mocked during tests, do:
from import ir_http
my_var = ir_http.get_request_website()
return request and getattr(request, 'website', False) or False

class Http(models.AbstractModel):
_inherit = 'ir.http'

@@ -2,7 +2,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models
from odoo.http import request
from import ir_http

class ResPartner(models.Model):
@@ -16,10 +16,11 @@ def _compute_last_website_so_id(self):
for partner in self:
is_public = any([u._is_public()
for u in partner.with_context(active_test=False).user_ids])
if request and hasattr(request, 'website') and not is_public:
website = ir_http.get_request_website()
if website and not is_public:
partner.last_website_so_id =[
('partner_id', '=',,
('website_id', '=',,
('website_id', '=',,
('state', '=', 'draft'),
], order='write_date desc', limit=1)
@@ -6,6 +6,7 @@
from odoo import api, fields, models, tools

from odoo.http import request
from import ir_http

_logger = logging.getLogger(__name__)

@@ -86,7 +87,7 @@ def get_pricelist_available(self, show_visible=False):
:param bool show_visible: if True, we don't display pricelist where selectable is False (Eg: Code promo)
:returns: pricelist recordset
website = request and hasattr(request, 'website') and or None
website = ir_http.get_request_website()
if not website:
if self.env.context.get('website_id'):
website = self.browse(self.env.context['website_id'])

1 comment on commit 25a8b50


This comment has been minimized.

Copy link
Contributor Author

commented on 25a8b50 Mar 18, 2019

Coming from #28301

Please sign in to comment.
You can’t perform that action at this time.