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.

158 lines
8.0 KiB

privacy_consent: Separate automated emails send process Before https://github.com/OCA/data-protection/pull/29 there was a race condition where an email could be sent while the same transaction that created the `privacy.consent` record still wasn't committed, producing a 404 error if the user clicked on "Accept" or "Reject" before all mails were sent. To avoid that, a raw `cr.commit()` was issued, but this produced another situation where the user had to wait until the full email queue is cleared to get his page loaded. It wasn't an error, but a long queue meant several minutes waiting, and it's ulikely that an average human is so patient. So, here's the final fix (I hope!). The main problem was that I was looking in the wrong place to send the email. It turns out that the `self.post_message_with_template()` method is absolutely helpless in the case at hand, where these criteria must be met: * E-mail must be enqueued, no matter if there are less or more than 50 consents to send. * The template must be processed per record. * In an ideal world, a `cr.commit()` must be issued after each sent mail. The metod that was being used: * Didn't allow to use `auto_commit` mode. * Only allowed to render the template per record if called with `composition_mode="mass_mail"`. * Only allowed to enqueue emails if called with `composition_mode="mass_post"`. Obviously, I cannot set 2 different values for `composition_mode`, so a different strategy had to be used. I discovered that the `mail.template` model has a helpful method called `send_mail()` that, by default: * Renders the template per record * Enqueues the email * The email queue is cleared in `auto_commit=True` mode. So, from now on, problems are gone: * The user click, or the cron run, will just generate the missing `privacy.consent` records and enqueue mails for them. * The mail queue manager will send them later, in `auto_commit` mode. * After sending the e-mail, this module will set the `privacy.consent` record as `sent`. * Thanks to *not* sending the email, the process the user faces when he hits the "generate" button is faster. * Instructions in the README and text in the "generate" button are updated to reflect this new behavior. * Thanks to the `auto_commit` feature, if Odoo is rebooted in the middle of a mail queue clearance, the records that were sent remain properly marked as sent, and the missing mails will be sent after the next boot. * No hardcoded commits. * No locked transactions. * BTW I discovered that 2 different emails were created when creating a new consent. I started using `mail_create_nolog=True` to avoid that problem and only log a single creation message. Note to self: never use again `post_message_with_template()`.
5 years ago
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Copyright 2018 Tecnativa - Jairo Llopis
  3. Copyright 2019 Tecnativa - Cristina Martin R.
  4. License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
  5. <data>
  6. <!-- Mail templates -->
  7. <record id="template_consent" model="mail.template">
  8. <field name="auto_delete" eval="False"/>
  9. <field name="name">Personal data processing consent request</field>
  10. <field name="subject">Data processing consent request for ${object.activity_id.display_name|safe}</field>
  11. <field name="model_id" ref="model_privacy_consent"/>
  12. <field name="use_default_to" eval="True"/>
  13. <field name="lang">${object.partner_id.lang}</field>
  14. <field name="body_html" type="xml">
  15. <div style="background:#F3F5F6;color:#515166;padding:25px 0px;font-family:Arial,Helvetica,sans-serif;font-size:14px;">
  16. <table style="width:600px;margin:5px auto;">
  17. <tbody>
  18. <tr>
  19. <td>
  20. <a href="/">
  21. <img src="/logo" alt="${object.activity_id.controller_id.display_name|safe}" style="vertical-align:baseline;max-width:100px;"/>
  22. </a>
  23. </td>
  24. </tr>
  25. </tbody>
  26. </table>
  27. <table style="width:600px;margin:0px auto;background:white;border:1px solid #e1e1e1;">
  28. <tbody>
  29. <tr>
  30. <td colspan="2" style="padding:15px 20px 0px 20px; font-size:16px;">
  31. <p>
  32. Hello, ${object.partner_id.name|safe}
  33. </p>
  34. <p>
  35. We contacted you to ask you to give us your explicit consent to include your data in a data processing activity called
  36. <b>${object.activity_id.display_name|safe}</b>, property of
  37. <i>${object.activity_id.controller_id.display_name|safe}</i>
  38. </p>
  39. ${object.description or ""}
  40. <p>
  41. % if object.state == "answered":
  42. The last time you answered, you
  43. % elif object.state == "sent":
  44. If you do nothing, we will assume you have
  45. % endif
  46. % if object.accepted:
  47. <b>accepted</b>
  48. % else:
  49. <b>rejected</b>
  50. % endif
  51. such data processing.
  52. </p>
  53. <p>
  54. You can update your preferences below:
  55. </p>
  56. </td>
  57. </tr>
  58. <tr>
  59. <td style="padding:15px 20px 0px 20px; font-size:16px; text-align:right;">
  60. <a href="/privacy/consent/accept/" style="background-color: #449d44; padding: 12px; font-weight: 12px; text-decoration: none; color: #fff; border-radius: 5px; font-size:16px;">
  61. Accept
  62. </a>
  63. </td>
  64. <td style="padding:15px 20px 0px 20px; font-size:16px; text-align:left;">
  65. <a href="/privacy/consent/reject/" style="background-color: #d9534f; padding: 12px; font-weight: 12px; text-decoration: none; color: #fff; border-radius: 5px; font-size:16px;">
  66. Reject
  67. </a>
  68. </td>
  69. </tr>
  70. <tr>
  71. <td colspan="2" style="padding:15px 20px 15px 20px; font-size:16px;">
  72. <p>
  73. If you need further information, please respond to this email and we will attend your request as soon as possible.
  74. </p>
  75. <p>
  76. Thank you!
  77. </p>
  78. </td>
  79. </tr>
  80. </tbody>
  81. </table>
  82. <table style="width:600px;margin:0px auto;text-align:center;">
  83. <tbody>
  84. <tr>
  85. <td style="padding-top:10px;font-size: 12px;">
  86. <p>
  87. Sent by
  88. <a href="/" style="color:#717188;">${object.activity_id.controller_id.display_name|safe}</a>.
  89. </p>
  90. </td>
  91. </tr>
  92. </tbody>
  93. </table>
  94. </div>
  95. </field>
  96. </record>
  97. <!-- Mail subtypes -->
  98. <record id="mt_consent_consent_new" model="mail.message.subtype">
  99. <field name="name">New Consent</field>
  100. <field name="description">Privacy consent request created</field>
  101. <field name="res_model">privacy.consent</field>
  102. <field name="default" eval="False"/>
  103. <field name="hidden" eval="False"/>
  104. <field name="internal" eval="True"/>
  105. </record>
  106. <record id="mt_consent_acceptance_changed" model="mail.message.subtype">
  107. <field name="name">Acceptance Changed by Subject</field>
  108. <field name="description">Acceptance status updated by subject</field>
  109. <field name="res_model">privacy.consent</field>
  110. <field name="default" eval="False"/>
  111. <field name="hidden" eval="False"/>
  112. <field name="internal" eval="True"/>
  113. </record>
  114. <record id="mt_consent_state_changed" model="mail.message.subtype">
  115. <field name="name">State Changed</field>
  116. <field name="description">Privacy consent request state changed</field>
  117. <field name="res_model">privacy.consent</field>
  118. <field name="default" eval="False"/>
  119. <field name="hidden" eval="False"/>
  120. <field name="internal" eval="True"/>
  121. </record>
  122. <record id="mt_activity_consent_new" model="mail.message.subtype">
  123. <field name="name">New Consent</field>
  124. <field name="description">Privacy consent request created</field>
  125. <field name="res_model">privacy.activity</field>
  126. <field name="default" eval="True"/>
  127. <field name="hidden" eval="False"/>
  128. <field name="internal" eval="True"/>
  129. <field name="parent_id" ref="mt_consent_consent_new"/>
  130. <field name="relation_field">activity_id</field>
  131. </record>
  132. <record id="mt_activity_acceptance_changed" model="mail.message.subtype">
  133. <field name="name">Acceptance Changed</field>
  134. <field name="description">Privacy consent request acceptance status changed</field>
  135. <field name="res_model">privacy.activity</field>
  136. <field name="default" eval="True"/>
  137. <field name="hidden" eval="False"/>
  138. <field name="internal" eval="True"/>
  139. <field name="parent_id" ref="mt_consent_acceptance_changed"/>
  140. <field name="relation_field">activity_id</field>
  141. </record>
  142. <record id="mt_activity_state_changed" model="mail.message.subtype">
  143. <field name="name">State Changed</field>
  144. <field name="description">Privacy consent request state changed</field>
  145. <field name="res_model">privacy.activity</field>
  146. <field name="default" eval="False"/>
  147. <field name="hidden" eval="False"/>
  148. <field name="internal" eval="True"/>
  149. <field name="parent_id" ref="mt_consent_state_changed"/>
  150. <field name="relation_field">activity_id</field>
  151. </record>
  152. </data>