Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1506,15 +1506,21 @@ def get_xml_files_from_pdf(self, pdf_file):
@api.model
def post_create_or_update(self, parsed_dict, record, doc_filename=None):
if parsed_dict.get("attachments"):
attachments = self.env["ir.attachment"]
for filename, data_base64 in parsed_dict["attachments"].items():
self.env["ir.attachment"].create(
attachments |= self.env["ir.attachment"].create(
{
"name": filename,
"res_id": record.id,
"res_model": str(record._name),
"datas": data_base64,
}
)

if hasattr(record, "_message_set_main_attachment_id"):
record.sudo()._message_set_main_attachment_id(
[(4, attach.id) for attach in attachments]
)
chatter_msg_html = []
for msg in parsed_dict["chatter_msg"]:
chatter_msg_html.append(msg.replace("\n", "<br>"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import base64
import logging

from odoo.exceptions import UserError
Expand Down Expand Up @@ -481,3 +482,24 @@ def test_match_account_smaller_in(self):
res = bdio._match_account({"code": "898999"}, chatter)
self.assertEqual(acc, res)
self.assertEqual(len(chatter), 1)

def test_post_create_or_update_main_attachment(self):
partner = self.env["res.partner"].create({"name": "Main attachment test"})
xml_b64 = base64.b64encode(b"<?xml version='1.0'?><Invoice/>")
pdf_b64 = base64.b64encode(b"%PDF-1.4\n%%EOF\n")
parsed_dict = {
"attachments": {"invoice.xml": xml_b64, "invoice.pdf": pdf_b64},
"chatter_msg": [],
}
self.env["business.document.import"].post_create_or_update(parsed_dict, partner)
self.assertEqual(partner.message_main_attachment_id.name, "invoice.pdf")

def test_post_create_or_update_main_attachment_xml_only(self):
partner = self.env["res.partner"].create({"name": "Main attachment test"})
xml_b64 = base64.b64encode(b"<?xml version='1.0'?><Invoice/>")
parsed_dict = {
"attachments": {"invoice.xml": xml_b64},
"chatter_msg": [],
}
self.env["business.document.import"].post_create_or_update(parsed_dict, partner)
self.assertFalse(partner.message_main_attachment_id)
Loading