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.

86 lines
3.0 KiB

2 years ago
2 years ago
2 years ago
2 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. @api.model
  6. def cron_close_deadline_survey(self):
  7. deadline = self.search(
  8. [
  9. ("date_deadline", "!=", False),
  10. ("date_deadline", "<", fields.Date.today()),
  11. ]
  12. )
  13. to_close = deadline.filtered("auto_close")
  14. if to_close:
  15. to_close.action_close_survey()
  16. for survey in deadline - to_close:
  17. survey.message_post(
  18. subtype="survey_deadline_autoclose.mail_message_subtype_survey_deadline",
  19. body=_("This survey has expired."),
  20. )
  21. date_deadline = fields.Date(
  22. string="Deadline", copy=False, track_visibility="onchange"
  23. )
  24. auto_close = fields.Boolean(
  25. string="Auto close",
  26. default=False,
  27. help="If checked, the survey will be automatically closed when deadline is overpassed.",
  28. track_visibility="onchange",
  29. )
  30. # ACTIONS
  31. @api.multi
  32. def action_send_survey(self):
  33. self.ensure_one()
  34. action = super(SurveySurvey, self).action_send_survey()
  35. action["context"].update(
  36. {
  37. "default_date_deadline": self.date_deadline,
  38. }
  39. )
  40. return action
  41. @api.multi
  42. def action_close_survey(self):
  43. stage = self.env["survey.stage"].search([("closed", "=", True)], limit=1)
  44. if not stage:
  45. if self.env.context.get("cron", False):
  46. for survey in self:
  47. survey.message_post(
  48. subtype="survey_deadline_autoclose.mail_message_subtype_survey_closed",
  49. subject=_("Survey closing impossible"),
  50. body=_(
  51. 'Survey should have been automatically closed but no "closed" '
  52. "stage was found, the requested operation was impossible to proceed.\n"
  53. 'To fix this situation, you have to check "Closed" at least on one survey stage.'
  54. ),
  55. )
  56. else:
  57. raise UserError(
  58. _(
  59. 'No "closed" stage found, the requested operation is impossible.\n'
  60. 'To fix this situation, you have to check "Closed" at least on one survey stage.'
  61. )
  62. )
  63. else:
  64. self.write({"stage_id": stage.id})
  65. for survey in self:
  66. survey.message_post(
  67. subtype="survey_deadline_autoclose.mail_message_subtype_survey_closed",
  68. body=_("This survey was automatically closed."),
  69. )
  70. # ONCHANGES
  71. @api.onchange("date_deadline")
  72. def onchange_date_deadline(self):
  73. self.ensure_one()
  74. if not self.date_deadline:
  75. self.auto_close = False
  76. elif not self.auto_close:
  77. self.auto_close = True