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.

88 lines
3.8 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()`.
6 years ago
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Copyright 2018 Tecnativa - Jairo Llopis
  3. License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
  4. <data>
  5. <record id="activity_form" model="ir.ui.view">
  6. <field name="name">Add consent fields</field>
  7. <field name="model">privacy.activity</field>
  8. <field name="inherit_id" ref="privacy.activity_form"/>
  9. <field name="arch" type="xml">
  10. <div name="button_box" position="inside">
  11. <button
  12. attrs='{"invisible": [("consent_required", "=", False)]}'
  13. class="oe_stat_button"
  14. context='{"search_default_activity_id": active_id}'
  15. icon="fa-handshake-o"
  16. name="%(consent_action)d"
  17. type="action"
  18. >
  19. <field
  20. name="consent_count"
  21. widget="statinfo"
  22. />
  23. </button>
  24. </div>
  25. <notebook name="advanced" position="inside">
  26. <page string="Consent" name="consent">
  27. <group>
  28. <label for="consent_required"/>
  29. <div>
  30. <field name="consent_required" class="oe_inline"/>
  31. <button
  32. attrs='{"invisible": [("consent_required", "!=", "manual")]}'
  33. class="btn-link"
  34. icon="fa-user-plus"
  35. name="action_new_consents"
  36. type="object"
  37. string="Generate missing draft consent requests"
  38. />
  39. <button
  40. attrs='{"invisible": [("consent_required", "!=", "auto")]}'
  41. class="btn-link"
  42. icon="fa-user-plus"
  43. name="action_new_consents"
  44. type="object"
  45. string="Generate and enqueue missing consent requests"
  46. confirm="This could enqueue many consent emails, are you sure to proceed?"
  47. />
  48. </div>
  49. </group>
  50. <group
  51. attrs='{"invisible": [("consent_required", "=", False)]}'
  52. >
  53. <group>
  54. <field name="default_consent"/>
  55. <field
  56. name="server_action_id"
  57. groups="base.group_no_one"
  58. />
  59. </group>
  60. <group>
  61. <field
  62. name="consent_template_default_body_html"
  63. invisible="1"
  64. />
  65. <field
  66. name="consent_template_default_subject"
  67. invisible="1"
  68. />
  69. <field
  70. name="consent_template_id"
  71. attrs='{"required": [("consent_required", "=", "auto")]}'
  72. context='{
  73. "default_model": "privacy.consent",
  74. "default_body_html": consent_template_default_body_html,
  75. "default_subject": consent_template_default_subject,
  76. }'
  77. />
  78. </group>
  79. </group>
  80. </page>
  81. </notebook>
  82. </field>
  83. </record>
  84. </data>