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())