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.

135 lines
5.3 KiB

  1. # Copyright 2018 Tecnativa - Jairo Llopis
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import ValidationError
  5. from odoo.tools.safe_eval import safe_eval
  6. class PrivacyActivity(models.Model):
  7. _inherit = "privacy.activity"
  8. server_action_id = fields.Many2one(
  9. "ir.actions.server",
  10. "Server action",
  11. domain=[("model_id.model", "=", "privacy.consent")],
  12. help="Run this action when a new consent request is created or its "
  13. "acceptance status is updated.",
  14. )
  15. consent_ids = fields.One2many("privacy.consent", "activity_id", "Consents",)
  16. consent_count = fields.Integer("Consents count", compute="_compute_consent_count",)
  17. consent_required = fields.Selection(
  18. [("auto", "Automatically"), ("manual", "Manually")],
  19. "Ask subjects for consent",
  20. help="Enable if you need to track any kind of consent "
  21. "from the affected subjects",
  22. )
  23. consent_template_id = fields.Many2one(
  24. "mail.template",
  25. "Email template",
  26. default=lambda self: self._default_consent_template_id(),
  27. domain=[("model", "=", "privacy.consent")],
  28. help="Email to be sent to subjects to ask for consent. "
  29. "A good template should include details about the current "
  30. "consent request status, how to change it, and where to "
  31. "get more information.",
  32. )
  33. default_consent = fields.Boolean(
  34. "Accepted by default",
  35. help="Should we assume the subject has accepted if we receive no " "response?",
  36. )
  37. # Hidden helpers help user design new templates
  38. consent_template_default_body_html = fields.Text(
  39. compute="_compute_consent_template_defaults",
  40. )
  41. consent_template_default_subject = fields.Char(
  42. compute="_compute_consent_template_defaults",
  43. )
  44. @api.model
  45. def _default_consent_template_id(self):
  46. return self.env.ref("privacy_consent.template_consent", False)
  47. @api.depends("consent_ids")
  48. def _compute_consent_count(self):
  49. groups = self.env["privacy.consent"].read_group(
  50. [("activity_id", "in", self.ids)], ["activity_id"], ["activity_id"],
  51. )
  52. for group in groups:
  53. self.browse(group["activity_id"][0], self._prefetch).consent_count = group[
  54. "activity_id_count"
  55. ]
  56. def _compute_consent_template_defaults(self):
  57. """Used in context values, to help users design new templates."""
  58. template = self._default_consent_template_id()
  59. if template:
  60. self.update(
  61. {
  62. "consent_template_default_body_html": template.body_html,
  63. "consent_template_default_subject": template.subject,
  64. }
  65. )
  66. @api.constrains("consent_required", "consent_template_id")
  67. def _check_auto_consent_has_template(self):
  68. """Require a mail template to automate consent requests."""
  69. for one in self:
  70. if one.consent_required == "auto" and not one.consent_template_id:
  71. raise ValidationError(
  72. _("Specify a mail template to ask automated consent.")
  73. )
  74. @api.constrains("consent_required", "subject_find")
  75. def _check_consent_required_subject_find(self):
  76. for one in self:
  77. if one.consent_required and not one.subject_find:
  78. raise ValidationError(
  79. _(
  80. "Require consent is available only for subjects "
  81. "in current database."
  82. )
  83. )
  84. @api.model
  85. def _cron_new_consents(self):
  86. """Ask all missing automatic consent requests."""
  87. automatic = self.search([("consent_required", "=", "auto")])
  88. automatic.action_new_consents()
  89. @api.onchange("consent_required")
  90. def _onchange_consent_required_subject_find(self):
  91. """Find subjects automatically if we require their consent."""
  92. if self.consent_required:
  93. self.subject_find = True
  94. def action_new_consents(self):
  95. """Generate new consent requests."""
  96. consents_vals = []
  97. # Skip activitys where consent is not required
  98. for one in self.with_context(active_test=False).filtered("consent_required"):
  99. domain = [
  100. ("id", "not in", one.mapped("consent_ids.partner_id").ids),
  101. ("email", "!=", False),
  102. ] + safe_eval(one.subject_domain)
  103. # Store values for creating missing consent requests
  104. for missing in self.env["res.partner"].search(domain):
  105. consents_vals.append(
  106. {
  107. "partner_id": missing.id,
  108. "accepted": one.default_consent,
  109. "activity_id": one.id,
  110. }
  111. )
  112. # Create and send consent request emails for automatic activitys
  113. consents = self.env["privacy.consent"].create(consents_vals)
  114. consents.action_auto_ask()
  115. # Redirect user to new consent requests generated
  116. return {
  117. "domain": [("id", "in", consents.ids)],
  118. "name": _("Generated consents"),
  119. "res_model": consents._name,
  120. "type": "ir.actions.act_window",
  121. "view_mode": "tree,form",
  122. }