[IMP] website_event_questions: update the constraint validation message

- If we try to delete an answer which is already been selected by attendees,
  it throws a validation error saying "There must be suggested value or
  text value". The constraint here does important check, which makes sure that
  the answer must have a valid value (either selection or text) otherwise the
  answer record is of no use, and keeping such records doesn't make sense.
  Thus it prevents corrupting the historic responses. For example, if attendees
  answered a selection question with particular option and we try to delete the
  same option from event question, the constraint will not let us delete that,
  because the answers with this option selected will be of no use.

  However, the current validation error message does not make it clear to the
  user trying to delete the selection options which are present in the answers.

- The same goes for event questions. If we try to delete a question which is
  already been answered by attendees, it throws a raw sql error message to
  prevent corrupting the historic responses.

With this commit, we simply improve the validation error messages for both the
above cases. While deleting the answer selection, we now raise a UserError from
`_unlink_except_selected_answer` method saying "You cannot delete an answer
that has already been selected by attendees." And while deleting the question
itself, we now raise a UserError from `_unlink_except_answered_question` method
saying "You cannot delete a question that has already been answered by attendees."

TaskID-2635599

closes odoo/odoo#76265

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
ijas ahammed
2021-10-04 14:12:06 +00:00
parent f312e1359a
commit ec7f7405e7
@@ -40,6 +40,11 @@ class EventQuestion(models.Model):
raise UserError(_("You cannot change the question type of a question that already has answers!"))
return super(EventQuestion, self).write(vals)
@api.ondelete(at_uninstall=False)
def _unlink_except_answered_question(self):
if self.env['event.registration.answer'].search_count([('question_id', 'in', self.ids)]):
raise UserError(_('You cannot delete a question that has already been answered by attendees.'))
def action_view_question_answers(self):
""" Allow analyzing the attendees answers to event questions in a convenient way:
- A graph view showing counts of each suggestions for simple_choice questions
@@ -63,3 +68,8 @@ class EventQuestionAnswer(models.Model):
name = fields.Char('Answer', required=True, translate=True)
question_id = fields.Many2one('event.question', required=True, ondelete='cascade')
sequence = fields.Integer(default=10)
@api.ondelete(at_uninstall=False)
def _unlink_except_selected_answer(self):
if self.env['event.registration.answer'].search_count([('value_answer_id', 'in', self.ids)]):
raise UserError(_('You cannot delete an answer that has already been selected by attendees.'))