From 46f46b6072111bdd9abcc40cdbd18ce137c5ea5b Mon Sep 17 00:00:00 2001 From: Saurabh Mishra Date: Fri, 28 Apr 2023 05:46:33 +0000 Subject: [PATCH] [FIX] survey: prevent question deletion when session in progress If we create a survey and start a new session. When the session state is in progress and If the survey creater deletes a question and participant tries to answer the deleted question and submits it then participant will face the issue of 'False is not in list'. This commit will raise an usererror if the survey creater tries to delete the question when the survey session is in progress. sentry-4113791861 closes odoo/odoo#124769 X-original-commit: f636025fce4edda8a5f89b7161c4e9481460f03f Signed-off-by: Thibault Delavallee (tde) Signed-off-by: Saurabh Mishra (sami) --- addons/survey/i18n/survey.pot | 8 ++++++++ addons/survey/models/survey_question.py | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/addons/survey/i18n/survey.pot b/addons/survey/i18n/survey.pot index 18f42051aff..818dbda0a9e 100644 --- a/addons/survey/i18n/survey.pot +++ b/addons/survey/i18n/survey.pot @@ -5158,6 +5158,14 @@ msgstr "" #. module: survey #. odoo-python +#: code:addons/survey/models/survey_question.py:0 +#, python-format +msgid "" +"You cannot delete questions from surveys \"%(survey_names)s\" while live " +"sessions are in progress." +msgstr "" + +#. module: survey #: code:addons/survey/models/survey_survey.py:0 #, python-format msgid "" diff --git a/addons/survey/models/survey_question.py b/addons/survey/models/survey_question.py index ba123ca20a4..3c07a249902 100644 --- a/addons/survey/models/survey_question.py +++ b/addons/survey/models/survey_question.py @@ -8,7 +8,7 @@ import itertools import operator from odoo import api, fields, models, tools, _ -from odoo.exceptions import ValidationError +from odoo.exceptions import UserError, ValidationError class SurveyQuestion(models.Model): @@ -407,6 +407,19 @@ class SurveyQuestion(models.Model): }) return super().unlink() + # ------------------------------------------------------------ + # CRUD + # ------------------------------------------------------------ + + @api.ondelete(at_uninstall=False) + def _unlink_except_live_sessions_in_progress(self): + running_surveys = self.survey_id.filtered(lambda survey: survey.session_state == 'in_progress') + if running_surveys: + raise UserError(_( + 'You cannot delete questions from surveys "%(survey_names)s" while live sessions are in progress.', + survey_names=', '.join(running_surveys.mapped('title')), + )) + # ------------------------------------------------------------ # VALIDATION # ------------------------------------------------------------