Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkhao committed Oct 17, 2023
1 parent d4b2719 commit 8d71108
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 41 deletions.
13 changes: 11 additions & 2 deletions wms_connector/data/ir_filters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@
<record model="ir.filters" id="default_wms_export_product_filter">
<field name="name">WMS: Default filter for products</field>
<field name="model_id">product.product</field>
<field name="domain">[
("wms_export_date", "=", False)
]</field>
</record>
<record model="ir.filters" id="default_wms_export_picking_in_filter">
<field name="name">WMS: Default filter for picking in</field>
<field name="model_id">stock.picking</field>
<field name="domain">[("picking_type_id.code", "=", "incoming")]</field>
<field name="domain">[
("wms_export_date", "=", False),
("picking_type_id.code", "=", "incoming")
]</field>
</record>
<record model="ir.filters" id="default_wms_export_picking_out_filter">
<field name="name">WMS: Default filter picking out</field>
<field name="model_id">stock.picking</field>
<field name="domain">[("picking_type_id.code", "=", "outgoing")]</field>
<field name="domain">[
("wms_export_date", "=", False),
("picking_type_id.code", "=", "outgoing")
]</field>
</record>

</odoo>
5 changes: 0 additions & 5 deletions wms_connector/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,3 @@
class StockPicking(models.Model):
_inherit = ["stock.picking", "synchronize.exportable.mixin"]
_name = "stock.picking"

def _get_export_name(self):
if self.file_creation_mode == "per_record":
return self.name + ".csv"
return super()._get_export_name()
22 changes: 9 additions & 13 deletions wms_connector/models/stock_warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"filetype": "export",
"name_fragment": "exports (products, awaiting receptions, preparation orders",
"code": "wh = env['stock.warehouse'].browse({0})\n"
'wh.{1}.scheduler_export("model",wh["taskfield"]._get_eval_domain())\n'
'wh.{1}.scheduler_export("model",wh["taskfield"]._get_eval_domain())\n'
'wh.{1}.scheduler_export("model",wh["taskfield"]._get_eval_domain())',
'wh.{1}.scheduler_export("wms.product.sync", wh["wms_export_product_filter_id"]._get_eval_domain())\n'
'wh.{1}.scheduler_export("stock.picking", wh["wms_export_picking_in_filter_id"]._get_eval_domain())\n'
'wh.{1}.scheduler_export("stock.picking", wh["wms_export_picking_out_filter_id"]._get_eval_domain())',
},
"reception": {
"fieldname_task": "wms_import_confirm_reception_task_id",
Expand Down Expand Up @@ -82,7 +82,7 @@ def _activate_crons_tasks(self):
"attachment.synchronize.task"
].create(
rec._prepare_wms_task_vals(
mappings["name_fragment"], mappings["filetype"]
mappings["filetype"], mappings["name_fragment"]
)
)
cron_field_name = mappings["fieldname_cron"]
Expand All @@ -91,31 +91,27 @@ def _activate_crons_tasks(self):
cron.active = True
else:
code = mappings["code"].format(self.id, task_field_name)
if kind == "export":
for el in MAPPINGS_FILTERS:
code = code.replace("taskfield", el[0], 1)
code = code.replace("model", el[1], 1)
rec[cron_field_name] = self.env["ir.cron"].create(
rec._prepare_wms_cron_vals(mappings["name_fragment"], code)
rec._prepare_wms_cron_vals(code, mappings["name_fragment"])
)
for field in [fieldname for fieldname, _ in MAPPINGS_FILTERS]:
if not getattr(rec, field):
rec[field] = self.env.ref(
"wms_connector.default_{}".format(field[:-3])
)

def _prepare_wms_task_vals(self, name, filetype):
def _prepare_wms_task_vals(self, filetype, name_fragment=""):
return {
"name": "WMS task for {} ({})".format(self.name, name),
"name": "WMS task for {} {}".format(self.name, name_fragment),
"method_type": "export",
"filepath": "OUT/",
"backend_id": self.env.ref("storage_backend.default_storage_backend").id,
"file_type": filetype,
}

