Browse Source

Merge PR #1604 into 12.0

Signed-off-by sebastienbeau
12.0-mig-module_prototyper_last
OCA-git-bot 4 years ago
parent
commit
0c9adc37ca
  1. 8
      sql_export/wizard/wizard_file.py
  2. 1
      sql_export_mail/__init__.py
  3. 20
      sql_export_mail/__manifest__.py
  4. 94
      sql_export_mail/i18n/fr.po
  5. 89
      sql_export_mail/i18n/sql_export_mail.pot
  6. 22
      sql_export_mail/mail_template.xml
  7. 1
      sql_export_mail/models/__init__.py
  8. 148
      sql_export_mail/models/sql_export.py
  9. 7
      sql_export_mail/readme/CONFIGURE.rst
  10. 1
      sql_export_mail/readme/CONTRIBUTORS.rst
  11. 1
      sql_export_mail/readme/DESCRIPTION.rst
  12. 1
      sql_export_mail/tests/__init__.py
  13. 29
      sql_export_mail/tests/test_sql_query_mail.py
  14. 29
      sql_export_mail/views/sql_export_view.xml

8
sql_export/wizard/wizard_file.py

@ -64,9 +64,13 @@ class SqlFileWizard(models.TransientModel):
for field in sql_export.field_ids:
variable_dict[field.name] = self[field.name]
if "%(company_id)s" in sql_export.query:
variable_dict['company_id'] = self.env.user.company_id.id
company_id = self.env.context.get(
'force_company', self.env.user.company_id.id)
variable_dict['company_id'] = company_id
if "%(user_id)s" in sql_export.query:
variable_dict['user_id'] = self.env.uid
user_id = self.env.context.get(
'force_user', self._uid)
variable_dict['user_id'] = user_id
# Execute Request
res = sql_export._execute_sql_request(

1
sql_export_mail/__init__.py

@ -0,0 +1 @@
from . import models

20
sql_export_mail/__manifest__.py

@ -0,0 +1,20 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
'name': 'SQL Export Mail',
'version': '12.0.1.0.0',
'category': 'Generic Modules',
'summary': 'Send csv file generated by sql query by mail.',
'author':
"Akretion, Odoo Community Association (OCA)",
'website': 'https://github.com/OCA/server-tools',
'depends': [
'sql_export',
'mail',
],
'license': 'AGPL-3',
'data': [
'views/sql_export_view.xml',
'mail_template.xml',
],
'installable': True,
}

94
sql_export_mail/i18n/fr.po

@ -0,0 +1,94 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * sql_export_mail
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 8.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-07-18 13:15+0000\n"
"PO-Revision-Date: 2017-07-18 13:15+0000\n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"
#. module: sql_export_mail
#: model:email.template,body_html:sql_export_mail.sql_export_mailer
msgid "\n"
"<div style=\"font-family: 'Lucida Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; \">\n"
"\n"
"<p>You will find the report ${object.name or ''} as an attachment of the mail.</p>\n"
"\n"
"</div>\n"
" "
msgstr "\n"
"<div style=\"font-family: 'Lucida Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; \">\n"
"\n"
"<p>Vous trouverez le rapport ${object.name or ''} en pièce jointe de ce mail.</p>\n"
"\n"
"</div>\n"
" "
#. module: sql_export_mail
#: model:email.template,subject:sql_export_mail.sql_export_mailer
msgid "${object.name or ''}"
msgstr "${object.name or ''}"
#. module: sql_export_mail
#: help:sql.export,mail_user_ids:0
msgid "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"
msgstr "Ajoutez les utilisateurs voulant recevoir le rapport par mail. Vous devez ensuite créer une tâche planifiée pour envoyer le mail automatiquement."
#. module: sql_export_mail
#: view:sql.export:sql_export_mail.sql_export_mail_view_form
msgid "Create Cron"
msgstr "Créer une tâche planfiée"
#. module: sql_export_mail
#: model:ir.model.fields,field_description:sql_export_mail.field_sql_export_cron_ids
#: view:sql.export:sql_export_mail.sql_export_mail_view_form
#: field:sql.export,cron_ids:0
msgid "Crons"
msgstr "Tâches planifiées"
#. module: sql_export_mail
#: selection:sql.export,mail_condition:0
msgid "File Not Empty"
msgstr "Fichier non vide."
#. module: sql_export_mail
#: code:addons/sql_export_mail/models/sql_export.py:126
#, python-format
msgid "It is not possible to execute and send a query automatically by mail if there are parameters to fill"
msgstr "Il n'est pas possible d'exécuter en envoyer le résultat d'une requête par mail si celle-ci contient des paramètres."
#. module: sql_export_mail
#: model:ir.model.fields,field_description:sql_export_mail.field_sql_export_mail_condition
#: field:sql.export,mail_condition:0
msgid "Mail condition"
msgstr "Condition d'envoi par mail"
#. module: sql_export_mail
#: model:ir.model,name:sql_export_mail.model_sql_export
msgid "SQL export"
msgstr "Export SQL"
#. module: sql_export_mail
#: code:addons/sql_export_mail/models/sql_export.py:135
#, python-format
msgid "The user does not have any e-mail address."
msgstr "L'utilisateur selectionné n'a pas d'addresse mail."
#. module: sql_export_mail
#: model:ir.model.fields,field_description:sql_export_mail.field_sql_export_mail_user_ids
#: field:sql.export,mail_user_ids:0
msgid "User to notify"
msgstr "Utilisateurs à notifier par mail"
#. module: sql_export_mail
#: view:sql.export:sql_export_mail.sql_export_mail_view_form
msgid "Users Notified by e-mail"
msgstr "Utilisateurs notifiés par mail"

