[MIG] sql_export_mail from OCA/server-tools 14.0 to OCA/reporting-engine 16.0 branch

- Update views, regarding changes in sql_export module
- Add prepare function for cron vals
This commit is contained in:
Sylvain LE GAL
2022-10-26 16:24:25 +02:00
parent 12d67040df
commit c62e8f2ad9
6 changed files with 48 additions and 29 deletions

View File

@@ -27,21 +27,23 @@ class SqlExport(models.Model):
[("not_empty", "File Not Empty")], default="not_empty"
)
def create_cron(self):
def _prepare_cron_mail(self):
self.ensure_one()
nextcall = datetime.now() + timedelta(hours=2)
cron_vals = {
return {
"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,
"nextcall": datetime.now() + timedelta(hours=2),
"doall": False,
"numbercall": -1,
"user_id": SUPERUSER_ID,
}
cron = self.env["ir.cron"].create(cron_vals)
def create_cron(self):
self.ensure_one()
cron = self.env["ir.cron"].create(self._prepare_cron_mail())
# 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
@@ -51,24 +53,25 @@ class SqlExport(models.Model):
def send_mail(self, params=None):
self.ensure_one()
params = params or {}
mail_template = self.env.ref("sql_export_mail.sql_export_mailer")
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()
if "user_id" in params:
wizard = wizard.with_context(force_user=params["user_id"])
if "company_id" in params:
wizard = wizard.with_context(force_company=params["company_id"])
wizard.export_sql()
binary = wizard.binary_file
filename = wizard.file_name
msg_id = mail_template.send_mail(self.id, force_send=False)