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.

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