From bd32465d92d12de793d4e841444d2ad56bc72e13 Mon Sep 17 00:00:00 2001 From: Ryan Cen Date: Wed, 28 Feb 2024 13:34:21 -0800 Subject: [PATCH] [FIX] product: Validate URLs on documents Added a validation for URLs on product.document to resolve an issue where incomplete links on documents will redirect to a local URL instead of an external URL. Thus leading to a page that does not exist. e.g.: youtube.com instead of https://www.youtube.com This seemed like the proper course of action as there would be no reason to not validate as any URL within the database can still be formatted properly and work the same. api.constrains does not seem to work upon creation on this model, so I opted to use the api.onchange. opw-3698591 closes odoo/odoo#155806 Signed-off-by: Ryan Cen (ryce) Co-authored-by: Morgane Demesmaeker (edm) <69820391+Demesmaeker@users.noreply.github.com> --- addons/product/i18n/product.pot | 11 +++++++++++ addons/product/models/product_document.py | 13 ++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/addons/product/i18n/product.pot b/addons/product/i18n/product.pot index 4513e590355..fc75f8df117 100644 --- a/addons/product/i18n/product.pot +++ b/addons/product/i18n/product.pot @@ -2055,6 +2055,17 @@ msgstr "" msgid "Please enter a positive whole number." msgstr "" +#. module: product +#. odoo-python +#: code:addons/product/models/product_document.py:0 +#, python-format +msgid "" +"Please enter a valid URL.\n" +"Example: https://www.odoo.com\n" +"\n" +"Invalid URL: %s" +msgstr "" + #. module: product #. odoo-python #: code:addons/product/models/product_pricelist_item.py:0 diff --git a/addons/product/models/product_document.py b/addons/product/models/product_document.py index 6b5cdc503d0..a0a0ad3b7ac 100644 --- a/addons/product/models/product_document.py +++ b/addons/product/models/product_document.py @@ -1,7 +1,8 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import api, fields, models +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError class ProductDocument(models.Model): @@ -20,6 +21,16 @@ class ProductDocument(models.Model): active = fields.Boolean(default=True) + @api.onchange('url') + def _onchange_url(self): + for attachment in self: + if attachment.type == 'url' and attachment.url and\ + not attachment.url.startswith(('https://', 'http://', 'ftp://')): + raise ValidationError(_( + "Please enter a valid URL.\nExample: https://www.odoo.com\n\nInvalid URL: %s", + attachment.url + )) + #=== CRUD METHODS ===# @api.model_create_multi