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.

136 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. self.consent_count = 0
  50. groups = self.env["privacy.consent"].read_group(
  51. [("activity_id", "in", self.ids)], ["activity_id"], ["activity_id"],
  52. )
  53. for group in groups:
  54. self.browse(group["activity_id"][0]).consent_count = group[
  55. "activity_id_count"
  56. ]
  57. def _compute_consent_template_defaults(self):
  58. """Used in context values, to help users design new templates."""
  59. template = self._default_consent_template_id()
  60. if template:
  61. self.update(
  62. {
  63. "consent_template_default_body_html": template.body_html,
  64. "consent_template_default_subject": template.subject,
  65. }
  66. )
  67. @api.constrains("consent_required", "consent_template_id")
  68. def _check_auto_consent_has_template(self):
  69. """Require a mail template to automate consent requests."""
  70. for one in self:
  71. if one.consent_required == "auto" and not one.consent_template_id:
  72. raise ValidationError(
  73. _("Specify a mail template to ask automated consent.")
  74. )
  75. @api.constrains("consent_required", "subject_find")
  76. def _check_consent_required_subject_find(self):
  77. for one in self:
  78. if one.consent_required and not one.subject_find:
  79. raise ValidationError(
  80. _(
  81. "Require consent is available only for subjects "
  82. "in current database."
  83. )
  84. )
  85. @api.model
  86. def _cron_new_consents(self):
  87. """Ask all missing automatic consent requests."""
  88. automatic = self.search([("consent_required", "=", "auto")])
  89. automatic.action_new_consents()
  90. @api.onchange("consent_required")
  91. def _onchange_consent_required_subject_find(self):
  92. """Find subjects automatically if we require their consent."""
  93. if self.consent_required:
  94. self.subject_find = True
  95. def action_new_consents(self):
  96. """Generate new consent requests."""
  97. consents_vals = []
  98. # Skip activitys where consent is not required
  99. for one in self.with_context(active_test=False).filtered("consent_required"):
  100. domain = [
  101. ("id", "not in", one.mapped("consent_ids.partner_id").ids),
  102. ("email", "!=", False),
  103. ] + safe_eval(one.subject_domain)
  104. # Store values for creating missing consent requests
  105. for missing in self.env["res.partner"].search(domain):
  106. consents_vals.append(
  107. {
  108. "partner_id": missing.id,
  109. "accepted": one.default_consent,
  110. "activity_id": one.id,
  111. }
  112. )
  113. # Create and send consent request emails for automatic activitys
  114. consents = self.env["privacy.consent"].create(consents_vals)
  115. consents.action_auto_ask()
  116. # Redirect user to new consent requests generated
  117. return {
  118. "domain": [("id", "in", consents.ids)],
  119. "name": _("Generated consents"),
  120. "res_model": consents._name,
  121. "type": "ir.actions.act_window",
  122. "view_mode": "tree,form",
  123. }