Skip to content

Commit

Permalink
[IMP] edi_oca: Prevent duplication of type code in exchange filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
QuocDuong1306 committed Aug 29, 2024
1 parent e9fe1a7 commit e9a26ed
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion edi_oca/models/edi_exchange_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ def name_get(self):
rec_name = rec.identifier
if rec.res_id and rec.model:
rec_name = rec.record.display_name
name = "[{}] {}".format(rec.type_id.name, rec_name)
name = (
"[{}] {}".format(rec.type_id.name, rec_name)
if rec._context.get("include_exchange_type_name", True)
else rec_name
)
result.append((rec.id, name))
return result

Expand Down
9 changes: 9 additions & 0 deletions edi_oca/models/edi_exchange_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ def _make_exchange_filename(self, exchange_record):
ext = self.exchange_file_ext
pattern = pattern + ".{ext}"
dt = self._make_exchange_filename_datetime()
# Avoid duplicating type code in filename if not having related record
if (
"{type.code}" in pattern
and slugify(self.code) == slugify(self.name)
and (not exchange_record.res_id or not exchange_record.model)
):
exchange_record = exchange_record.with_context(
include_exchange_type_name=False
)
record_name = self._get_record_name(exchange_record)
record = exchange_record
if exchange_record.model and exchange_record.res_id:
Expand Down
19 changes: 19 additions & 0 deletions edi_oca/tests/test_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from odoo.tools import mute_logger

from odoo.addons.edi_oca.utils import get_checksum
from odoo.addons.http_routing.models.ir_http import slugify
from odoo.addons.queue_job.delay import DelayableRecordset

from .common import EDIBackendCommonTestCase
Expand Down Expand Up @@ -77,6 +78,24 @@ def test_record_exchange_date(self):
fields.Datetime.to_string(record.exchanged_on), "2020-10-21 10:00:00"
)

def test_record_exchange_filename(self):
# Using pattern with `record_name`
self.exchange_type_out.exchange_filename_pattern = (
"{record_name}-{type.code}-{dt}"
)
vals = {
"edi_exchange_state": "new",
}
record = self.backend.create_record("test_csv_output", vals)
self.assertFalse(record.exchanged_on)
with freeze_time("2020-10-21 10:00:00"):
# Should not have a duplicate of exc type code in filename
identifier = slugify(record.identifier)
self.assertEqual(
record.exchange_filename,
f"{identifier}-test_csv_output-2020-10-21-12-00-00.csv",
)

@mute_logger("odoo.models.unlink")
def test_record_relation(self):
# create new one to delete it later
Expand Down

0 comments on commit e9a26ed

Please sign in to comment.