89
sql_export_mail/i18n/sql_export_mail.pot

@ -0,0 +1,89 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * sql_export_mail
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 8.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-07-18 13:24+0000\n"
"PO-Revision-Date: 2017-07-18 13:24+0000\n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"
#. module: sql_export_mail
#: model:email.template,body_html:sql_export_mail.sql_export_mailer
msgid "\n"
"<div style=\"font-family: 'Lucida Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; \">\n"
"\n"
"<p>You will find the report ${object.name or ''} as an attachment of the mail.</p>\n"
"\n"
"</div>\n"
" "
msgstr ""
#. module: sql_export_mail
#: model:email.template,subject:sql_export_mail.sql_export_mailer
msgid "${object.name or ''}"
msgstr ""
#. module: sql_export_mail
#: help:sql.export,mail_user_ids:0
msgid "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"
msgstr ""
#. module: sql_export_mail
#: view:sql.export:sql_export_mail.sql_export_mail_view_form
msgid "Create Cron"
msgstr ""
#. module: sql_export_mail
#: model:ir.model.fields,field_description:sql_export_mail.field_sql_export_cron_ids
#: view:sql.export:sql_export_mail.sql_export_mail_view_form
#: field:sql.export,cron_ids:0
msgid "Crons"
msgstr ""
#. module: sql_export_mail
#: selection:sql.export,mail_condition:0
msgid "File Not Empty"
msgstr ""
#. module: sql_export_mail
#: code:addons/sql_export_mail/models/sql_export.py:126
#, python-format
msgid "It is not possible to execute and send a query automatically by e-mail if there are parameters to fill"
msgstr ""
#. module: sql_export_mail
#: model:ir.model.fields,field_description:sql_export_mail.field_sql_export_mail_condition
#: field:sql.export,mail_condition:0
msgid "Mail condition"
msgstr ""
#. module: sql_export_mail
#: model:ir.model,name:sql_export_mail.model_sql_export
msgid "SQL export"
msgstr ""
#. module: sql_export_mail
#: code:addons/sql_export_mail/models/sql_export.py:135
#, python-format
msgid "The user does not have any e-mail address."
msgstr ""
#. module: sql_export_mail
#: model:ir.model.fields,field_description:sql_export_mail.field_sql_export_mail_user_ids
#: field:sql.export,mail_user_ids:0
msgid "User to notify"
msgstr ""
#. module: sql_export_mail
#: view:sql.export:sql_export_mail.sql_export_mail_view_form
msgid "Users Notified by e-mail"
msgstr ""

