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.

70 lines
2.4 KiB

4 years ago
4 years ago
  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. new_survey = self.copy(default=default)
  23. return {
  24. 'type': 'ir.actions.act_window',
  25. 'res_model': 'survey.survey',
  26. 'view_type': 'form',
  27. 'view_mode': 'form',
  28. 'target': 'current',
  29. 'res_id': new_survey.id,
  30. }
  31. @api.multi
  32. def create_survey_from_template(self, default={}):
  33. self.ensure_one()
  34. if not self.is_template:
  35. raise UserError(_("You should use the \"Copy\" secondary action to duplicate a non-template survey."))
  36. new_survey = self.copy(default=default)
  37. return {
  38. 'type': 'ir.actions.act_window',
  39. 'res_model': 'survey.survey',
  40. 'view_type': 'form',
  41. 'view_mode': 'form',
  42. 'target': 'current',
  43. 'res_id': new_survey.id,
  44. }
  45. @api.multi
  46. def action_send_survey(self):
  47. self.ensure_one()
  48. if self.is_template:
  49. raise UserError(_("You cannot send a template survey, create a new survey from this template and you'll be able to share it."))
  50. return super(SurveySurvey, self).action_send_survey()
  51. # ONCHANGES
  52. @api.onchange('is_template')
  53. def onchange_is_template(self):
  54. if self.title:
  55. if self.is_template:
  56. if _("(Template)") not in self.title:
  57. self.title = self.title.strip() + _(" (Template)")
  58. else:
  59. if _(" (Template)") in self.title:
  60. self.title = self.title.replace(_(" (Template)"), "")