From 59966f0f5dc595f3058bd1520d27de219127d66b Mon Sep 17 00:00:00 2001 From: Eduardo De Miguel Date: Fri, 18 Mar 2022 14:21:16 +0100 Subject: [PATCH] [ADD] Customize notification appearance Added settings to customize notification and translations to important parts of the message. [FIX] Security fixes and simplified customization Field `show_followers_partner_format` formatted with %(param)s Removed CSS customizations [FIX] README options [FIX] Index.html configure [FIX] Several changes - Removed unnecesary properties on res_config_settings - Markup safe on partner_format - Use email_domain_extract instead own one --- mail_show_follower/README.rst | 7 +- mail_show_follower/__manifest__.py | 5 +- mail_show_follower/models/mail_mail.py | 84 +++++++++++++++---- mail_show_follower/models/res_company.py | 21 ++++- .../models/res_config_settings.py | 44 +++++++++- mail_show_follower/readme/CONFIGURE.rst | 5 +- mail_show_follower/readme/CONTRIBUTORS.rst | 1 + .../static/description/index.html | 9 +- .../views/res_config_settings.xml | 64 +++++++++++++- 9 files changed, 213 insertions(+), 27 deletions(-) diff --git a/mail_show_follower/README.rst b/mail_show_follower/README.rst index 94ae7e1..42feffb 100644 --- a/mail_show_follower/README.rst +++ b/mail_show_follower/README.rst @@ -41,8 +41,11 @@ Configuration To configure this module, you need to: -#. Go General settings/Discuss/Show Internal Users CC and set if want to show or not internal users in cc details. +#. Go General settings/Discuss/Show Followers on mails/Show Internal Users CC and set if want to show or not internal users in cc details. #. Go Settings/Users & Company salect any user in 'Preferences' check or not the 'Show in CC' field if this user need to appear in the cc note. +#. Go General settings/Discuss/Show Followers on mails/Text 'Sent to' and set the initial part of the message. +#. Go General settings/Discuss/Show Followers on mails/Partner format and choose desired fields to show on CC recipients. +#. Go General settings/Discuss/Show Followers on mails/Text 'Replies' and choose desired warn message Usage ===== @@ -68,12 +71,14 @@ Authors ~~~~~~~ * Sygel +* Moduon Contributors ~~~~~~~~~~~~ * Valentin Vinagre * Lorenzo Battistini +* Eduardo de Miguel Maintainers ~~~~~~~~~~~ diff --git a/mail_show_follower/__manifest__.py b/mail_show_follower/__manifest__.py index b816bd0..cfac196 100644 --- a/mail_show_follower/__manifest__.py +++ b/mail_show_follower/__manifest__.py @@ -1,13 +1,14 @@ # Copyright 2020 Valentin Vinagre +# Copyright 2022 Eduardo de Miguel # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). { "name": "Mail Show Follower", "summary": "Show CC document followers in mails.", - "version": "15.0.1.0.0", + "version": "15.0.1.1.0", "category": "Mail", "website": "https://github.com/OCA/social", - "author": "Sygel, Odoo Community Association (OCA)", + "author": "Sygel, Moduon, Odoo Community Association (OCA)", "license": "AGPL-3", "application": False, "installable": True, diff --git a/mail_show_follower/models/mail_mail.py b/mail_show_follower/models/mail_mail.py index 5c9f6ad..66e6829 100644 --- a/mail_show_follower/models/mail_mail.py +++ b/mail_show_follower/models/mail_mail.py @@ -1,16 +1,64 @@ -from odoo import models +from markupsafe import Markup + +from odoo import api, models, tools class MailMail(models.Model): _inherit = "mail.mail" - def _send(self, auto_commit=False, raise_exception=False, smtp_session=None): - plain_text = ( - '
CC: %s
' + @api.model + def _build_cc_text(self, partners): + if not partners: + return "" + + def get_ctx_param(ctx_key, default_parm): + if ctx_key in self.env.context: + return self.env.context[ctx_key] + return default_parm + + def remove_p(markup_txt): + if markup_txt.startswith("

") and markup_txt.endswith("

