Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
damdam-s committed Jan 22, 2025
1 parent 6c8f7c8 commit b56cdc5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def _get_composer_context(self, records, mass_mail=False, overwrite=None):
active_ids=records.ids,
default_composition_mode="mass_mail" if mass_mail else "comment",
mail_auto_delete=False,
default_template_id=self.template.id,
default_mail_template_id=self.template.id,
)
if not mass_mail:
ctx["default_res_ids"] = self.invoice_01.id
Expand All @@ -121,9 +121,9 @@ def _get_composer_context(self, records, mass_mail=False, overwrite=None):
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.env["account.move.send"].with_context(
**self._get_composer_context(
self.invoice_01, overwrite={"default_template_id": None}
self.invoice_01, overwrite={"default_mail_template_id": None}
)
)
) as composer:
Expand All @@ -139,7 +139,7 @@ def test_default_images_attachments_from_template(self):
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.env["account.move.send"].with_context(
**self._get_composer_context(self.invoice_01)
)
) as composer:
Expand All @@ -150,7 +150,7 @@ def test_clear_default_images_attachments_changing_template(self):
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.env["account.move.send"].with_context(
**self._get_composer_context(self.invoice_01)
)
) as composer:
Expand All @@ -177,7 +177,7 @@ def test_send_email_with_default_and_manual_extra_attachment(self):
def test_no_pattern(self):
self.template.attach_exist_document_regex = False
with Form(
self.env["account.invoice.send"].with_context(
self.env["account.move.send"].with_context(
**self._get_composer_context(self.invoice_01)
)
) as composer:
Expand All @@ -189,7 +189,7 @@ 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.env["account.move.send"].with_context(
**self._get_composer_context(records, mass_mail=True)
)
).save()
Expand All @@ -216,7 +216,7 @@ 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.env["account.move.send"].with_context(
**self._get_composer_context(records, mass_mail=True)
)
).save()
Expand All @@ -237,9 +237,9 @@ def test_switch_template_with_different_templates(self):
jpg_template.attach_exist_document_regex = ".*.jpg"
png_template.attach_exist_document_regex = ".*.png"
with Form(
self.env["mail.compose.message"].with_context(
self.env["account.move.send"].with_context(
**self._get_composer_context(
self.invoice_01, overwrite={"default_template_id": None}
self.invoice_01, overwrite={"default_mail_template_id": None}
)
)
) as composer:
Expand Down
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,44 @@
# 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):
self.ensure_one()
object_attachment_ids = self.env["ir.attachment"].browse()
if self.mail_template_id and self.mail_template_id.attach_exist_document_regex:
pattern = re.compile(self.mail_template_id.attach_exist_document_regex)
object_attachment_ids |= self._match_attachment(
pattern, "account.move", self.move_ids.ids
)

@api.model
def _get_invoice_extra_attachments(self, move):
res = super()._get_invoice_extra_attachments(move)
res |= self._get_regex_attachment_ids()
return res

0 comments on commit b56cdc5

Please sign in to comment.