Skip to content

Commit ac8af3e

Browse files
etobellaHviorForgeFlow
authored andcommitted
[BPRT] edi: Backport from 14.0
1 parent 3b49484 commit ac8af3e

16 files changed

+119
-94
lines changed

edi_oca/__manifest__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Define backends, exchange types, exchange records,
1010
basic automation and views for handling EDI exchanges.
1111
""",
12-
"version": "14.0.1.20.1",
12+
"version": "12.0.1.20.1",
1313
"website": "https://github.com/OCA/edi",
1414
"development_status": "Beta",
1515
"license": "LGPL-3",
@@ -22,7 +22,7 @@
2222
"base_sparse_field",
2323
"queue_job",
2424
],
25-
"external_dependencies": {"python": ["pyyaml"]},
25+
"external_dependencies": {"python": ["yaml"]},
2626
"data": [
2727
"wizards/edi_exchange_record_create_wiz.xml",
2828
"data/cron.xml",

edi_oca/models/edi_backend.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def _create_record_prepare_values(self, type_code, values):
174174
exchange_type = self.env["edi.exchange.type"].search(
175175
self._get_exchange_type_domain(type_code), limit=1
176176
)
177-
assert exchange_type, f"Exchange type not found: {type_code}"
177+
assert exchange_type, "Exchange type not found: {}".format(type_code)
178178
res["type_id"] = exchange_type.id
179179
res["backend_id"] = self.id
180180
return res
@@ -473,9 +473,6 @@ def exchange_process(self, exchange_record):
473473
{
474474
"edi_exchange_state": state,
475475
"exchange_error": error,
476-
# FIXME: this should come from _compute_exchanged_on
477-
# but somehow it's failing in send tests (in record tests it works).
478-
"exchanged_on": fields.Datetime.now(),
479476
}
480477
)
481478
if state == "input_processed_error":
@@ -621,8 +618,7 @@ def _find_existing_exchange_records(
621618
return self.env["edi.exchange.record"].search(domain, count=count_only)
622619

623620
def action_view_exchanges(self):
624-
xmlid = "edi_oca.act_open_edi_exchange_record_view"
625-
action = self.env["ir.actions.act_window"]._for_xml_id(xmlid)
621+
action = self.env.ref("edi_oca.act_open_edi_exchange_record_view")
626622
action["context"] = {
627623
"search_default_backend_id": self.id,
628624
"default_backend_id": self.id,
@@ -631,8 +627,7 @@ def action_view_exchanges(self):
631627
return action
632628

633629
def action_view_exchange_types(self):
634-
xmlid = "edi_oca.act_open_edi_exchange_type_view"
635-
action = self.env["ir.actions.act_window"]._for_xml_id(xmlid)
630+
action = self.env.ref("edi_oca.act_open_edi_exchange_type_view")
636631
action["context"] = {
637632
"search_default_backend_id": self.id,
638633
"default_backend_id": self.id,

edi_oca/models/edi_backend_type.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ def _onchange_code(self):
3131
def _inverse_code(self):
3232
for rec in self:
3333
# Make sure it's always normalized
34-
rec.code = normalize_string(rec.code)
34+
code = normalize_string(rec.code)
35+
if code != rec.code:
36+
rec.code = code

edi_oca/models/edi_exchange_consumer_mixin.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class EDIExchangeConsumerMixin(models.AbstractModel):
3636
exchange_record_ids = fields.One2many(
3737
"edi.exchange.record",
3838
inverse_name="res_id",
39+
prefetch=False,
3940
domain=lambda r: [("model", "=", r._name)],
4041
)
4142
exchange_record_count = fields.Integer(compute="_compute_exchange_record_count")
@@ -80,7 +81,7 @@ def _edi_get_exchange_type_config(self):
8081
if not eval_ctx.get("result", False):
8182
continue
8283

83-
result[rule.id] = self._edi_get_exchange_type_rule_conf(rule)
84+
result[str(rule.id)] = self._edi_get_exchange_type_rule_conf(rule)
8485
return result
8586

8687
@api.model
@@ -181,8 +182,7 @@ def edi_create_exchange_record(self, exchange_type_id):
181182
return self._edi_get_create_record_wiz_action(exchange_type_id)
182183

183184
def _edi_get_create_record_wiz_action(self, exchange_type_id):
184-
xmlid = "edi_oca.edi_exchange_record_create_act_window"
185-
action = self.env["ir.actions.act_window"]._for_xml_id(xmlid)
185+
action = self.env.ref("edi_oca.edi_exchange_record_create_act_window")
186186
action["context"] = {
187187
"default_res_id": self.id,
188188
"default_model": self._name,
@@ -241,8 +241,7 @@ def _compute_exchange_record_count(self):
241241

242242
def action_view_edi_records(self):
243243
self.ensure_one()
244-
xmlid = "edi_oca.act_open_edi_exchange_record_view"
245-
action = self.env["ir.actions.act_window"]._for_xml_id(xmlid)
244+
action = self.env.ref("edi_oca.act_open_edi_exchange_record_view")
246245
action["domain"] = [("model", "=", self._name), ("res_id", "=", self.id)]
247246
# Purge default search filters from ctx to avoid hiding records
248247
ctx = action.get("context", {})

edi_oca/models/edi_exchange_record.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from collections import defaultdict
99

1010
from odoo import _, api, exceptions, fields, models
11+
from odoo import SUPERUSER_ID
1112

1213
_logger = logging.getLogger(__name__)
1314

@@ -36,13 +37,11 @@ class EDIExchangeRecord(models.Model):
3637
direction = fields.Selection(related="type_id.direction")
3738
backend_id = fields.Many2one(comodel_name="edi.backend", required=True)
3839
model = fields.Char(index=True, required=False, readonly=True)
39-
res_id = fields.Many2oneReference(
40-
string="Record",
40+
res_id = fields.Integer(
41+
string="Record ID",
4142
index=True,
4243
required=False,
4344
readonly=True,
44-
model_field="model",
45-
copy=False,
4645
)
4746
related_record_exists = fields.Boolean(compute="_compute_related_record_exists")
4847
related_name = fields.Char(compute="_compute_related_name", compute_sudo=True)
@@ -53,8 +52,6 @@ class EDIExchangeRecord(models.Model):
5352
exchanged_on = fields.Datetime(
5453
string="Exchanged on",
5554
help="Sent or received on this date.",
56-
compute="_compute_exchanged_on",
57-
store=True,
5855
readonly=False,
5956
)
6057
edi_exchange_state = fields.Selection(
@@ -125,15 +122,15 @@ def _compute_related_name(self):
125122
related_record = rec.record
126123
rec.related_name = related_record.display_name if related_record else ""
127124

128-
@api.depends("model", "type_id")
125+
@api.depends("model", "type_id", "res_id")
129126
def _compute_exchange_filename(self):
130127
for rec in self:
131128
if not rec.type_id:
132129
continue
133130
if not rec.exchange_filename:
134131
rec.exchange_filename = rec.type_id._make_exchange_filename(rec)
135132

136-
@api.depends("edi_exchange_state")
133+
@api.constrains("edi_exchange_state")
137134
def _compute_exchanged_on(self):
138135
for rec in self:
139136
if rec.edi_exchange_state in ("input_received", "output_sent"):
@@ -355,8 +352,7 @@ def action_open_related_exchanges(self):
355352
self.ensure_one()
356353
if not self.related_exchange_ids:
357354
return {}
358-
xmlid = "edi_oca.act_open_edi_exchange_record_view"
359-
action = self.env["ir.actions.act_window"]._for_xml_id(xmlid)
355+
action = self.env.ref("edi_oca.act_open_edi_exchange_record_view")
360356
action["domain"] = [("id", "in", self.related_exchange_ids.ids)]
361357
return action
362358

@@ -372,7 +368,7 @@ def notify_action_complete(self, action, message=None):
372368
self._notify_related_record(message)
373369

374370
# Trigger generic action complete event on exchange record
375-
event_name = f"{action}_complete"
371+
event_name = "{}_complete".format(action)
376372
self._trigger_edi_event(event_name)
377373
if self.related_record_exists:
378374
# Trigger specific event on related record
@@ -453,8 +449,8 @@ def _search(
453449
count=False,
454450
access_rights_uid=access_rights_uid,
455451
)
456-
if self.env.is_system():
457-
# restrictions do not apply to group "Settings"
452+
if self._uid == SUPERUSER_ID:
453+
# rules do not apply for the superuser
458454
return len(ids) if count else ids
459455

460456
# TODO highlight orphaned EDI records in UI:
@@ -531,7 +527,7 @@ def check_access_rule(self, operation):
531527
"""In order to check if we can access a record, we are checking if we can access
532528
the related document"""
533529
super(EDIExchangeRecord, self).check_access_rule(operation)
534-
if self.env.is_superuser():
530+
if self._uid == SUPERUSER_ID:
535531
return
536532
default_checker = self.env["edi.exchange.consumer.mixin"].get_edi_access
537533
by_model_rec_ids = defaultdict(set)
@@ -546,7 +542,7 @@ def check_access_rule(self, operation):
546542
)
547543

548544
for model, rec_ids in by_model_rec_ids.items():
549-
records = self.env[model].browse(rec_ids).with_user(self._uid)
545+
records = self.env[model].browse(rec_ids).sudo(self._uid)
550546
checker = by_model_checker[model]
551547
for record in records:
552548
check_operation = checker(

edi_oca/tests/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def _setup_records(cls):
4949
code="test_csv_output_ack",
5050
direction="output",
5151
exchange_file_ext="txt",
52-
exchange_filename_pattern="{record.ref}-{type.code}-{dt}",
52+
exchange_filename_pattern="{record.name}-{type.code}-{dt}",
5353
)
5454
cls.exchange_type_out.ack_type_id = cls.exchange_type_out_ack
5555
cls.partner = cls.env.ref("base.res_partner_1")

edi_oca/tests/fake_models.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class EdiExchangeConsumerTest(models.Model):
1111
_description = "Model used only for test"
1212

1313
name = fields.Char()
14+
ref = fields.Char()
1415

1516
def _get_edi_exchange_record_name(self, exchange_record):
1617
return self.id

edi_oca/tests/test_backend_process.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import base64
66

7-
from freezegun import freeze_time
8-
97
from odoo import fields
108
from odoo.exceptions import UserError
119
from odoo.tools import mute_logger
@@ -36,15 +34,16 @@ def setUp(self):
3634

3735
def test_process_record(self):
3836
self.record.write({"edi_exchange_state": "input_received"})
39-
with freeze_time("2020-10-22 10:00:00"):
40-
self.record.action_exchange_process()
37+
now = fields.Datetime.now()
38+
self.record.action_exchange_process()
4139
self.assertTrue(FakeInputProcess.check_called_for(self.record))
4240
self.assertRecordValues(
4341
self.record, [{"edi_exchange_state": "input_processed"}]
4442
)
45-
self.assertEqual(
46-
fields.Datetime.to_string(self.record.exchanged_on), "2020-10-22 10:00:00"
47-
)
43+
self.record.refresh()
44+
self.assertAlmostEqual(
45+
(self.record.exchanged_on - now).total_seconds(),
46+
0, places=2)
4847

4948
def test_process_record_with_error(self):
5049
self.record.write({"edi_exchange_state": "input_received"})

0 commit comments

Comments
 (0)