"): + return markup_txt[3:-4] + return markup_txt + + company = self.env.company + partner_format = get_ctx_param( + "partner_format", company.show_followers_partner_format ) + msg_sent_to = get_ctx_param( + "msg_sent_to", company.show_followers_message_sent_to + ) + msg_warn = get_ctx_param( + "msg_warn", company.show_followers_message_response_warning + ) + partner_message = ", ".join( + [ + partner_format + % { + # Supported parameters + "partner_name": p.name, + "partner_email": p.email, + "partner_email_domain": tools.email_domain_extract(p.email), + } + for p in partners + ] + ) + full_text = """ +
+ {msg_sent_to} {partner_message} + {rc}{msg_warn} +
+ """.format( + msg_sent_to=remove_p(msg_sent_to), + partner_message=Markup.escape(partner_message), + rc=msg_warn.striptags() and "
" or "", + msg_warn=msg_warn.striptags() and remove_p(msg_warn) or "", + ) + return full_text + + def _send(self, auto_commit=False, raise_exception=False, smtp_session=None): group_portal = self.env.ref("base.group_portal") for mail_id in self.ids: mail = self.browse(mail_id) @@ -73,13 +121,21 @@ class MailMail(models.Model): or x.user_ids # otherwise, email is not sent and "email" in x.user_ids.mapped("notification_type") ) - # get names and emails - final_cc = None - mails = "" - for p in partners: - mails += "%s <%s>, " % (p.name, p.email) - # join texts - final_cc = plain_text % (mails[:-2]) + # set proper lang for recipients + langs = list( + filter( + bool, + mail.mapped("recipient_ids.lang") + + [ + mail.author_id.lang, + self.env.company.partner_id.lang, + ], + ) + ) + # get show follower text + final_cc = mail.with_context( + lang=langs and langs[0] + )._build_cc_text(partners) # it is saved in the body_html field so that it does # not appear in the odoo log mail.body_html = final_cc + mail.body_html diff --git a/mail_show_follower/models/res_company.py b/mail_show_follower/models/res_company.py index fdbc33e..3e802cc 100644 --- a/mail_show_follower/models/res_company.py +++ b/mail_show_follower/models/res_company.py @@ -5,5 +5,24 @@ class ResCompany(models.Model): _inherit = "res.company" show_internal_users_cc = fields.Boolean( - string="Show Internal Users CC", default=True + string="Show Internal Users CC", + default=True, + ) + show_followers_message_sent_to = fields.Html( + string="Text 'Sent to'", + translate=True, + default="This message has been sent to", + ) + show_followers_partner_format = fields.Char( + string="Partner format", + default="%(partner_name)s", + help="Supported parameters:\n" + "%(partner_name)s = Partner Name\n" + "%(partner_email)s = Partner Email\n" + "%(partner_email_domain)s = Partner Email Domain", + ) + show_followers_message_response_warning = fields.Html( + string="Text 'Replies'", + translate=True, + default="Notice: Replies to this email will be sent to all recipients", ) diff --git a/mail_show_follower/models/res_config_settings.py b/mail_show_follower/models/res_config_settings.py index 5b46dde..43c92ed 100644 --- a/mail_show_follower/models/res_config_settings.py +++ b/mail_show_follower/models/res_config_settings.py @@ -1,11 +1,51 @@ -from odoo import fields, models +from odoo import api, fields, models class ResConfigSettings(models.TransientModel): _inherit = "res.config.settings" show_internal_users_cc = fields.Boolean( - string="Show Internal Users CC", related="company_id.show_internal_users_cc", readonly=False, ) + show_followers_message_sent_to = fields.Html( + related="company_id.show_followers_message_sent_to", + readonly=False, + ) + show_followers_partner_format = fields.Char( + related="company_id.show_followers_partner_format", + readonly=False, + help="Supported parameters:\n" + "%(partner_name)s = Partner Name\n" + "%(partner_email)s = Partner Email\n" + "%(partner_email_domain)s = Partner Email Domain", + ) + show_followers_message_response_warning = fields.Html( + related="company_id.show_followers_message_response_warning", + readonly=False, + ) + show_followers_message_preview = fields.Html( + string="Message preview", + readonly=True, + store=False, + ) + + @api.onchange( + "show_followers_message_sent_to", + "show_followers_partner_format", + "show_followers_message_response_warning", + ) + def onchange_show_followers_message_preview(self): + self.show_followers_message_preview = ( + self.env["mail.mail"] + .with_context( + # Use current data before + partner_format=self.show_followers_partner_format or "", + msg_sent_to=self.show_followers_message_sent_to or "", + msg_warn=self.show_followers_message_response_warning or "", + ) + ._build_cc_text( + # Sample partners + self.env["res.partner"].search([("email", "!=", False)], limit=3), + ) + ) diff --git a/mail_show_follower/readme/CONFIGURE.rst b/mail_show_follower/readme/CONFIGURE.rst index 02a169a..dade09d 100644 --- a/mail_show_follower/readme/CONFIGURE.rst +++ b/mail_show_follower/readme/CONFIGURE.rst @@ -1,4 +1,7 @@ To configure this module, you need to: -#. Go General settings/Discuss/Show Internal Users CC and set if want to show or not internal users in cc details. +#. Go General settings/Discuss/Show Followers on mails/Show Internal Users CC and set if want to show or not internal users in cc details. #. Go Settings/Users & Company salect any user in 'Preferences' check or not the 'Show in CC' field if this user need to appear in the cc note. +#. Go General settings/Discuss/Show Followers on mails/Text 'Sent to' and set the initial part of the message. +#. Go General settings/Discuss/Show Followers on mails/Partner format and choose desired fields to show on CC recipients. +#. Go General settings/Discuss/Show Followers on mails/Text 'Replies' and choose desired warn message diff --git a/mail_show_follower/readme/CONTRIBUTORS.rst b/mail_show_follower/readme/CONTRIBUTORS.rst index 40630f3..5aa1679 100644 --- a/mail_show_follower/readme/CONTRIBUTORS.rst +++ b/mail_show_follower/readme/CONTRIBUTORS.rst @@ -1,2 +1,3 @@ * Valentin Vinagre * Lorenzo Battistini +* Eduardo de Miguel diff --git a/mail_show_follower/static/description/index.html b/mail_show_follower/static/description/index.html index be30241..8b9d2f8 100644 --- a/mail_show_follower/static/description/index.html +++ b/mail_show_follower/static/description/index.html @@ -3,7 +3,7 @@ - + Mail Show Follower