[IMP] base, model, website[*]: add get_base_url helper

Before this commit, when we want to make an external url, we need to know
if the record is related to a specific website or not, and choose base_url
or website domain.

Now each model have a fonction get_base_url (public to be callable from email
template, ...) and website override this function to add the website domain if
the record is website specific.

closes odoo/odoo#34089

Signed-off-by: Jérémy Kersten (jke) <jke@openerp.com>
This commit is contained in:
Jeremy Kersten
2019-06-13 09:26:54 +00:00
parent 22437e331a
commit f727cd2645
9 changed files with 70 additions and 37 deletions
-10
View File
@@ -33,16 +33,6 @@ class PortalMixin(models.AbstractModel):
self.sudo().write({'access_token': str(uuid.uuid4())})
return self.access_token
@api.multi
def get_base_url(self):
"""Get the base URL for the current model.
Defined here to be overriden by website specific models.
The method has to be public because it is called from mail templates.
"""
self.ensure_one()
return self.env['ir.config_parameter'].sudo().get_param('web.base.url')
def _get_share_url(self, redirect=False, signup_partner=False, pid=None):
"""
Build the url of the record that will be sent by mail and adds additional parameters such as
-7
View File
@@ -51,10 +51,3 @@ class Partner(models.Model):
# onchange uses the cache to retrieve value, we need to copy computed_value into the initial env
for record, record2 in zip(self, self2):
record.display_name = record2.display_name
@api.multi
def get_base_url(self):
"""When using multi-website, we want the user to be redirected to the
most appropriate website if possible."""
res = super(Partner, self).get_base_url()
return self.website_id and self.website_id._get_http_domain() or res
+21
View File
@@ -820,3 +820,24 @@ class Website(models.Model):
return ''
res = urls.url_parse(self.domain)
return 'http://' + self.domain if not res.scheme else self.domain
class BaseModel(models.AbstractModel):
_inherit = 'base'
@api.multi
def get_base_url(self):
"""
Returns baseurl about one given record.
If a website_id field exists in the current record we use the url
from this website as base url.
:return: the base url for this record
:rtype: string
"""
self.ensure_one()
if 'website_id' in self and self.website_id.domain:
return self.website_id._get_http_domain()
else:
return super(BaseModel, self).get_base_url()
+1
View File
@@ -2,6 +2,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import test_converter
from . import test_base_url
from . import test_get_current_website
from . import test_qweb
from . import test_crawl
+33
View File
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import odoo
import odoo.tests
@odoo.tests.tagged('-at_install', 'post_install')
class TestBaseUrl(odoo.tests.HttpCase):
def test_base_url(self):
ICP = self.env['ir.config_parameter']
Website = self.env['website']
icp_base_url = ICP.sudo().get_param('web.base.url')
domain ='https://www.domain.jke'
website = Website.create({'name': 'test base url', 'domain': domain})
without_website_id = website # a model that don't have website_id as field
with_website_id = self.env['res.partner'].create({'name': 'test base url'})
self.assertEqual(without_website_id.get_base_url(), icp_base_url)
with_website_id.website_id = False
self.assertEqual(with_website_id.get_base_url(), icp_base_url)
with_website_id.website_id = website
self.assertEqual(with_website_id.get_base_url(), domain)
website.domain = False
self.assertEqual(with_website_id.get_base_url(), icp_base_url)
@@ -10,10 +10,3 @@ class AccountInvoice(models.Model):
website_id = fields.Many2one('website', related='partner_id.website_id', string='Website',
help='Website through which this invoice was created.',
store=True, readonly=True)
@api.multi
def get_base_url(self):
"""When using multi-website, we want the user to be redirected to the
most appropriate website if possible."""
res = super(AccountInvoice, self).get_base_url()
return self.website_id and self.website_id._get_http_domain() or res
-7
View File
@@ -359,13 +359,6 @@ class SaleOrder(models.Model):
sent_orders |= order
sent_orders.write({'cart_recovery_email_sent': True})
@api.multi
def get_base_url(self):
"""When using multi-website, we want the user to be redirected to the
most appropriate website if possible."""
res = super(SaleOrder, self).get_base_url()
return self.website_id and self.website_id._get_http_domain() or res
class SaleOrderLine(models.Model):
_inherit = "sale.order.line"
-6
View File
@@ -983,12 +983,6 @@ class Partner(models.Model):
def _get_country_name(self):
return self.country_id.name or ''
@api.multi
def get_base_url(self):
"""Get the base URL for the current partner."""
self.ensure_one()
return self.env['ir.config_parameter'].sudo().get_param('web.base.url')
class ResPartnerIndustry(models.Model):
_description = 'Industry'
+15
View File
@@ -3047,6 +3047,21 @@ Fields:
r['noupdate'] = value.get('noupdate', False)
return res
@api.multi
def get_base_url(self):
"""
Returns rooturl for a specific given record.
By default, it return the ir.config.parameter of base_url
but it can be overidden by model.
:return: the base url for this record
:rtype: string
"""
self.ensure_one()
return self.env['ir.config_parameter'].sudo().get_param('web.base.url')
@api.multi
def _check_concurrency(self):
if not (self._log_access and self._context.get(self.CONCURRENCY_CHECK_FIELD)):