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.

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