From f54feba5b62d5a3f2e003beb95f071ff31176cd0 Mon Sep 17 00:00:00 2001 From: "Tiffany Chang (tic)" Date: Wed, 8 Jan 2020 13:31:05 +0000 Subject: [PATCH] [FIX] survey: allow 0 as numerical_box user answer even if required Because 0 evaluates as False in "if" statements, generalized answer_type constraint check logic would not allow user to submit a survey with 0 as a numerical value. This commit fixes this issue by explicitly allowing this case. Triggered by task ID 2061937 (improve scoring in surveys) PR #44205 --- addons/survey/models/survey_user.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/addons/survey/models/survey_user.py b/addons/survey/models/survey_user.py index 56b4ad2d5df..5ac8db82cf2 100644 --- a/addons/survey/models/survey_user.py +++ b/addons/survey/models/survey_user.py @@ -8,6 +8,7 @@ from dateutil.relativedelta import relativedelta from odoo import api, fields, models, _ from odoo.exceptions import ValidationError +from odoo.tools import float_is_zero _logger = logging.getLogger(__name__) @@ -361,19 +362,21 @@ class SurveyUserInputLine(models.Model): answer.answer_is_correct = False @api.constrains('skipped', 'answer_type') - def _check_answered_or_skipped(self): - if any(line.skipped == bool(line.answer_type) for line in self): - raise ValidationError(_('A question is either skipped, either answered. Not both.')) - - @api.constrains('answer_type') - def _check_answer_type(self): + def _check_answer_type_skipped(self): for line in self: + if (line.skipped == bool(line.answer_type)): + raise ValidationError(_('A question is either skipped, either answered. Not both.')) + + # allow 0 for numerical box + if line.answer_type == 'numerical_box' and float_is_zero(line['value_numerical_box'], precision_digits=6): + continue if line.answer_type == 'suggestion': field_name = 'suggested_answer_id' elif line.answer_type: field_name = 'value_%s' % line.answer_type - else: + else: # skipped field_name = False + if field_name and not line[field_name]: raise ValidationError(_('The answer must be in the right type'))