From 8b1daf9b80ac8fca16193f6cee4fa7fd390dfad2 Mon Sep 17 00:00:00 2001 From: Pratik Raval Date: Mon, 4 Jul 2022 10:33:16 +0000 Subject: [PATCH] [FIX] website_slides_survey: prevent crash on survey deletion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, when we delete a single certification that is linked to a course, it gives a validation error, which is good. But when we try to delete multiple certifications that are linked with courses, it throws an error. This commit fixes the issue by properly handling multiple records in the deletion check, and updates the ValidationError message accordingly. taskID-2901629 closes odoo/odoo#97391 X-original-commit: fecc1babb63ad1164189c95bb019afb2469043bb Signed-off-by: Warnon Aurélien (awa) --- .../i18n/website_slides_survey.pot | 10 ++++++++-- .../models/survey_survey.py | 13 ++++++++----- .../tests/test_course_certification_unlink.py | 19 ++++++++++++++----- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/addons/website_slides_survey/i18n/website_slides_survey.pot b/addons/website_slides_survey/i18n/website_slides_survey.pot index 9742d7370f0..794b9dd4e1c 100644 --- a/addons/website_slides_survey/i18n/website_slides_survey.pot +++ b/addons/website_slides_survey/i18n/website_slides_survey.pot @@ -15,6 +15,12 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" +#. module: website_slides_survey +#: code:addons/website_slides_survey/models/survey_survey.py:0 +#, python-format +msgid "- %s (Courses - %s)" +msgstr "" + #. module: website_slides_survey #: model_terms:ir.ui.view,arch_db:website_slides_survey.survey_survey_view_kanban msgid "" @@ -412,8 +418,8 @@ msgstr "" #: code:addons/website_slides_survey/models/survey_survey.py:0 #, python-format msgid "" -"The Certification '%(certification_name)s' cannot be deleted because it is " -"still being used by the Course(s) '%(courses_names)s'" +"Any Survey listed below is currently used as a Course Certification and cannot be deleted:\n" +"%s" msgstr "" #. module: website_slides_survey diff --git a/addons/website_slides_survey/models/survey_survey.py b/addons/website_slides_survey/models/survey_survey.py index 4024afd0716..e3800c5d94e 100644 --- a/addons/website_slides_survey/models/survey_survey.py +++ b/addons/website_slides_survey/models/survey_survey.py @@ -28,11 +28,14 @@ class Survey(models.Model): @api.ondelete(at_uninstall=False) def _unlink_except_linked_to_course(self): - slides = self.slide_ids.filtered(lambda slide: slide.slide_type == "certification").exists() - if slides: - raise ValidationError(_("The Certification '%(certification_name)s' cannot be deleted because it is still being used by the Course(s) '%(courses_names)s'", - certification_name=self.title, - courses_names=', '.join(slides.mapped('channel_id.name')))) + # we consider it's ok to show certification names for people trying to delete courses + # even if they don't have access to those surveys hence the sudo usage + certifications = self.sudo().slide_ids.filtered(lambda slide: slide.slide_type == "certification").mapped('survey_id').exists() + if certifications: + certifications_course_mapping = [_('- %s (Courses - %s)', certi.title, '; '.join(certi.slide_channel_ids.mapped('name'))) for certi in certifications] + raise ValidationError(_( + 'Any Survey listed below is currently used as a Course Certification and cannot be deleted:\n%s', + '\n'.join(certifications_course_mapping))) # --------------------------------------------------------- # Actions diff --git a/addons/website_slides_survey/tests/test_course_certification_unlink.py b/addons/website_slides_survey/tests/test_course_certification_unlink.py index 94f08a4e6ea..bfaaf8ed864 100644 --- a/addons/website_slides_survey/tests/test_course_certification_unlink.py +++ b/addons/website_slides_survey/tests/test_course_certification_unlink.py @@ -16,17 +16,26 @@ class TestSurvey(SlidesCase): @users('user_manager') def test_unlink(self): - self.certification = self.env['slide.slide'].create({ + [certification, _dummy] = self.env['slide.slide'].create([{ 'name': 'Certification', 'slide_type': 'certification', 'channel_id': self.channel.id, 'survey_id': self.survey.id, - }) + }, { + 'name': 'Second Certification', + 'slide_type': 'certification', + 'channel_id': self.channel.id, + 'survey_id': self.survey2.id, + }]) - with self.assertRaises(ValidationError, msg="The error is unexpected"): - self.survey.unlink() + with self.assertRaises( + ValidationError, + msg="Should raise when trying to unlink a survey linked to courses" + ): + (self.survey | self.survey2).unlink() self.assertTrue(self.survey.exists()) - self.certification.survey_id = self.survey2 + self.assertTrue(self.survey2.exists()) + certification.survey_id = self.survey2 self.survey.unlink() self.assertFalse(self.survey.exists())