From c9bed78c2bc674d43a2c924d91fd379565927410 Mon Sep 17 00:00:00 2001 From: "Florimond Husquinet (fhu)" Date: Fri, 18 Oct 2019 07:45:39 +0000 Subject: [PATCH] [FIX] crm: compute lead probability on kanban quick create -In CRM, click the CREATE button. -Only enter a title for the new lead and directly click ADD. -Click on the newly created lead to view its details. Before this commit: although a probability could be computed based on the stage of the lead, this field has a default value and, at creation time, this value doesn't end up in the list of values from which the `_write_probability` function decide whether to recompute the probability. After this commit: The `_write_probability` function, renamed `_update_probability`, doesn't check whether it should or not update the probability. It just does it. This function is directly called in the `create`. `_should_update_probability` is called before `_update_probability` in the `write`. Task ID : 2081480 closes odoo/odoo#40411 X-original-commit: 483a94116629e418ebb224bad375747cf9ab3c1d Signed-off-by: Thibault Delavallee (tde) --- addons/crm/models/crm_lead.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 886f6b97cf2..3cdd10c9732 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -401,7 +401,7 @@ class Lead(models.Model): result = super(Lead, self.with_context(context)).create(vals) # Compute new probability for each lead separately - result._write_probability(vals) + result._update_probability() return result def write(self, vals): @@ -424,20 +424,12 @@ class Lead(models.Model): write_result = super(Lead, self).write(vals) # Compute new automated_probability (and, eventually, probability) for each lead separately - self._write_probability(vals) + if self._should_update_probability(vals): + self._update_probability() return write_result - def _write_probability(self, vals): - compute = False - fields_to_check = ['tag_ids', 'stage_id', 'team_id'] + self._pls_get_safe_fields() - for field, value in vals.items(): - if field in fields_to_check: - compute = True - break - - if not compute: - return + def _update_probability(self): lead_probabilities = self._pls_get_naive_bayes_probabilities() for lead in self: if lead.id in lead_probabilities: @@ -448,6 +440,13 @@ class Lead(models.Model): super(Lead, lead).write(proba_vals) return + def _should_update_probability(self, vals): + fields_to_check = ['tag_ids', 'stage_id', 'team_id'] + self._pls_get_safe_fields() + for field, value in vals.items(): + if field in fields_to_check: + return True + return False + @api.returns('self', lambda value: value.id) def copy(self, default=None): self.ensure_one()