Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][BKP] edi_oca: More commits #1011

Merged
merged 5 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions edi_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"data": [
"wizards/edi_exchange_record_create_wiz.xml",
"data/cron.xml",
"data/ir_actions_server.xml",
"data/sequence.xml",
"data/job_channel.xml",
"data/job_function.xml",
Expand Down
14 changes: 14 additions & 0 deletions edi_oca/data/ir_actions_server.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record model="ir.actions.server" id="action_retry_edi_exchange_record">
<field name="name">Retry</field>
<field name="groups_id" eval="[(4, ref('base_edi.group_edi_manager'))]" />
<field name="model_id" ref="model_edi_exchange_record" />
<field name="binding_model_id" ref="model_edi_exchange_record" />
<field name="state">code</field>
<field name="code">
if records:
action = records.action_retry()
</field>
</record>
</odoo>
22 changes: 18 additions & 4 deletions edi_oca/models/edi_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class EDIBackend(models.Model):
required=True,
ondelete="restrict",
)
backend_type_code = fields.Char(related="backend_type_id.code")
output_sent_processed_auto = fields.Boolean(
help="""
Automatically set the record as processed after sending.
Expand Down Expand Up @@ -207,10 +208,13 @@ def exchange_generate(self, exchange_record, store=True, force=False, **kw):

:param exchange_record: edi.exchange.record recordset
:param store: store output on the record itself
:param force: allow to re-genetate the content
:param force: allow to re-generate the content
:param kw: keyword args to be propagated to output generate handler
"""
self.ensure_one()
if force and exchange_record.exchange_file:
# Remove file to regenerate
exchange_record.exchange_file = False
self._check_exchange_generate(exchange_record, force=force)
output = self._exchange_generate(exchange_record, **kw)
message = None
Expand Down Expand Up @@ -286,6 +290,12 @@ def _exchange_generate(self, exchange_record, **kw):

# TODO: add tests
def _validate_data(self, exchange_record, value=None, **kw):
if exchange_record.direction == "input" and not exchange_record.exchange_file:
if not exchange_record.type_id.allow_empty_files_on_receive:
raise ValueError(
_("Empty files are not allowed for this exchange type")
)

component = self._get_component(exchange_record, "validate")
if component:
return component.validate(value)
Expand Down Expand Up @@ -471,7 +481,10 @@ def _exchange_process_check(self, exchange_record):
raise exceptions.UserError(
_("Record ID=%d is not meant to be processed") % exchange_record.id
)
if not exchange_record.exchange_file:
if (
not exchange_record.exchange_file
and not exchange_record.type_id.allow_empty_files_on_receive
):
raise exceptions.UserError(
_("Record ID=%d has no file to process!") % exchange_record.id
)
Expand Down Expand Up @@ -540,7 +553,8 @@ def exchange_receive(self, exchange_record):
content = None
try:
content = self._exchange_receive(exchange_record)
if content:
# Ignore result of FileNotFoundError/OSError
if content is not None:
exchange_record._set_file_content(content)
self._validate_data(exchange_record)
except EDIValidationError:
Expand Down Expand Up @@ -637,7 +651,7 @@ def _input_pending_records_domain(self, record_ids=None):
return domain

def _input_pending_process_records_domain(self, record_ids=None):
states = ("input_received", "input_processed_error")
states = ("input_received",)
domain = [
("backend_id", "=", self.id),
("type_id.direction", "=", "input"),
Expand Down
4 changes: 4 additions & 0 deletions edi_oca/models/edi_exchange_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ def _retry_exchange_action(self):
self._execute_next_action()
return True

def action_regenerate(self):
for rec in self:
rec.action_exchange_generate(force=True)

def action_open_related_record(self):
self.ensure_one()
if not self.related_record_exists:
Expand Down
1 change: 1 addition & 0 deletions edi_oca/models/edi_exchange_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class EDIExchangeType(models.Model):
help="Handling of decoding errors on process "
"(default is always 'Raise Error').",
)
allow_empty_files_on_receive = fields.Boolean(string="Allow Empty Files")

_sql_constraints = [
(
Expand Down
23 changes: 23 additions & 0 deletions edi_oca/tests/test_backend_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,26 @@ def test_receive_record(self):
self.backend.with_context(fake_output="yeah!").exchange_receive(self.record)
self.assertEqual(self.record._get_file_content(), "yeah!")
self.assertRecordValues(self.record, [{"edi_exchange_state": "input_received"}])

def test_receive_no_allow_empty_file_record(self):
self.record.edi_exchange_state = "input_pending"
self.backend.with_context(
fake_output="", _edi_receive_break_on_error=False
).exchange_receive(self.record)
# Check the record
msg = "Empty files are not allowed for this exchange type"
self.assertIn(msg, self.record.exchange_error)
self.assertEqual(self.record._get_file_content(), "")
self.assertRecordValues(
self.record, [{"edi_exchange_state": "input_receive_error"}]
)

def test_receive_allow_empty_file_record(self):
self.record.edi_exchange_state = "input_pending"
self.record.type_id.allow_empty_files_on_receive = True
self.backend.with_context(
fake_output="", _edi_receive_break_on_error=False
).exchange_receive(self.record)
# Check the record
self.assertEqual(self.record._get_file_content(), "")
self.assertRecordValues(self.record, [{"edi_exchange_state": "input_received"}])
21 changes: 21 additions & 0 deletions edi_oca/tests/test_backend_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,24 @@ def test_input(self):
job = self.backend.with_delay().exchange_process(record)
created = job_counter.search_created()
self.assertEqual(created[0].name, "Process an incoming document.")

def test_input_processed_error(self):
vals = {
"model": self.partner._name,
"res_id": self.partner.id,
"edi_exchange_state": "input_received",
}
record = self.backend.create_record("test_csv_input", vals)
record._set_file_content("ABC")
# Process `input_received` records
job_counter = self.job_counter()
self.backend._check_input_exchange_sync()
created = job_counter.search_created()
# Create job
self.assertEqual(len(created), 1)
record.edi_exchange_state = "input_processed_error"
# Don't re-process `input_processed_error` records
self.backend._check_input_exchange_sync()
new_created = job_counter.search_created() - created
# Should not create new job
self.assertEqual(len(new_created), 0)
9 changes: 9 additions & 0 deletions edi_oca/tests/test_backend_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,18 @@ def test_process_no_file_record(self):
self.record.write({"edi_exchange_state": "input_received"})
self.record._onchange_edi_exchange_state()
self.record.exchange_file = False
self.exchange_type_in.allow_empty_files_on_receive = False
with self.assertRaises(UserError):
self.record.action_exchange_process()

@mute_logger("odoo.models.unlink")
def test_process_allow_no_file_record(self):
self.record.write({"edi_exchange_state": "input_received"})
self.record.exchange_file = False
self.exchange_type_in.allow_empty_files_on_receive = True
self.record.action_exchange_process()
self.assertEqual(self.record.edi_exchange_state, "input_processed")

def test_process_outbound_record(self):
vals = {
"model": self.partner._name,
Expand Down
25 changes: 25 additions & 0 deletions edi_oca/tests/test_backend_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,28 @@ def test_generate_validate_record_error(self):
],
)
self.assertIn("Data seems wrong!", self.record_out.exchange_error)

def test_validate_record_error_regenerate(self):
self.record_out.write({"edi_exchange_state": "new"})
exc = EDIValidationError("Data seems wrong!")
self.backend.with_context(test_break_validate=exc).exchange_generate(
self.record_out
)
self.assertRecordValues(
self.record_out,
[
{
"edi_exchange_state": "validate_error",
}
],
)
self.record_out.with_context(fake_output="yeah!").action_regenerate()
self.assertEqual(self.record_out._get_file_content(), "yeah!")
self.assertRecordValues(
self.record_out,
[
{
"edi_exchange_state": "output_pending",
}
],
)
2 changes: 2 additions & 0 deletions edi_oca/views/edi_backend_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
</h2>
</div>
<group>
<!-- can be used to hide elements based on the backend type, e.g. notebook pages -->
<field name="backend_type_code" invisible="1" />
<field name="output_sent_processed_auto" />
<field name="active" invisible="1" />
</group>
Expand Down
6 changes: 6 additions & 0 deletions edi_oca/views/edi_exchange_record_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
string="Retry"
attrs="{'invisible': [('retryable', '=', False)]}"
/>
<button
name="action_regenerate"
type="object"
string="Regenerate"
attrs="{'invisible': ['|', ('direction', 'in', ('input', False)), ('edi_exchange_state', '!=', 'validate_error')]}"
/>
<!-- FIXME: colors are ignored...
and can't find how they are supposed to work.
Probably not supported at all. -->
Expand Down
4 changes: 4 additions & 0 deletions edi_oca/views/edi_exchange_type_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
name="encoding_in_error_handler"
attrs="{'invisible': [('direction', '=', 'output')]}"
/>
<field
name="allow_empty_files_on_receive"
attrs="{'invisible': [('direction', '=', 'output')]}"
/>
</group>
</group>
<field name="deprecated_rule_fields_still_used" invisible="1" />
Expand Down
Loading