[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:
@@ -33,16 +33,6 @@ class PortalMixin(models.AbstractModel):
|
|||||||
self.sudo().write({'access_token': str(uuid.uuid4())})
|
self.sudo().write({'access_token': str(uuid.uuid4())})
|
||||||
return self.access_token
|
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):
|
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
|
Build the url of the record that will be sent by mail and adds additional parameters such as
|
||||||
|
|||||||
@@ -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
|
# onchange uses the cache to retrieve value, we need to copy computed_value into the initial env
|
||||||
for record, record2 in zip(self, self2):
|
for record, record2 in zip(self, self2):
|
||||||
record.display_name = record2.display_name
|
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
|
|
||||||
|
|||||||
@@ -820,3 +820,24 @@ class Website(models.Model):
|
|||||||
return ''
|
return ''
|
||||||
res = urls.url_parse(self.domain)
|
res = urls.url_parse(self.domain)
|
||||||
return 'http://' + self.domain if not res.scheme else 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()
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||||
|
|
||||||
from . import test_converter
|
from . import test_converter
|
||||||
|
from . import test_base_url
|
||||||
from . import test_get_current_website
|
from . import test_get_current_website
|
||||||
from . import test_qweb
|
from . import test_qweb
|
||||||
from . import test_crawl
|
from . import test_crawl
|
||||||
|
|||||||
@@ -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',
|
website_id = fields.Many2one('website', related='partner_id.website_id', string='Website',
|
||||||
help='Website through which this invoice was created.',
|
help='Website through which this invoice was created.',
|
||||||
store=True, readonly=True)
|
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
|
|
||||||
|
|||||||
@@ -359,13 +359,6 @@ class SaleOrder(models.Model):
|
|||||||
sent_orders |= order
|
sent_orders |= order
|
||||||
sent_orders.write({'cart_recovery_email_sent': True})
|
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):
|
class SaleOrderLine(models.Model):
|
||||||
_inherit = "sale.order.line"
|
_inherit = "sale.order.line"
|
||||||
|
|||||||
@@ -983,12 +983,6 @@ class Partner(models.Model):
|
|||||||
def _get_country_name(self):
|
def _get_country_name(self):
|
||||||
return self.country_id.name or ''
|
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):
|
class ResPartnerIndustry(models.Model):
|
||||||
_description = 'Industry'
|
_description = 'Industry'
|
||||||
|
|||||||
@@ -3047,6 +3047,21 @@ Fields:
|
|||||||
r['noupdate'] = value.get('noupdate', False)
|
r['noupdate'] = value.get('noupdate', False)
|
||||||
return res
|
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
|
@api.multi
|
||||||
def _check_concurrency(self):
|
def _check_concurrency(self):
|
||||||
if not (self._log_access and self._context.get(self.CONCURRENCY_CHECK_FIELD)):
|
if not (self._log_access and self._context.get(self.CONCURRENCY_CHECK_FIELD)):
|
||||||
|
|||||||
Reference in New Issue
Block a user