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.

74 lines
2.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp import _, api, fields, models
  5. from .. import exceptions
  6. class MailUnsubscription(models.Model):
  7. _name = "mail.unsubscription"
  8. _inherit = "mail.thread"
  9. _rec_name = "date"
  10. date = fields.Datetime(
  11. default=lambda self: self._default_date(),
  12. required=True)
  13. email = fields.Char(
  14. required=True)
  15. mass_mailing_id = fields.Many2one(
  16. "mail.mass_mailing",
  17. "Mass mailing",
  18. required=True,
  19. help="Mass mailing from which he was unsubscribed.")
  20. unsubscriber_id = fields.Reference(
  21. lambda self: self._selection_unsubscriber_id(),
  22. "Unsubscriber",
  23. required=True,
  24. help="Who was unsubscribed.")
  25. reason_id = fields.Many2one(
  26. "mail.unsubscription.reason",
  27. "Reason",
  28. ondelete="restrict",
  29. required=True,
  30. help="Why the unsubscription was made.")
  31. details = fields.Char(
  32. help="More details on why the unsubscription was made.")
  33. details_required = fields.Boolean(
  34. related="reason_id.details_required")
  35. success = fields.Boolean(
  36. help="If this is unchecked, it indicates some failure happened in the "
  37. "unsubscription process.")
  38. @api.model
  39. def _default_date(self):
  40. return fields.Datetime.now()
  41. @api.model
  42. def _selection_unsubscriber_id(self):
  43. """Models that can be linked to a ``mail.mass_mailing``."""
  44. return self.env["mail.mass_mailing"]._get_mailing_model()
  45. @api.multi
  46. @api.constrains("details", "reason_id")
  47. def _check_details_needed(self):
  48. """Ensure details are given if required."""
  49. for s in self:
  50. if not s.details and s.details_required:
  51. raise exceptions.DetailsRequiredError(
  52. _("This reason requires an explanation."))
  53. class MailUnsubscriptionReason(models.Model):
  54. _name = "mail.unsubscription.reason"
  55. _order = "sequence, name"
  56. name = fields.Char(
  57. index=True,
  58. translate=True,
  59. required=True)
  60. details_required = fields.Boolean(
  61. help="Check to ask for more details when this reason is selected.")
  62. sequence = fields.Integer(
  63. index=True,
  64. help="Position of the reason in the list.")