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.

118 lines
3.9 KiB

[9.0][MIG][mass_mailing_custom_unsubscribe] Migrate. - Imported last updates from v8. - Adapted to v9. - Added a saner default to `mass_mailing.salt` configuration parameter by reusing `database.secret` if available, hoping that some day https://github.com/odoo/odoo/pull/12040 gets merged. - Updated README. - Increase security, drop backwards compatibility. Security got improved upstream, which would again break compatibility among current addon and future master upstream. I choose to break it now and keep it secured future-wise, so I drop the backwards compatibility features. - Includes tour tests. - Removes outdated tests. - Extends the mailing list management form when unsubscriber is a contact. - Adds a reason form even if he is not. - Avoids all methods that were not model-agnostic. [FIX][mass_mailing_custom_unsubscribe] Reasons noupdate After this fix, when you update the addon, you will not lose your customized reasons. [FIX] Compatibilize with mass_mailing_partner Current test code was based on the assumption that the `@api.model` decorator on `create()` ensured an empty recordset when running the method, but that's not true. This was causing an incompatibility betwee these tests and the `mass_mailing_partner` addon, which works assuming 0-1 recordsets. Now records are created from an empty recordset, and thus tests work everywhere. Update instructions If the user does not add the unsubscribe snippet, nothing will happen, so it's added to README to avoid confusion when testing/using the addon. [FIX] Use the right operator to preserve recordsets order Using `|=` sorts records at will each time (treating them as Python's `set`). Using `+=` always appends a record to the end of the set. Since we are using the record position in the set, this caused the test to work sometimes and fail other times. Now it works always.
8 years ago
[9.0][MIG][mass_mailing_custom_unsubscribe] Migrate. - Imported last updates from v8. - Adapted to v9. - Added a saner default to `mass_mailing.salt` configuration parameter by reusing `database.secret` if available, hoping that some day https://github.com/odoo/odoo/pull/12040 gets merged. - Updated README. - Increase security, drop backwards compatibility. Security got improved upstream, which would again break compatibility among current addon and future master upstream. I choose to break it now and keep it secured future-wise, so I drop the backwards compatibility features. - Includes tour tests. - Removes outdated tests. - Extends the mailing list management form when unsubscriber is a contact. - Adds a reason form even if he is not. - Avoids all methods that were not model-agnostic. [FIX][mass_mailing_custom_unsubscribe] Reasons noupdate After this fix, when you update the addon, you will not lose your customized reasons. [FIX] Compatibilize with mass_mailing_partner Current test code was based on the assumption that the `@api.model` decorator on `create()` ensured an empty recordset when running the method, but that's not true. This was causing an incompatibility betwee these tests and the `mass_mailing_partner` addon, which works assuming 0-1 recordsets. Now records are created from an empty recordset, and thus tests work everywhere. Update instructions If the user does not add the unsubscribe snippet, nothing will happen, so it's added to README to avoid confusion when testing/using the addon. [FIX] Use the right operator to preserve recordsets order Using `|=` sorts records at will each time (treating them as Python's `set`). Using `+=` always appends a record to the end of the set. Since we are using the record position in the set, this caused the test to work sometimes and fail other times. Now it works always.
8 years ago
[9.0][MIG][mass_mailing_custom_unsubscribe] Migrate. - Imported last updates from v8. - Adapted to v9. - Added a saner default to `mass_mailing.salt` configuration parameter by reusing `database.secret` if available, hoping that some day https://github.com/odoo/odoo/pull/12040 gets merged. - Updated README. - Increase security, drop backwards compatibility. Security got improved upstream, which would again break compatibility among current addon and future master upstream. I choose to break it now and keep it secured future-wise, so I drop the backwards compatibility features. - Includes tour tests. - Removes outdated tests. - Extends the mailing list management form when unsubscriber is a contact. - Adds a reason form even if he is not. - Avoids all methods that were not model-agnostic. [FIX][mass_mailing_custom_unsubscribe] Reasons noupdate After this fix, when you update the addon, you will not lose your customized reasons. [FIX] Compatibilize with mass_mailing_partner Current test code was based on the assumption that the `@api.model` decorator on `create()` ensured an empty recordset when running the method, but that's not true. This was causing an incompatibility betwee these tests and the `mass_mailing_partner` addon, which works assuming 0-1 recordsets. Now records are created from an empty recordset, and thus tests work everywhere. Update instructions If the user does not add the unsubscribe snippet, nothing will happen, so it's added to README to avoid confusion when testing/using the addon. [FIX] Use the right operator to preserve recordsets order Using `|=` sorts records at will each time (treating them as Python's `set`). Using `+=` always appends a record to the end of the set. Since we are using the record position in the set, this caused the test to work sometimes and fail other times. Now it works always.
8 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 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. _order = "date DESC"
  11. date = fields.Datetime(
  12. default=lambda self: self._default_date(),
  13. required=True)
  14. email = fields.Char(
  15. required=True)
  16. action = fields.Selection(
  17. selection=[
  18. ("subscription", "Subscription"),
  19. ("unsubscription", "Unsubscription"),
  20. ],
  21. required=True,
  22. default="unsubscription",
  23. help="What did the (un)subscriber choose to do.",
  24. )
  25. mass_mailing_id = fields.Many2one(
  26. "mail.mass_mailing",
  27. "Mass mailing",
  28. required=True,
  29. help="Mass mailing from which he was unsubscribed.")
  30. unsubscriber_id = fields.Reference(
  31. lambda self: self._selection_unsubscriber_id(),
  32. "(Un)subscriber",
  33. help="Who was subscribed or unsubscribed.")
  34. mailing_list_id = fields.Many2one(
  35. "mail.mass_mailing.list",
  36. "Mailing list",
  37. ondelete="set null",
  38. compute="_compute_mailing_list_id",
  39. store=True,
  40. help="(Un)subscribed mass mailing list, if any.",
  41. )
  42. reason_id = fields.Many2one(
  43. "mail.unsubscription.reason",
  44. "Reason",
  45. ondelete="restrict",
  46. help="Why the unsubscription was made.")
  47. details = fields.Char(
  48. help="More details on why the unsubscription was made.")
  49. details_required = fields.Boolean(
  50. related="reason_id.details_required")
  51. metadata = fields.Text(
  52. readonly=True,
  53. help="HTTP request metadata used when creating this record.",
  54. )
  55. @api.model
  56. def _default_date(self):
  57. return fields.Datetime.now()
  58. @api.model
  59. def _selection_unsubscriber_id(self):
  60. """Models that can be linked to a ``mail.mass_mailing``."""
  61. return self.env["mail.mass_mailing"]._get_mailing_model()
  62. @api.multi
  63. @api.constrains("action", "reason_id")
  64. def _check_reason_needed(self):
  65. """Ensure reason is given for unsubscriptions."""
  66. for one in self:
  67. if one.action == "unsubscription" and not one.reason_id:
  68. raise exceptions.ReasonRequiredError(
  69. _("Please indicate why are you unsubscribing."))
  70. @api.multi
  71. @api.constrains("details", "reason_id")
  72. def _check_details_needed(self):
  73. """Ensure details are given if required."""
  74. for one in self:
  75. if not one.details and one.details_required:
  76. raise exceptions.DetailsRequiredError(
  77. _("Please provide details on why you are unsubscribing."))
  78. @api.multi
  79. @api.depends("unsubscriber_id")
  80. def _compute_mailing_list_id(self):
  81. """Get the mass mailing list, if it is possible."""
  82. for one in self:
  83. try:
  84. one.mailing_list_id = one.unsubscriber_id.list_id
  85. except AttributeError:
  86. # Possibly model != mail.mass_mailing.contact; no problem
  87. pass
  88. @api.model
  89. def create(self, vals):
  90. # No reasons for subscriptions
  91. if vals.get("action") == "subscription":
  92. vals = dict(vals, reason_id=False, details=False)
  93. return super(MailUnsubscription, self).create(vals)
  94. class MailUnsubscriptionReason(models.Model):
  95. _name = "mail.unsubscription.reason"
  96. _order = "sequence, name"
  97. name = fields.Char(
  98. index=True,
  99. translate=True,
  100. required=True)
  101. details_required = fields.Boolean(
  102. help="Check to ask for more details when this reason is selected.")
  103. sequence = fields.Integer(
  104. index=True,
  105. help="Position of the reason in the list.")