Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] sql_export_mail, add partner as export email recipient #979

Open
wants to merge 3 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions sql_export_mail/models/sql_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
help="Add the users who want to receive the report by e-mail. You "
"need to link the sql query with a cron to send mail automatically",
)
mail_partner_ids = fields.Many2many(
"res.partner",
"mail_partner_sqlquery_rel",
"sql_id",
"partner_id",
help="Add the partners who wants to receive the report by e-mail. You "
"need to link the sql query with a cron to send a mail automatically",
)
cron_ids = fields.Many2many(
"ir.cron",
"cron_sqlquery_rel",
Expand Down Expand Up @@ -138,13 +146,43 @@
if not user.email:
raise UserError(_("The user does not have any e-mail address."))

@api.constrains("mail_partner_ids", "query")
def _check_mail_partner(self):
for export in self:
if export.mail_partner_ids and (
"%(company_id)s" in export.query or "%(user_id)s" in export.query
):
raise UserError(

Check warning on line 155 in sql_export_mail/models/sql_export.py

View check run for this annotation

Codecov / codecov/patch

sql_export_mail/models/sql_export.py#L155

Added line #L155 was not covered by tests
_(
"A query that uses the company_id or user_id parameter "
"cannot be directly sent to a partner."
)
)
missing_email_partners = export.mail_partner_ids.filtered(
lambda partner: not partner.email
)
if missing_email_partners:
raise UserError(

Check warning on line 165 in sql_export_mail/models/sql_export.py

View check run for this annotation

Codecov / codecov/patch

sql_export_mail/models/sql_export.py#L165

Added line #L165 was not covered by tests
_(
"Missing email address for partner(s): %(names)s",
names=", ".join(missing_email_partners.mapped("name")),
)
)

def get_email_address_for_template(self):
"""
Called from mail template
Called from mail template.
Collects email addresses from both users and partners.
"""
self.ensure_one()
if self.env.context.get("mail_to"):
mail_users = self.env["res.users"].browse(self.env.context.get("mail_to"))
mail_partners = self.env["res.partner"]
else:
mail_users = self.mail_user_ids
return ",".join([x.email for x in mail_users if x.email])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there was a test if user email was defined (or not). Why is the test removed ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@legalsylvain legalsylvain Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so.

Just tested in a shell :

(Console) >>> env["res.partner"].create({"name": "bob"}).mapped("email") [False] >>> ",".join([False]) Traceback (most recent call last): File "", line 1, in TypeError: sequence item 0: expected str instance, bool found

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stand corrected, thanks ;). See 2nd fixup commit

mail_partners = self.mail_partner_ids
email_addresses = set(
mail_users.mapped("email") + mail_partners.mapped("email")
)
email_addresses.discard(False)
return ",".join(email_addresses)
11 changes: 7 additions & 4 deletions sql_export_mail/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -274,7 +275,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: grey; } /* line numbers */
pre.code .ln { color: gray; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -300,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic {
span.problematic, pre.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -421,7 +422,9 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
Expand Down
11 changes: 10 additions & 1 deletion sql_export_mail/tests/test_sql_query_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,21 @@ def test_sql_query_mail_company_user(self):
)
self.check_execution()

def test_sql_query_mail_partner(self):
"""Check if emails are sent to partners"""
self.check_before_change()
partner = self.env.ref("base.res_partner_2")
self.sql_report_demo.write({"mail_partner_ids": [(4, partner.id)]})
self.check_execution(partner)

def check_before_change(self):
"""Check if there are no mails before changing the sql report"""
mails = self.env["mail.mail"].search(
[("model", "=", "sql.export"), ("res_id", "=", self.sql_report_demo.id)]
)
self.assertFalse(mails)

def check_execution(self):
def check_execution(self, partner=None):
"""Check if the cron could be created and the mail sending is working"""
self.sql_report_demo.create_cron()
self.assertTrue(self.sql_report_demo.cron_ids)
Expand All @@ -70,3 +77,5 @@ def check_execution(self):
)
self.assertTrue(mails)
self.assertTrue(mails.attachment_ids)
if partner:
self.assertIn(partner.email, mails.mapped("email_to"))
8 changes: 8 additions & 0 deletions sql_export_mail/views/sql_export_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
colspan="2"
/>
</group>
<group string="Partners Notified by e-mail">
<field
name="mail_partner_ids"
nolabel="1"
widget="many2many_tags"
colspan="2"
/>
</group>
<group string="Crons" groups="base.group_system">
<field
name="cron_ids"
Expand Down