summaryrefslogtreecommitdiff
path: root/addons
diff options
context:
space:
mode:
authorMunaf Khan <mub@odoo.com>2021-01-27 09:47:08 +0000
committerDharmraj Jhala <dja@odoo.com>2021-02-12 04:59:25 +0000
commitfe177e76f92c38216afb7c977d91dece856e6bf1 (patch)
tree408329ea558ea3d7683fd226877f92f65ce238c5 /addons
parent8bd81014f317c693b915a38d1d4dec0c5e490f90 (diff)
[FIX] crm: prevent crash due to invalid system parameter
Right now, if someone changes the `crm.pls_start_date` system parameter to something that is not valid, opening a settings gives you traceback. This commit improves the behavior by making sure that the compute field on settings page `predictive_lead_scoring_start_date` always contains a valid date. For doing so, if the system parameter is not a valid date(or not set or deleted), we set the above field to the date 8 days prior to current date, and thus, when saving the settings, the system parameter is again set with a valid date. closes odoo/odoo#65985 Taskid: 2448248 X-original-commit: ead10c7e0a22ce65a88558cdfaa99198ac27e524 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
Diffstat (limited to 'addons')
-rw-r--r--addons/crm/models/res_config_settings.py13
-rw-r--r--addons/crm/tests/test_crm_pls.py25
2 files changed, 36 insertions, 2 deletions
diff --git a/addons/crm/models/res_config_settings.py b/addons/crm/models/res_config_settings.py
index f01f8ea93c1..66fbc7335ca 100644
--- a/addons/crm/models/res_config_settings.py
+++ b/addons/crm/models/res_config_settings.py
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
+from datetime import timedelta
+
from odoo import api, fields, models
@@ -67,7 +69,16 @@ class ResConfigSettings(models.TransientModel):
""" As config_parameters does not accept Date field,
we get the date back from the Char config field, to ease the configuration in config panel """
for setting in self:
- setting.predictive_lead_scoring_start_date = fields.Date.to_date(setting.predictive_lead_scoring_start_date_str)
+ lead_scoring_start_date = setting.predictive_lead_scoring_start_date_str
+ # if config param is deleted / empty, set the date 8 days prior to current date
+ if not lead_scoring_start_date:
+ setting.predictive_lead_scoring_start_date = fields.Date.to_date(fields.Date.today() - timedelta(days=8))
+ else:
+ try:
+ setting.predictive_lead_scoring_start_date = fields.Date.to_date(lead_scoring_start_date)
+ except ValueError:
+ # the config parameter is malformed, so set the date 8 days prior to current date
+ setting.predictive_lead_scoring_start_date = fields.Date.to_date(fields.Date.today() - timedelta(days=8))
def _inverse_pls_start_date_str(self):
""" As config_parameters does not accept Date field,
diff --git a/addons/crm/tests/test_crm_pls.py b/addons/crm/tests/test_crm_pls.py
index 24c50111832..2d6db0d17b5 100644
--- a/addons/crm/tests/test_crm_pls.py
+++ b/addons/crm/tests/test_crm_pls.py
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
+from datetime import timedelta
+
+from odoo import fields, tools
from odoo.tests.common import TransactionCase
-from odoo import tools
class TestCRMPLS(TransactionCase):
@@ -376,3 +378,24 @@ class TestCRMPLS(TransactionCase):
self.assertEqual(tools.float_compare(leads[3].automated_probability, 4.21, 2), 0)
self.assertEqual(tools.float_compare(leads[8].automated_probability, 0.23, 2), 0)
+
+ def test_settings_pls_start_date(self):
+ # We test here that settings never crash due to ill-configured config param 'crm.pls_start_date'
+ set_param = self.env['ir.config_parameter'].sudo().set_param
+ str_date_8_days_ago = fields.Date.to_string(fields.Date.today() - timedelta(days=8))
+ resConfig = self.env['res.config.settings']
+
+ set_param("crm.pls_start_date", "2021-10-10")
+ res_config_new = resConfig.new()
+ self.assertEqual(fields.Date.to_string(res_config_new.predictive_lead_scoring_start_date),
+ "2021-10-10", "If config param is a valid date, date in settings should match with config param")
+
+ set_param("crm.pls_start_date", "")
+ res_config_new = resConfig.new()
+ self.assertEqual(fields.Date.to_string(res_config_new.predictive_lead_scoring_start_date),
+ str_date_8_days_ago, "If config param is empty, date in settings should be set to 8 days before today")
+
+ set_param("crm.pls_start_date", "One does not simply walk into system parameters to corrupt them")
+ res_config_new = resConfig.new()
+ self.assertEqual(fields.Date.to_string(res_config_new.predictive_lead_scoring_start_date),
+ str_date_8_days_ago, "If config param is not a valid date, date in settings should be set to 8 days before today")