| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import ast |
| 5 |
|
| 6 |
from odoo import api, fields, models, _ |
| 7 |
from odoo.exceptions import ValidationError |
| 8 |
|
| 9 |
|
| 10 |
class SurveySurvey(models.Model): |
| 11 |
_inherit = 'survey.survey' |
| 12 |
|
| 13 |
slide_ids = fields.One2many( |
| 14 |
'slide.slide', 'survey_id', string="Certification Slides", |
| 15 |
help="The slides this survey is linked to through the e-learning application") |
| 16 |
slide_channel_ids = fields.One2many( |
| 17 |
'slide.channel', string="Certification Courses", compute='_compute_slide_channel_data', |
| 18 |
help="The courses this survey is linked to through the e-learning application", |
| 19 |
groups='website_slides.group_website_slides_officer') |
| 20 |
slide_channel_count = fields.Integer("Courses Count", compute='_compute_slide_channel_data', groups='website_slides.group_website_slides_officer') |
| 21 |
|
| 22 |
@api.depends('slide_ids.channel_id') |
| 23 |
def _compute_slide_channel_data(self): |
| 24 |
for survey in self: |
| 25 |
survey.slide_channel_ids = survey.slide_ids.mapped('channel_id') |
| 26 |
survey.slide_channel_count = len(survey.slide_channel_ids) |
| 27 |
|
| 28 |
@api.ondelete(at_uninstall=False) |
| 29 |
def _unlink_except_linked_to_course(self): |
| 30 |
|
| 31 |
|
| 32 |
certifications = self.sudo().slide_ids.filtered(lambda slide: slide.slide_type == "certification").mapped('survey_id').exists() |
| 33 |
if certifications: |
| 34 |
certifications_course_mapping = [ |
| 35 |
self.env._( |
| 36 |
"- %(certification)s (Courses - %(courses)s)", |
| 37 |
certification=certi.title, |
| 38 |
courses=certi.slide_channel_ids.mapped("name"), |
| 39 |
) |
| 40 |
for certi in certifications |
| 41 |
] |
| 42 |
raise ValidationError(_( |
| 43 |
'Uh-oh! You can’t delete surveys used as a Course Certification! Otherwise, students might think diplomas just grow on trees.\n' |
| 44 |
'The courses that need them are:\n%s', |
| 45 |
'\n'.join(certifications_course_mapping))) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
def action_survey_view_slide_channels(self): |
| 52 |
""" Redirect to the channels using the survey as a certification. Open |
| 53 |
in no-create as link between those two comes through a slide, hard to |
| 54 |
keep as default values. """ |
| 55 |
action = self.env["ir.actions.actions"]._for_xml_id("website_slides.slide_channel_action_overview") |
| 56 |
action['display_name'] = _("Courses") |
| 57 |
if self.slide_channel_count == 1: |
| 58 |
action.update({'views': [(False, 'form')], |
| 59 |
'res_id': self.slide_channel_ids[0].id}) |
| 60 |
else: |
| 61 |
action.update({'views': [[False, 'list'], [False, 'form']], |
| 62 |
'domain': [('id', 'in', self.slide_channel_ids.ids)]}) |
| 63 |
action['context'] = dict( |
| 64 |
ast.literal_eval(action.get('context') or '{}'), |
| 65 |
create=False |
| 66 |
) |
| 67 |
return action |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
def _prepare_challenge_category(self): |
| 74 |
slide_survey = self.env['slide.slide'].search([('survey_id', '=', self.id)]) |
| 75 |
return 'slides' if slide_survey else 'certification' |