|
|
from odoo import models, fields, api, _ from odoo.exceptions import UserError
class SurveySurvey(models.Model): _inherit = 'survey.survey'
is_template = fields.Boolean(string="Is a template", default=False, copy=False)
@api.multi def copy_data(self, default=None): data = super(SurveySurvey, self).copy_data(default)[0] title = isinstance(default, dict) and default.get('title', False) or False if title: data['title'] = title elif _(" (Template)") in data['title']: data['title'] = data['title'].replace(_(" (Template)"), "") return [data]
# ACTIONS
@api.multi def create_template_from_survey(self, default={}): self.ensure_one() if self.is_template: raise UserError(_("You should use the \"Copy\" secondary action to duplicate a survey template.")) default.update({'is_template': True}) new_survey = self.copy(default=default) return { 'type': 'ir.actions.act_window', 'res_model': 'survey.survey', 'view_type': 'form', 'view_mode': 'form', 'target': 'current', 'res_id': new_survey.id, }
@api.multi def create_survey_from_template(self, default={}): self.ensure_one() if not self.is_template: raise UserError(_("You should use the \"Copy\" secondary action to duplicate a non-template survey.")) new_survey = self.copy(default=default) return { 'type': 'ir.actions.act_window', 'res_model': 'survey.survey', 'view_type': 'form', 'view_mode': 'form', 'target': 'current', 'res_id': new_survey.id, }
@api.multi def action_send_survey(self): self.ensure_one() if self.is_template: raise UserError(_("You cannot send a template survey, create a new survey from this template and you'll be able to share it.")) return super(SurveySurvey, self).action_send_survey()
# ONCHANGES
@api.onchange('is_template') def onchange_is_template(self): if self.title: if self.is_template: if _("(Template)") not in self.title: self.title = self.title.strip() + _(" (Template)") else: if _(" (Template)") in self.title: self.title = self.title.replace(_(" (Template)"), "")
|