-
-
Notifications
You must be signed in to change notification settings - Fork 57
[IMP] edi_core_oca: Improve result message #306
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
base: 19.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| from odoo.exceptions import UserError | ||
|
|
||
| from ..exceptions import EDINotImplementedError, EDIValidationError | ||
| from ..utils import EdiExchangeActionResult | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -126,8 +127,12 @@ def exchange_generate(self, exchange_record, store=True, force=False, **kw): | |
| # 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 | ||
| action_result = self._ensure_action_result( | ||
| self._exchange_generate(exchange_record, **kw), | ||
| default_message=exchange_record._exchange_status_message("generate_ok"), | ||
| ) | ||
| output = action_result.output | ||
| message = action_result.message | ||
| encoding = exchange_record.type_id.encoding or "UTF-8" | ||
| encoding_error_handler = ( | ||
| exchange_record.type_id.encoding_out_error_handler or "strict" | ||
|
|
@@ -142,7 +147,6 @@ def exchange_generate(self, exchange_record, store=True, force=False, **kw): | |
| } | ||
| ) | ||
| if output: | ||
| message = exchange_record._exchange_status_message("generate_ok") | ||
| try: | ||
| with self.env.cr.savepoint(): | ||
| self._validate_data(exchange_record, output) | ||
|
|
@@ -203,7 +207,16 @@ def _check_exchange_generate(self, exchange_record, force=False): | |
| def _exchange_generate(self, exchange_record, **kw): | ||
| exchange_function = self._get_exec_handler(exchange_record, "generate") | ||
| ctx = self._get_record_env_ctx(exchange_record, "generate") | ||
| return exchange_function(exchange_record.with_context(**ctx), **kw) | ||
| result = exchange_function(exchange_record.with_context(**ctx), **kw) | ||
| return self._ensure_action_result( | ||
| result, | ||
| default_message=exchange_record._exchange_status_message("generate_ok"), | ||
| ) | ||
|
|
||
| def _ensure_action_result(self, result, default_message=None): | ||
| if isinstance(result, EdiExchangeActionResult): | ||
| return result | ||
| return EdiExchangeActionResult(output=result, message=default_message) | ||
|
|
||
| # TODO: add tests | ||
| def _validate_data(self, exchange_record, value=None, **kw): | ||
|
|
@@ -244,7 +257,9 @@ def exchange_send(self, exchange_record): | |
| res = "" | ||
| try: | ||
| with self.env.cr.savepoint(): | ||
| self._exchange_send(exchange_record) | ||
| send_result = self._ensure_action_result( | ||
| self._exchange_send(exchange_record) | ||
| ) | ||
| _logger.debug("%s sent", exchange_record.identifier) | ||
| except self._send_retryable_exceptions() as err: | ||
| traceback = _get_exception_traceback() | ||
|
|
@@ -269,15 +284,18 @@ def exchange_send(self, exchange_record): | |
| res = "__sql_error__" | ||
| raise | ||
| else: | ||
| # TODO: maybe the send handler should return desired message and state | ||
| message = exchange_record._exchange_status_message("send_ok") | ||
| message = ( | ||
| send_result.message | ||
| or send_result.output | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you want the output as a message you must return it as the message |
||
| or exchange_record._exchange_status_message("send_ok") | ||
| ) | ||
| error = traceback = None | ||
| state = ( | ||
| "output_sent_and_processed" | ||
| if self.output_sent_processed_auto | ||
| else "output_sent" | ||
| ) | ||
| res = message | ||
| res = send_result.output or message | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. read above |
||
| finally: | ||
| if res != "__sql_error__": | ||
| exchange_record.write( | ||
|
|
@@ -329,7 +347,8 @@ def _output_check_send(self, exchange_record): | |
| def _exchange_send(self, exchange_record): | ||
| exchange_function = self._get_exec_handler(exchange_record, "send") | ||
| ctx = self._get_record_env_ctx(exchange_record, "send") | ||
| return exchange_function(exchange_record.with_context(**ctx)) | ||
| result = exchange_function(exchange_record.with_context(**ctx)) | ||
| return self._ensure_action_result(result) | ||
|
|
||
| def _cron_check_output_exchange_sync(self, **kw): | ||
| for backend in self: | ||
|
|
@@ -475,7 +494,9 @@ def exchange_process(self, exchange_record): | |
| res = None | ||
| try: | ||
| with self.env.cr.savepoint(): | ||
| res = self._exchange_process(exchange_record) | ||
| process_result = self._exchange_process(exchange_record) | ||
| res = process_result.output | ||
| message = process_result.message | ||
| except self._swallable_exceptions() as err: | ||
| if self.env.context.get("_edi_process_break_on_error"): | ||
| raise | ||
|
|
@@ -517,7 +538,8 @@ def exchange_process(self, exchange_record): | |
| def _exchange_process(self, exchange_record): | ||
| exchange_function = self._get_exec_handler(exchange_record, "process") | ||
| ctx = self._get_record_env_ctx(exchange_record, "process") | ||
| return exchange_function(exchange_record.with_context(**ctx)) | ||
| result = exchange_function(exchange_record.with_context(**ctx)) | ||
| return self._ensure_action_result(result) | ||
|
|
||
| def exchange_receive(self, exchange_record): | ||
| """Retrieve an incoming document.""" | ||
|
|
@@ -533,7 +555,11 @@ def exchange_receive(self, exchange_record): | |
| res = None | ||
| try: | ||
| with self.env.cr.savepoint(): | ||
| content = self._exchange_receive(exchange_record) | ||
| receive_result = self._ensure_action_result( | ||
| self._exchange_receive(exchange_record) | ||
| ) | ||
| content = receive_result.output | ||
| message = receive_result.message | ||
| # Ignore result of FileNotFoundError/OSError | ||
| if content is not None: | ||
| exchange_record._set_file_content(content) | ||
|
|
@@ -556,7 +582,7 @@ def exchange_receive(self, exchange_record): | |
| res = "__sql_error__" | ||
| raise | ||
| else: | ||
| message = exchange_record._exchange_status_message("receive_ok") | ||
| message = message or exchange_record._exchange_status_message("receive_ok") | ||
| error = traceback = None | ||
| state = "input_received" | ||
| res = message | ||
|
|
@@ -598,7 +624,8 @@ def _exchange_receive_check(self, exchange_record): | |
| def _exchange_receive(self, exchange_record): | ||
| exchange_function = self._get_exec_handler(exchange_record, "receive") | ||
| ctx = self._get_record_env_ctx(exchange_record, "receive") | ||
| return exchange_function(exchange_record.with_context(**ctx)) | ||
| result = exchange_function(exchange_record.with_context(**ctx)) | ||
| return self._ensure_action_result(result) | ||
|
|
||
| def _cron_check_input_exchange_sync(self, **kw): | ||
| for backend in self: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,14 @@ | |||||
| # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||||||
|
|
||||||
| import hashlib | ||||||
| from dataclasses import dataclass | ||||||
| from typing import Any | ||||||
|
|
||||||
|
|
||||||
| @dataclass | ||||||
| class EdiExchangeActionResult: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| output: Any = None | ||||||
| message: str | None = None | ||||||
|
|
||||||
|
|
||||||
| def normalize_string(cls, a_string, sep="_"): | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
each
_exchange_$actionshould take care of returning the wrapped result.Here you should not wrap.
Also I wouldn't pass
default_message.Just do
message = result.message or exchange_record._exchange_status_message("generate_ok")