[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
This commit is contained in:
Tiffany Chang (tic)
2020-01-29 14:32:24 +00:00
committed by Thibault Delavallée
parent 3028e9a864
commit f54feba5b6
+10 -7
View File
@@ -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'))