Skip to content
Snippets Groups Projects
Commit a4abdfa3 authored by Thibault Delavallée's avatar Thibault Delavallée
Browse files

[IMP] website_slides: merge slide quiz answers constraints in a single method

Currently there are several checks on answer integrity for slide quiz
questions. In this commit we simplify code to effectively implement
what is requested: 1 valid answer and at least 1 invalid answer.

Task ID 1999636
PR #35326
parent 0b5f1fbe
No related branches found
No related tags found
No related merge requests found
......@@ -20,26 +20,12 @@ class SlideQuestion(models.Model):
done_count = fields.Integer(compute="_compute_statistics", groups='website.group_website_publisher')
@api.constrains('answer_ids')
def _check_only_one_good_answer(self):
for question in self:
good_answer_count = 0
for answer in question.answer_ids:
if answer.is_correct:
good_answer_count += 1
if good_answer_count > 1:
raise ValidationError(_('Question "%s" can only have one good answer') % question.question)
@api.constrains('answer_ids')
def _check_correct_answer(self):
for question in self:
if not any([answer.is_correct for answer in question.answer_ids]):
raise ValidationError(_('Question "%s" must at least have one good answer') % question.question)
@api.constrains('answer_ids')
def _check_at_least_2_answers(self):
def _check_answers_integrity(self):
for question in self:
if len(question.answer_ids.filtered(lambda answer: answer.is_correct)) != 1:
raise ValidationError(_('Question "%s" must have 1 correct answer') % question.question)
if len(question.answer_ids) < 2:
raise ValidationError(_('Question "%s" has no valid answer, please set one') % question.question)
raise ValidationError(_('Question "%s" must have 1 correct answer and at least 1 invalid answer') % question.question)
@api.depends('slide_id')
def _compute_statistics(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment