Skip to content

Commit

Permalink
[CHG] account_payment_method_or_mode_fs_storage: export file to stora…
Browse files Browse the repository at this point in the history
…ge as late as possible
  • Loading branch information
AnizR committed Feb 10, 2025
1 parent 786b357 commit 473d250
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Copyright 2024 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import base64
import logging

from odoo import _, models
from odoo import _, api, models, registry
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -41,20 +42,46 @@ def _export_to_storage(self, file_content, filename):
) from e
return True

def generate_payment_file(self):
def _get_payment_attachment_to_export(self):
"""
Inherit to catch file generation and put it on the storage (if necessary)
Return attachment linked to payment order that should be exported
"""
file_content, filename = super().generate_payment_file()
if self._must_be_exported_to_storage():
self._export_to_storage(file_content, filename)
return file_content, filename
self.ensure_one()

return self.env["ir.attachment"].search(
[("res_model", "=", "account.payment.order"), ("res_id", "=", self.id)],
limit=1,
order="create_date DESC",
)

def open2generated(self):
self.ensure_one()
action = super().open2generated()
if self._must_be_exported_to_storage():
self.generated2uploaded()

if self._must_be_exported_to_storage():
# exporting to storage should be done as late as possible
# since it may be unreversible
@self.env.cr.postcommit.add
def export_attachment():
db_registry = registry(self.env.cr.dbname)
with db_registry.cursor() as cr:
context = self.env.context
uid = self.env.uid

env = api.Environment(cr, uid, context)
order = self.with_env(env)

attachment = order._get_payment_attachment_to_export()
content = base64.b64decode(attachment.datas)
try:
order._export_to_storage(content, attachment.name)
except UserError:

Check warning on line 80 in account_payment_method_or_mode_fs_storage/models/account_payment_order.py

View check run for this annotation

Codecov / codecov/patch

account_payment_method_or_mode_fs_storage/models/account_payment_order.py#L80

Added line #L80 was not covered by tests
# user error is raised if export failed
self.action_uploaded_cancel()
raise

Check warning on line 83 in account_payment_method_or_mode_fs_storage/models/account_payment_order.py

View check run for this annotation

Codecov / codecov/patch

account_payment_method_or_mode_fs_storage/models/account_payment_order.py#L82-L83

Added lines #L82 - L83 were not covered by tests

action = {
"type": "ir.actions.client",
"tag": "display_notification",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def test_payment_method_fs_storage(self):
order.draft2open()
with self.with_custom_method():
action = order.open2generated()
# commit need to be performed in order to perform the 'postcommit'
self.env.cr.commit() # pylint: disable=E8102

self.assertDictEqual(
action,
Expand All @@ -126,6 +128,15 @@ def test_payment_method_fs_storage(self):
)
self.assertEqual(len(attachment), 1)

# clean up
order.action_cancel()
order.unlink()

self.env.cr.commit() # pylint: disable=E8102
# because we committed the cursor, the savepoint of the test method is
# gone, and this would break TransactionCase cleanups
self.cr.execute("SAVEPOINT test_%d" % self._savepoint_id)

def test_check_use_on_payment_method(self):
self.env.user.company_id = self.company.id
self.company.update(
Expand Down Expand Up @@ -185,6 +196,8 @@ def test_method_fs_storage_other_company(self):
order.draft2open()
with self.with_custom_method():
action = order.open2generated()
# commit need to be performed in order to perform the 'postcommit'
self.env.cr.commit() # pylint: disable=E8102

self.assertDictEqual(
action,
Expand All @@ -208,6 +221,15 @@ def test_method_fs_storage_other_company(self):
)
self.assertEqual(len(attachment), 1)

# clean up
order.action_cancel()
order.unlink()

self.env.cr.commit() # pylint: disable=E8102
# because we committed the cursor, the savepoint of the test method is
# gone, and this would break TransactionCase cleanups
self.cr.execute("SAVEPOINT test_%d" % self._savepoint_id)

def test_payment_mode_fs_storage(self):
self.env.user.company_id = self.company.id
self.company.update(
Expand Down Expand Up @@ -243,6 +265,8 @@ def test_payment_mode_fs_storage(self):
order.draft2open()
with self.with_custom_method():
action = order.open2generated()
# commit need to be performed in order to perform the 'postcommit'
self.env.cr.commit() # pylint: disable=E8102

self.assertDictEqual(
action,
Expand All @@ -266,6 +290,15 @@ def test_payment_mode_fs_storage(self):
)
self.assertEqual(len(attachment), 1)

# clean up
order.action_cancel()
order.unlink()

self.env.cr.commit() # pylint: disable=E8102
# because we committed the cursor, the savepoint of the test method is
# gone, and this would break TransactionCase cleanups
self.cr.execute("SAVEPOINT test_%d" % self._savepoint_id)

def test_check_use_on_payment_mode(self):
self.env.user.company_id = self.company.id
self.company.update(
Expand Down

0 comments on commit 473d250

Please sign in to comment.