Skip to content

Commit

Permalink
[MIG] mail_template_attach_existing_attachment_account: Migration to …
Browse files Browse the repository at this point in the history
…17.0
  • Loading branch information
damdam-s committed Jan 23, 2025
1 parent 71aa746 commit 3899c10
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "Pierre Verkest, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/social",
"category": "Social Network",
"version": "14.0.1.1.0",
"version": "17.0.1.0.0",
"license": "AGPL-3",
"depends": [
"mail_attach_existing_attachment_account",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,21 @@
# Copyright 2022 Foodles (http://www.foodles.co).
# @author Pierre Verkest <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.tests.common import Form, SavepointCase

from odoo.addons.mail.tests.common import MockEmail
from odoo.addons.account.tests.test_account_move_send import TestAccountMoveSendCommon


class TestTemplateAttachExistingAttachment(SavepointCase, MockEmail):
@classmethod
def _create_invoice(cls):
account_type = cls.env.ref("account.data_account_type_other_income")
income_account = cls.env["account.account"].search(
[
("user_type_id", "=", account_type.id),
("company_id", "=", cls.env.company.id),
],
limit=1,
)
invoice_form = Form(
cls.env["account.move"].with_context(
default_move_type="out_invoice",
tracking_disable=True,
)
)
invoice_form.partner_id = cls.partner
with invoice_form.invoice_line_ids.new() as line_form:
line_form.product_id = cls.product
line_form.account_id = income_account
invoice = invoice_form.save()
invoice.action_post()
return invoice

class TestTemplateAttachExistingAttachment(TestAccountMoveSendCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.template = cls.env.ref("account.email_template_edi_invoice")
cls.journal = cls.env["account.journal"].create(
{"name": "Test sale journal", "code": "TSALE", "type": "sale"}
)
cls.product = cls.env["product.product"].create({"name": "Test product"})
cls.partner = cls.env["res.partner"].create(
{
"name": "Partner",
"email": "[email protected]",
}
)

cls.invoice_01 = cls._create_invoice()
cls.invoice_02 = cls._create_invoice()
cls.invoice_03_no_attachment = cls._create_invoice()
cls.invoice_01 = cls.init_invoice("out_invoice", amounts=[1000], post=True)
cls.invoice_02 = cls.init_invoice("out_invoice", amounts=[1000], post=True)
cls.invoice_03_no_attachment = cls.init_invoice(
"out_invoice", amounts=[1000], post=True
)
cls.attach_p1_csv1 = cls.env["ir.attachment"].create(
{
"name": "attach1.csv",
Expand Down Expand Up @@ -98,163 +65,120 @@ def setUpClass(cls):
}
)

def _get_composer_context(self, records, mass_mail=False, overwrite=None):
"""Inspired from
addons.test_mail.tests.test_mail_composer:TestMailComposer._get_web_context
"""
if not overwrite:
overwrite = {}
ctx = dict(
default_model=records._name,
active_model=records._name,
active_id=records[0].id,
active_ids=records.ids,
default_composition_mode="mass_mail" if mass_mail else "comment",
mail_auto_delete=False,
default_template_id=self.template.id,
)
if not mass_mail:
ctx["default_res_id"] = self.invoice_01.id
ctx.update(overwrite)
return ctx

def test_default_images_attachments_from_template(self):
self.template.attach_exist_document_regex = ".*.[png|jpg]"
with Form(
self.env["account.invoice.send"].with_context(
**self._get_composer_context(
self.invoice_01, overwrite={"default_template_id": None}
)
composer = self.create_send_and_print(self.invoice_01, mail_template_id=None)

# invoice report
self.assertEqual(len(composer.mail_attachments_widget), 1)
composer.mail_template_id = self.template
# invoice report + 2 images
self.assertEqual(len(composer.mail_attachments_widget), 3)

self.assertTrue(
any(
[
attachment["name"].endswith(".jpg")
for attachment in composer.mail_attachments_widget
]
)
) as composer:
self.assertEqual(len(composer.object_attachment_ids), 0)
composer.template_id = self.template
self.assertEqual(len(composer.object_attachment_ids), 2)
composed_mail = composer.save()
self.assertEqual(
composed_mail.object_attachment_ids,
(self.attach_p1_jpg | self.attach_p1_png),
)
self.assertTrue(
any(
[
attachment["name"].endswith(".png")
for attachment in composer.mail_attachments_widget
]
)
)

composer.action_send_and_print(allow_fallback_pdf=True)
message = self._get_mail_message(self.invoice_01)
self.assertTrue(len(message.attachment_ids), 3)

def test_clear_default_images_attachments_changing_template(self):
self.template.attach_exist_document_regex = ".*.[png|jpg]"
with Form(
self.env["account.invoice.send"].with_context(
**self._get_composer_context(self.invoice_01)
)
) as composer:
self.assertEqual(len(composer.object_attachment_ids), 2)
composer.template_id = self.env["mail.template"].browse()
self.assertEqual(len(composer.object_attachment_ids), 0)
composer = self.create_send_and_print(self.invoice_01)

self.assertEqual(len(composer.mail_attachments_widget), 3)
composer.mail_template_id = False
self.assertEqual(len(composer.mail_attachments_widget), 1)
composer.action_send_and_print(allow_fallback_pdf=True)
message = self._get_mail_message(self.invoice_01)
self.assertTrue(len(message.attachment_ids), 1)

def test_send_email_with_default_and_manual_extra_attachment(self):
self.template.attach_exist_document_regex = ".*.[png|jpg]"
with Form(
self.env["account.invoice.send"].with_context(
**self._get_composer_context(self.invoice_01)
)
) as composer:
composer.object_attachment_ids.add(self.attach_p1_csv1)
self.assertEqual(len(composer.object_attachment_ids), 3)
composed_mail = composer.save()

self.assertEqual(len(composed_mail.object_attachment_ids), 3)
self.assertEqual(
(self.attach_p1_png | self.attach_p1_jpg | self.attach_p1_csv1)
& composed_mail.object_attachment_ids,
(self.attach_p1_png | self.attach_p1_jpg | self.attach_p1_csv1),
)
composer = self.create_send_and_print(self.invoice_01)

with self.mock_mail_gateway():
composed_mail.send_and_print_action()

self.assertEqual(
(self.attach_p1_png | self.attach_p1_jpg | self.attach_p1_csv1)
& self.invoice_01.message_ids[0].attachment_ids,
(self.attach_p1_png | self.attach_p1_jpg | self.attach_p1_csv1),
)
self.assertEqual(len(composer.mail_attachments_widget), 3)
composer.object_attachment_ids |= self.attach_p1_csv1
composer.action_send_and_print(allow_fallback_pdf=True)
message = self._get_mail_message(self.invoice_01)
self.assertTrue(len(message.attachment_ids), 4)

def test_no_pattern(self):
self.template.attach_exist_document_regex = False
with Form(
self.env["account.invoice.send"].with_context(
**self._get_composer_context(self.invoice_01)
)
) as composer:
self.assertEqual(len(composer.object_attachment_ids), 0)
composed_mail = composer.save()
self.assertEqual(len(composed_mail.object_attachment_ids), 0)
composer = self.create_send_and_print(self.invoice_01)

self.assertEqual(len(composer.mail_attachments_widget), 1)
composer.action_send_and_print(allow_fallback_pdf=True)
message = self._get_mail_message(self.invoice_01)
self.assertTrue(len(message.attachment_ids), 1)

def test_send_mass_mail_with_default_extra_attachment(self):
self.template.attach_exist_document_regex = ".*.csv"
records = self.invoice_01 | self.invoice_02 | self.invoice_03_no_attachment
composed_mail = Form(
self.env["account.invoice.send"].with_context(
**self._get_composer_context(records, mass_mail=True)
)
).save()
composer = self.create_send_and_print(records)
composer.action_send_and_print(allow_fallback_pdf=True)

with self.mock_mail_gateway():
composed_mail.send_and_print_action()
message_invoice_01 = self._get_mail_message(self.invoice_01)
message_invoice_02 = self._get_mail_message(self.invoice_02)
message_invoice_03 = self._get_mail_message(self.invoice_03_no_attachment)

self.assertEqual(len(self.invoice_01.message_ids[0].attachment_ids), 3)
self.assertEqual(len(message_invoice_01.attachment_ids), 3)
self.assertTrue(
self.attach_p1_csv1 in self.invoice_01.message_ids[0].attachment_ids,
self.attach_p1_csv1.name
in message_invoice_01.attachment_ids.mapped("name"),
)
self.assertTrue(
self.attach_p1_csv2 in self.invoice_01.message_ids[0].attachment_ids,
self.attach_p1_csv2.name
in message_invoice_01.attachment_ids.mapped("name"),
)
self.assertEqual(len(self.invoice_02.message_ids[0].attachment_ids), 2)

self.assertEqual(len(message_invoice_02.attachment_ids), 2)
self.assertTrue(
self.attach_p2_csv in self.invoice_02.message_ids[0].attachment_ids
)
self.assertEqual(
len(self.invoice_03_no_attachment.message_ids[0].attachment_ids), 1
self.attach_p2_csv.name in message_invoice_02.attachment_ids.mapped("name")
)
self.assertEqual(len(message_invoice_03.attachment_ids), 1)

def test_mass_mailing_no_pattern(self):
self.template.attach_exist_document_regex = False
records = self.invoice_01 | self.invoice_02 | self.invoice_03_no_attachment
composed_mail = Form(
self.env["account.invoice.send"].with_context(
**self._get_composer_context(records, mass_mail=True)
)
).save()
self.assertEqual(len(composed_mail.object_attachment_ids), 0)
composer = self.create_send_and_print(records)
composer.action_send_and_print(allow_fallback_pdf=True)

with self.mock_mail_gateway():
composed_mail.send_and_print_action()
message_invoice_01 = self._get_mail_message(self.invoice_01)
message_invoice_02 = self._get_mail_message(self.invoice_02)
message_invoice_03 = self._get_mail_message(self.invoice_03_no_attachment)

self.assertEqual(len(self.invoice_01.message_ids[0].attachment_ids), 1)
self.assertEqual(len(self.invoice_02.message_ids[0].attachment_ids), 1)
self.assertEqual(
len(self.invoice_03_no_attachment.message_ids[0].attachment_ids), 1
)
self.assertEqual(len(message_invoice_01.attachment_ids), 1)
self.assertEqual(len(message_invoice_02.attachment_ids), 1)
self.assertEqual(len(message_invoice_03.attachment_ids), 1)

def test_switch_template_with_different_templates(self):
jpg_template = self.template
png_template = self.template.copy()
jpg_template.attach_exist_document_regex = ".*.jpg"
png_template.attach_exist_document_regex = ".*.png"
with Form(
self.env["mail.compose.message"].with_context(
**self._get_composer_context(
self.invoice_01, overwrite={"default_template_id": None}
)
)
) as composer:
self.assertEqual(len(composer.object_attachment_ids), 0)
composer.template_id = jpg_template
self.assertEqual(len(composer.object_attachment_ids), 1)
composed_mail = composer.save()
self.assertEqual(
composed_mail.object_attachment_ids,
self.attach_p1_jpg,
)
composer.template_id = png_template
composed_mail = composer.save()
self.assertEqual(len(composer.object_attachment_ids), 1)
self.assertEqual(
composed_mail.object_attachment_ids,
self.attach_p1_png,
)
composer = self.create_send_and_print(self.invoice_01, mail_template_id=None)
self.assertEqual(len(composer.mail_attachments_widget), 1)
composer.mail_template_id = jpg_template
self.assertEqual(len(composer.mail_attachments_widget), 2)
composer.mail_template_id = png_template
self.assertEqual(len(composer.mail_attachments_widget), 2)
composer.action_send_and_print(allow_fallback_pdf=True)
message = self._get_mail_message(self.invoice_01)
self.assertTrue(
self.attach_p1_png.name in message.attachment_ids.mapped("name")
)
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import account_invoice_send
from . import account_move_send

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2022 Foodles (http://www.foodles.co).
# @author Pierre Verkest <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import re

from odoo import api, models


class AccountMoveSend(models.TransientModel):
_inherit = "account.move.send"

@api.model
def _match_attachment(self, re_pattern, model, res_ids):
"""Could be nice to perform regex in sql side using PGSQL `~` operator"""
return (
self.env["ir.attachment"]
.sudo()
.search(
[
("res_model", "=", model),
("res_id", "in", res_ids),
],
order="res_id",
)
.filtered(
lambda attachement, pattern=re_pattern: pattern.match(attachement.name)
is not None
)
)

def _get_regex_attachment_ids(self, move):
result = {}
for record in self:
object_attachment_ids = self.env["ir.attachment"].browse()
if (
record.mail_template_id
and record.mail_template_id.attach_exist_document_regex
):
pattern = re.compile(
record.mail_template_id.attach_exist_document_regex
)
object_attachment_ids |= self._match_attachment(
pattern, "account.move", move.ids
)
result[record.id] = object_attachment_ids
return result

@api.model
def _get_invoice_extra_attachments(self, move):
res = super()._get_invoice_extra_attachments(move)
regex_attachments = self._get_regex_attachment_ids(move)
if regex_attachments:
res |= regex_attachments[self.id]
return res
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
odoo-test-helper
odoo-addon-mail_attach_existing_attachment_account @ git+https://github.com/OCA/social.git@refs/pull/1547/head#subdirectory=mail_attach_existing_attachment_account

0 comments on commit 3899c10

Please sign in to comment.