Odoo modules related to surveys
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.9 KiB

  1. from odoo import models, fields, api, _
  2. from odoo.exceptions import UserError
  3. class SurveySurvey(models.Model):
  4. _inherit = 'survey.survey'
  5. is_template = fields.Boolean(string="Is a template", default=False, copy=False)
  6. @api.multi
  7. def copy_data(self, default=None):
  8. data = super(SurveySurvey, self).copy_data(default)[0]
  9. title = isinstance(default, dict) and default.get('title', False) or False
  10. if title:
  11. data['title'] = title
  12. elif _(" (Template)") in data['title']:
  13. data['title'] = data['title'].replace(_(" (Template)"), "")
  14. return [data]
  15. # ACTIONS
  16. @api.multi
  17. def create_template_from_survey(self, default={}):
  18. self.ensure_one()
  19. if self.is_template:
  20. raise UserError(_("You should use the \"Copy\" secondary action to duplicate a survey template."))
  21. default.update({'is_template': True})
  22. return self.copy(default=default)
  23. @api.multi
  24. def create_survey_from_template(self, default={}):
  25. self.ensure_one()
  26. if not self.is_template:
  27. raise UserError(_("You should use the \"Copy\" secondary action to duplicate a non-template survey."))
  28. return self.copy(default=default)
  29. @api.multi
  30. def action_send_survey(self):
  31. self.ensure_one()
  32. if self.is_template:
  33. raise UserError(_("You cannot send a template survey, create a new survey from this template and you'll be able to share it."))
  34. return super(SurveySurvey, self).action_send_survey()
  35. # ONCHANGES
  36. @api.onchange('is_template')
  37. def onchange_is_template(self):
  38. if self.title:
  39. if self.is_template:
  40. if _("(Template)") not in self.title:
  41. self.title = self.title.strip() + _(" (Template)")
  42. else:
  43. if _(" (Template)") in self.title:
  44. self.title = self.title.replace(_(" (Template)"), "")