[FIX] support for attachment to record

This commit is contained in:
KKamaa
2022-08-26 11:49:40 +03:00
parent 65e4a41a89
commit cb5bbcff6a
3 changed files with 44 additions and 1 deletions

View File

@@ -144,7 +144,8 @@ class ReportAsync(models.Model):
@api.model
def print_document_async(
self, record_ids, report_name, html=None, data=None, to_email=""
self, record_ids, report_name, html=None, data=None, to_email="",
save_attachment_to_records=False
):
"""Generate a document async, do not return the document file"""
user_email = to_email or self.env.user.email
@@ -157,6 +158,7 @@ class ReportAsync(models.Model):
email_notify=True,
to_email=user_email,
session_id=request.session.sid,
save_attachment_to_records=save_attachment_to_records
)
@api.model
@@ -169,6 +171,7 @@ class ReportAsync(models.Model):
email_notify=False,
to_email=None,
session_id=None,
save_attachment_to_records=False
):
report = self.env["ir.actions.report"].browse(report_id)
func = REPORT_TYPES_FUNC[report.report_type]
@@ -199,6 +202,22 @@ class ReportAsync(models.Model):
}
)
)
# save attachment to records
if save_attachment_to_records:
model = report.model
records = self.env[model].browse(docids)
for record in records:
attachment = self.env["ir.attachment"].sudo().create(
{
"name": out_name,
"datas": out_file,
"type": "binary",
"res_model": model,
"res_id": record.id,
}
)
if hasattr(record, 'message_post'):
record.message_post(attachment_ids=[attachment.id])
self._cr.execute(
"""
UPDATE ir_attachment SET create_uid = %s, write_uid = %s