def _prepare_wms_cron_vals(self, name, code=""):
def _prepare_wms_cron_vals(self, code="", name_fragment=""):
return {
"name": "WMS cron for {} ({})".format(self.name, name),
"name": "WMS cron for {} {}".format(self.name, name_fragment),
"active": False,
"interval_type": "days",
"interval_number": 1,
Expand Down
2 changes: 1 addition & 1 deletion wms_connector/models/synchronize_exportable_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from odoo import fields, models

DEBUGMODE = True
DEBUGMODE = False


class SynchronizeExportableMixin(models.AbstractModel):
Expand Down
5 changes: 2 additions & 3 deletions wms_connector/security/wms_product_sync.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

<odoo>

<record model="ir.model.access" id="wms_product_sync_access_name"> <!-- TODO acl id -->
<field name="name">wms.product.sync access name</field> <!-- TODO acl name -->
<record model="ir.model.access" id="wms_product_sync_access_name">
<field name="name">wms.product.sync access name</field>
<field name="model_id" ref="model_wms_product_sync"/>
<!-- TODO review and adapt -->
<field name="group_id" ref="base.group_user"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="1"/>
Expand Down
37 changes: 28 additions & 9 deletions wms_connector/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,55 @@
from odoo.tests.common import SavepointCase
import uuid
from odoo_test_helper import FakeModelLoader
from odoo.addons.component.tests.common import SavepointComponentCase
from odoo.addons.component.tests.common import ComponentMixin
from odoo.addons.attachment_synchronize.tests.common import SyncCommon
from odoo.addons.base.tests.common import SavepointCase
import datetime


class WmsConnectorCommon(SavepointComponentCase):
class WmsConnectorCommon(ComponentMixin, SavepointCase):
# pylint: disable=W8106
def setUp(self):
self.registry.enter_test_mode(self.env.cr)
super().setUp()
ComponentMixin.setUp(self)

def setAllExported(self):
self.env["stock.picking"].search([]).wms_export_date = datetime.date.today()
self.env["wms.product.sync"].search([]).wms_export_date = datetime.date.today()

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.setUpComponent()
cls.backend = cls.env.ref("wms_connector.demo_wms_backend")
cls.backend.directory_path = str(uuid.uuid1()) + "/"
cls.aq_before = cls.env["attachment.queue"].search([])
cls.warehouse = cls.env.ref("stock.warehouse0")
cls.loader = FakeModelLoader(cls.env, cls.__module__)
cls.loader.backup_registry()
from .model import WmsProductSync, StockPicking

from .model import WmsProductSync

cls.loader.update_registry((WmsProductSync,))
cls.loader.update_registry(
(
WmsProductSync,
StockPicking,
)
)

def tearDown(self):
super().tearDown()
files = self.backend.list_files("OUT/")
self.registry.leave_test_mode()
files = self.backend.list_files(self.backend.directory_path)
for f in files:
self.backend.delete("OUT/" + f)
self.backend.delete(self.backend.directory_path + f)

@classmethod
def tearDownClass(cls):
cls.loader.restore_registry()
super().tearDownClass()

def assertNewAttachmentQueue(self):
def assertNewAttachmentQueues(self, n=1):
aq_after = self.env["attachment.queue"].search([])
self.assertEqual(len(aq_after - self.env["attachment.queue"].search([])), 1)
self.assertEqual(len(aq_after - self.aq_before), n)
return aq_after
15 changes: 15 additions & 0 deletions wms_connector/tests/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@ def _prepare_export_data(self):

def _get_export_name(self):
return str(uuid.uuid4())


class StockPicking(models.Model):
_inherit = "stock.picking"

def _prepare_export_data(self):
return [
{
"name": rec.name,
}
for rec in self
]

def _get_export_name(self):
return str(uuid.uuid4())
4 changes: 2 additions & 2 deletions wms_connector/tests/test_activate_sync.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright 2023 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.tests.common import TransactionCase
from odoo.tests.common import SavepointCase


class TestActivateSync(TransactionCase):
class TestActivateSync(SavepointCase):
def setUp(self):
super().setUp()
self.warehouse = self.env.ref("stock.warehouse0")
Expand Down
40 changes: 34 additions & 6 deletions wms_connector/tests/test_export.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
# Copyright 2023 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.tests.common import TransactionCase
from .common import WmsConnectorCommon
import mock

FN_EXPORT_VALS = (
"odoo.addons.wms_connector.tests.model.WmsProductSync._prepare_export_data"
)


class TestExportFile(WmsConnectorCommon):
def setUp(self):
super().setUp()
self.warehouse.active_wms_sync = True
self.cron_export = self.warehouse.wms_export_cron_id
self.demo_product = self.env.ref("product.product_product_1")

def test_export_filter(self):
self.warehouse.refresh_wms_products()
self.setAllExported()
self.env["wms.product.sync"].search(
[("product_id", "=", self.demo_product.id)]
).wms_export_date = False
self.cron_export.method_direct_trigger()
self.assertNewAttachmentQueues(1)

# def test_run_export_cron(self):
# self.warehouse.wms_export_cron_id.method_direct_trigger()
# def test_export_error(self):
# self.setAllExported()
# self.demo_product.wms_export_date = False
# # with mock.patch(FN_EXPORT_VALS) as mocked:
# # # mocked.
# # mocked.side_effect = derp
# self.cron_export.method_direct_trigger()
# wms_product = self.env["wms.product.sync"].search(
# [("product_id", "=", self.demo_product.id)]
# )
# # self.assertIn(wms_product.wms_export_error, "ValueError")

def test_export_product(self):
def test_export_repeat(self):
self.warehouse.refresh_wms_products()
self.warehouse.wms_export_cron_id.method_direct_trigger()
self.assertNewAttachmentQueue()
self.cron_export.method_direct_trigger()
n_products = len(self.env["wms.product.sync"].search([]).ids)
n_pickings = len(self.env["stock.picking"].search([]).ids)
self.assertNewAttachmentQueues(n_pickings + n_products)
self.cron_export.method_direct_trigger()
self.assertNewAttachmentQueues(n_pickings + n_products)

0 comments on commit 8d71108

Please sign in to comment.