[IMP] models, website: allow get_base_url() on empty recordset
Before this commit, `get_base_url()` could not be called on an empty recordset. It might be called on an empty recordset for instance when getting the paypal payment endpoint URLs. This commit allows that and returns the ICP value in that case. The goal is to always use the util method `get_base_url()` instead of directly accessing the ICP. closes odoo/odoo#68201 Related: odoo/enterprise#17538 Signed-off-by: Jérémy Kersten (jke) <jke@openerp.com>
This commit is contained in:
@@ -20,7 +20,11 @@ class BaseModel(models.AbstractModel):
|
||||
:return: the base url for this record
|
||||
:rtype: string
|
||||
"""
|
||||
# Ensure zero or one record
|
||||
if not self:
|
||||
return super().get_base_url()
|
||||
self.ensure_one()
|
||||
|
||||
if self._name == 'website':
|
||||
# Note that website_1.company_id.website_id might not be website_1
|
||||
return self._get_http_domain() or super().get_base_url()
|
||||
|
||||
@@ -117,3 +117,12 @@ class TestGetBaseUrl(odoo.tests.TransactionCase):
|
||||
website_2.unlink()
|
||||
# .. on unlink ..
|
||||
self.assertEqual(attach.get_base_url(), web_base_url, "Cache should be recomputed, no more website for company_1.")
|
||||
|
||||
def test_02_get_base_url_recordsets(self):
|
||||
Attachment = self.env['ir.attachment']
|
||||
web_base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
|
||||
self.assertEqual(Attachment.get_base_url(), web_base_url, "Empty recordset should get ICP value.")
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
# if more than one record, an error we should be raised
|
||||
Attachment.search([], limit=2).get_base_url()
|
||||
|
||||
@@ -14,7 +14,6 @@ class PaymentAcquirer(models.Model):
|
||||
)
|
||||
|
||||
def get_base_url(self):
|
||||
self.ensure_one()
|
||||
# Give priority to url_root to handle multi-website cases
|
||||
if request and request.httprequest.url_root:
|
||||
return request.httprequest.url_root
|
||||
|
||||
+2
-1
@@ -3320,7 +3320,8 @@ Fields:
|
||||
:rtype: string
|
||||
|
||||
"""
|
||||
self.ensure_one()
|
||||
if len(self) > 1:
|
||||
raise ValueError("Expected singleton or no record: %s" % self)
|
||||
return self.env['ir.config_parameter'].sudo().get_param('web.base.url')
|
||||
|
||||
def _check_concurrency(self):
|
||||
|
||||
Reference in New Issue
Block a user