[FIX] website_slides_survey: prevent crash on survey deletion

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) <awa@odoo.com>
This commit is contained in:
Pratik Raval
2022-08-03 15:10:14 +02:00
committed by Aurélien Warnon
parent 46cceb4806
commit 8b1daf9b80
3 changed files with 30 additions and 12 deletions
@@ -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
@@ -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
@@ -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())