22
sql_export_mail/mail_template.xml

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo noupdate="1">
<!-- Error Email template -->
<record id="sql_export_mailer" model="mail.template">
<field name="name">SQL Export</field>
<field name="email_from">admin@example.com</field>
<field name="email_to">${object.get_email_address_for_template()}</field>
<field name="subject">${object.name or ''}</field>
<field name="model_id" ref="sql_export.model_sql_export"/>
<field name="auto_delete" eval="True"/>
<field name="body_html"><![CDATA[
<div style="font-family: 'Lucida Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; ">
<p>You will find the report ${object.name or ''} as an attachment of the mail.</p>
</div>
]]></field>
</record>
</odoo>

1
sql_export_mail/models/__init__.py

@ -0,0 +1 @@
from . import sql_export

148
sql_export_mail/models/sql_export.py

@ -0,0 +1,148 @@
# Copyright 2019 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import models, fields, api, _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.exceptions import UserError
from datetime import datetime, timedelta
from odoo import SUPERUSER_ID
class SqlExport(models.Model):
_inherit = 'sql.export'
mail_user_ids = fields.Many2many(
'res.users',
'mail_user_sqlquery_rel',
'sql_id',
'user_id',
'User to notify',
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')
cron_ids = fields.Many2many(
'ir.cron',
'cron_sqlquery_rel',
'sql_id',
'cron_id',
'Crons')
# We could implement other conditions, that is why it is a selection field
mail_condition = fields.Selection(
[('not_empty', 'File Not Empty')], default='not_empty')
@api.multi
def create_cron(self):
self.ensure_one()
nextcall = datetime.now() + timedelta(hours=2)
cron_vals = {
'active': True,
'model_id': self.env.ref('sql_export.model_sql_export').id,
'state': 'code',
'code': 'model._run_all_sql_export_for_cron()',
'name': 'SQL Export : %s' % self.name,
'nextcall': nextcall,
'doall': False,
'numbercall': -1,
'user_id': SUPERUSER_ID,
}
cron = self.env['ir.cron'].create(cron_vals)
# We need to pass cron_id in the cron args because a cron is not
# aware of itself in the end method and we need it to find all
# linked sql exports
write_vals = {
'code': 'model._run_all_sql_export_for_cron([%s])' % cron.id
}
cron.write(write_vals)
self.write({'cron_ids': [(4, cron.id)]})
@api.multi
def send_mail(self, params=None):
self.ensure_one()
mail_template = self.env.ref('sql_export_mail.sql_export_mailer')
now_time = datetime.strftime(datetime.now(),
DEFAULT_SERVER_DATETIME_FORMAT)
attach_obj = self.env['ir.attachment']
if self.mail_condition == 'not_empty':
res = self._execute_sql_request(
params=params, mode='fetchone')
if not res:
return
ctx = self.env.context.copy()
if params:
if 'user_id' in params:
ctx['force_user'] = params['user_id']
if 'company_id' in params:
ctx['force_company'] = params['company_id']
wizard = self.env['sql.file.wizard'].create({
'sql_export_id': self.id,
})
wizard.with_context(ctx).export_sql()
binary = wizard.binary_file
filename = wizard.file_name
msg_id = mail_template.send_mail(self.id, force_send=False)
mail = self.env['mail.mail'].browse(msg_id)
attach_vals = {
'name': now_time + ' - ' + self.name,
'datas_fname': filename,
'datas': binary,
'res_model': 'mail.mail',
'res_id': mail.id,
}
attachment = attach_obj.create(attach_vals)
mail.write({'attachment_ids': [(4, attachment.id)]})
@api.model
def _run_all_sql_export_for_cron(self, cron_ids):
exports = self.search([('cron_ids', 'in', cron_ids)])
for export in exports:
if "%(company_id)s" in export.query and \
"%(user_id)s" not in export.query:
variable_dict = {}
companies = self.env['res.company'].search([])
for company in companies:
users = export.mail_user_ids.filtered(
lambda u: u.company_id == company)
if users:
variable_dict['company_id'] = users[0].company_id.id
export.with_context(mail_to=users.ids).send_mail(
params=variable_dict)
elif "%(user_id)s" in export.query:
variable_dict = {}
for user in export.mail_user_ids:
variable_dict['user_id'] = user.id
if "%(company_id)s" in export.query:
variable_dict['company_id'] = user.company_id.id
export.with_context(mail_to=[user.id]).send_mail(
params=variable_dict)
else:
export.send_mail()
@api.multi
@api.constrains('field_ids', 'mail_user_ids')
def check_no_parameter_if_sent_by_mail(self):
for export in self:
if export.field_ids and export.mail_user_ids:
raise UserError(_(
"It is not possible to execute and send a query "
"automatically by mail if there are parameters to fill"))
@api.multi
@api.constrains('mail_user_ids')
def check_mail_user(self):
for export in self:
for user in export.mail_user_ids:
if not user.email:
raise UserError(_(
"The user does not have any e-mail address."))
@api.multi
def get_email_address_for_template(self):
"""
Called from mail template
"""
self.ensure_one()
if self.env.context.get('mail_to'):
mail_users = self.env['res.users'].browse(
self.env.context.get('mail_to'))
else:
mail_users = self.mail_user_ids
return ','.join([x.email for x in mail_users if x.email])

7
sql_export_mail/readme/CONFIGURE.rst

@ -0,0 +1,7 @@
To configure this module, you need to:
#. Go to the sql query for which you want users to be notified by e-mail.
#. Add users to be notified in the field Users Notified by e-mail.
#. Click on the button create a cron and then configure the cron to run when
you want to. If you already have created a cron for another query, you can
use it again for other queries

1
sql_export_mail/readme/CONTRIBUTORS.rst

@ -0,0 +1 @@
* Florian da Costa <florian.dacosta@akretion.com>

1
sql_export_mail/readme/DESCRIPTION.rst

@ -0,0 +1 @@
Allow to send the result of a query (made with the module sql_export) by mail.

1
sql_export_mail/tests/__init__.py

@ -0,0 +1 @@
from . import test_sql_query_mail

29
sql_export_mail/tests/test_sql_query_mail.py

@ -0,0 +1,29 @@
# Copyright (C) 2019 Akretion (<http://www.akretion.com>)
# @author: Florian da Costa
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests.common import TransactionCase
from odoo import SUPERUSER_ID
class TestExportSqlQueryMail(TransactionCase):
def setUp(self):
super(TestExportSqlQueryMail, self).setUp()
self.sql_report_demo = self.env.ref('sql_export.sql_export_partner')
self.sql_report_demo.write({'mail_user_ids': [(4, SUPERUSER_ID)]})
def test_sql_query_mail(self):
mail_obj = self.env['mail.mail']
mails = mail_obj.search(
[('model', '=', 'sql.export'),
('res_id', '=', self.sql_report_demo.id)])
self.assertFalse(mails)
self.sql_report_demo.create_cron()
self.assertTrue(self.sql_report_demo.cron_ids)
self.sql_report_demo.cron_ids.method_direct_trigger()
mails = mail_obj.search(
[('model', '=', 'sql.export'),
('res_id', '=', self.sql_report_demo.id)])
self.assertTrue(mails)
self.assertTrue(mails.attachment_ids)

29
sql_export_mail/views/sql_export_view.xml

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="sql_export_mail_view_form" model="ir.ui.view">
<field name="model">sql.export</field>
<field name="inherit_id" ref="sql_export.sql_export_view_form" />
<field name="arch" type="xml">
<button name="export_sql_query" position="after">
<button name="create_cron" string="Create Cron" type="object" groups="base.group_system"
attrs="{'invisible': ['|', ('state', '=', 'draft'), ('mail_user_ids', '=', [(6, False, [])])]}"/>
</button>
<field name="copy_options" position="after">
<field name="mail_condition" attrs="{'invisible': [('cron_ids', '=', [(6, False, [])])]}"/>
</field>
<group name="parameters" position="after">
<group groups="sql_request_abstract.group_sql_request_user">
<group string="Users Notified by e-mail">
<field name="mail_user_ids" nolabel="1"/>
</group>
<group string="Crons" groups="base.group_system">
<field name="cron_ids" nolabel="1" domain="[('function', '=', '_run_all_sql_export_for_cron')]" groups="base.group_system"/>
</group>
</group>
</group>
</field>
</record>
</odoo>
Loading…
Cancel
Save