-
-
Notifications
You must be signed in to change notification settings - Fork 49
[18.0][IMP] edi_core_oca: add listener action #232
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: 18.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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -67,6 +67,12 @@ class EdiConfiguration(models.Model): | |||||
| help="""Used to do something specific here. | ||||||
| Receives: operation, edi_action, vals, old_vals.""", | ||||||
| ) | ||||||
| is_global = fields.Boolean( | ||||||
| string="Global Configuration", | ||||||
| help="If checked, this configuration will be executed for all records, " | ||||||
| "regardless of the partner.", | ||||||
|
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
|
||||||
| default=False, | ||||||
| ) | ||||||
|
|
||||||
| @api.constrains("backend_id", "type_id") | ||||||
| def _constrains_backend(self): | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <odoo noupdate="1"> | ||
| <record id="edi_config_trigger_storage_done" model="edi.configuration.trigger"> | ||
| <field name="name">On record exchange done</field> | ||
|
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. aren't these triggers generic enough to stay in the core module? 🤔 |
||
| <field name="code">on_edi_exchange_done</field> | ||
| <field name="description">Trigger when a record exchange is done</field> | ||
| </record> | ||
| <record id="edi_config_trigger_storage_error" model="edi.configuration.trigger"> | ||
| <field name="name">On record exchange error</field> | ||
| <field name="code">on_edi_exchange_error</field> | ||
| <field name="description">Trigger when a record exchange has an error</field> | ||
| </record> | ||
| <record id="edi_config_storage_done" model="edi.configuration"> | ||
| <field name="name">Storage Move File on Done</field> | ||
| <field name="is_global" eval="True" /> | ||
| <field name="trigger_id" ref="edi_config_trigger_storage_done" /> | ||
| <field name="snippet_do">record.on_edi_exchange_done()</field> | ||
| </record> | ||
| <record id="edi_config_storage_error" model="edi.configuration"> | ||
| <field name="name">Storage Move File on Error</field> | ||
| <field name="is_global" eval="True" /> | ||
| <field name="trigger_id" ref="edi_config_trigger_storage_error" /> | ||
| <field name="snippet_do">record.on_edi_exchange_error()</field> | ||
| </record> | ||
| </odoo> | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,8 +1,14 @@ | ||||||
| # Copyright 2024 Camptocamp SA | ||||||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||||||
|
|
||||||
| import functools | ||||||
| import os | ||||||
| from pathlib import PurePath | ||||||
|
|
||||||
| from odoo import fields, models | ||||||
|
|
||||||
| from .. import utils | ||||||
|
|
||||||
|
|
||||||
| class EDIExchangeRecord(models.Model): | ||||||
| _inherit = "edi.exchange.record" | ||||||
|
|
@@ -13,3 +19,67 @@ class EDIExchangeRecord(models.Model): | |||||
| string="FS Storage", | ||||||
| help="Record created from a file found in this FS storage", | ||||||
| ) | ||||||
|
|
||||||
| def _move_file(self, storage, from_dir_str, to_dir_str, filename): | ||||||
|
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. I would move this to fs_storage as we are dragging this from project to project, module to module |
||||||
| from_dir = PurePath(from_dir_str) | ||||||
| to_dir = PurePath(to_dir_str) | ||||||
| # - storage.list_files now includes path in fs_storage, breaking change | ||||||
| # - we remove path | ||||||
| files = utils.list_files(storage, from_dir.as_posix()) | ||||||
| files = [os.path.basename(f) for f in files] | ||||||
| if filename not in files: | ||||||
| return False | ||||||
| self._add_post_commit_hook( | ||||||
| utils.move_files, | ||||||
| storage, | ||||||
| [(from_dir / filename).as_posix()], | ||||||
| to_dir.as_posix(), | ||||||
| ) | ||||||
| return True | ||||||
|
|
||||||
| def _add_post_commit_hook( | ||||||
| self, move_func, storage, sftp_filepath, sftp_destination_path | ||||||
| ): | ||||||
| """Add hook after commit to move the file when transaction is over.""" | ||||||
| self.env.cr.postcommit.add( | ||||||
| functools.partial(move_func, storage, sftp_filepath, sftp_destination_path) | ||||||
| ) | ||||||
|
|
||||||
| def on_edi_exchange_done(self): | ||||||
|
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
|
||||||
| storage = self.storage_id | ||||||
| res = False | ||||||
| if self.direction == "input" and storage: | ||||||
| file = self.exchange_filename | ||||||
| pending_dir = self.type_id._storage_fullpath( | ||||||
| self.backend_id.input_dir_pending | ||||||
| ).as_posix() | ||||||
| done_dir = self.type_id._storage_fullpath( | ||||||
| self.backend_id.input_dir_done | ||||||
| ).as_posix() | ||||||
| error_dir = self.type_id._storage_fullpath( | ||||||
| self.backend_id.input_dir_error | ||||||
| ).as_posix() | ||||||
| if not done_dir: | ||||||
| return res | ||||||
| res = self._move_file(storage, pending_dir, done_dir, file) | ||||||
| if not res: | ||||||
| # If a file previously failed it should have been previously | ||||||
| # moved to the error dir, therefore it is not present in the | ||||||
| # pending dir and we need to retry from error dir. | ||||||
| res = self._move_file(storage, error_dir, done_dir, file) | ||||||
| return res | ||||||
|
|
||||||
| def on_edi_exchange_error(self): | ||||||
|
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
I'm not a fan of adding this custom logic to the record, having it on the component was better.
Author
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. Yes, I agree with you, I think it's much cleaner to keep using components and move that logic into a glue module, as I did in my first approach.
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. we can, but at least we should prefix w/ proper naming (eg: @etobella your POV?
Author
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. @etobella friendly reminder!
Member
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 we need to add a component, I prefer a glue module. I know it is not the best solution, but it is better to avoid dependancies that are not needed. About the naming, edi_storage_event or something like that looks fine for me. |
||||||
| storage = self.storage_id | ||||||
| res = False | ||||||
| if self.direction == "input" and storage: | ||||||
| file = self.exchange_filename | ||||||
| pending_dir = self.type_id._storage_fullpath( | ||||||
| self.backend_id.input_dir_pending | ||||||
| ).as_posix() | ||||||
| error_dir = self.type_id._storage_fullpath( | ||||||
| self.backend_id.input_dir_error | ||||||
| ).as_posix() | ||||||
| if error_dir: | ||||||
| res = self._move_file(storage, pending_dir, error_dir, file) | ||||||
| return res | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| from . import test_edi_backend_storage | ||
| from . import test_exchange_type | ||
| from . import test_edi_event_listenner |
Uh oh!
There was an error while loading. Please reload this page.