From 3998733ba1a4a764902c61dd131d0d48be127eef Mon Sep 17 00:00:00 2001 From: Florent Xicluna Date: Tue, 18 Jun 2024 13:40:33 +0200 Subject: [PATCH] [IMP] product_import: support company-specific vendors --- product_import/tests/common.py | 5 ++++- product_import/tests/test_product_import.py | 23 ++++++++++++++------- product_import/wizard/product_import.py | 7 +++---- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/product_import/tests/common.py b/product_import/tests/common.py index dac4cb1b48..eaf64ff7f8 100644 --- a/product_import/tests/common.py +++ b/product_import/tests/common.py @@ -12,7 +12,10 @@ def setUpClass(cls): super().setUpClass() cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) cls.wiz_model = cls.env["product.import"] - cls.supplier = cls.env["res.partner"].create({"name": "Catalogue Vendor"}) + cls.company = cls.env["res.company"].create({"name": "Customer ABC"}) + cls.supplier = cls.env["res.partner"].create( + {"name": "Catalogue Vendor", "company_id": cls.company.id} + ) def _mock(self, method_name): return mock.patch.object(type(self.wiz_model), method_name) diff --git a/product_import/tests/test_product_import.py b/product_import/tests/test_product_import.py index 408780a716..c261544dca 100644 --- a/product_import/tests/test_product_import.py +++ b/product_import/tests/test_product_import.py @@ -1,6 +1,7 @@ # Copyright 2022 Camptocamp SA # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo.exceptions import UserError from .common import TestCommon PARSED_CATALOG = { @@ -48,6 +49,7 @@ }, ], "ref": "1387", + "company": {"name": "Customer ABC"}, "seller": { "contact": False, "email": False, @@ -70,19 +72,24 @@ def setUpClass(cls): cls.parsed_catalog["chatter_msg"] = [] def test_get_seller(self): - seller = self.wiz_model._get_seller(self.parsed_catalog) + # Not found + with self.assertRaises(UserError): + seller = self.wiz_model._get_seller(self.parsed_catalog) + # Found + seller = self.wiz_model.with_company(self.company.id)._get_seller( + self.parsed_catalog + ) self.assertEqual(seller, self.supplier) def test_get_company_id(self): - # Test not found company_id - company_id = self.wiz_model._get_company_id(self.parsed_catalog) - self.assertIs(company_id, False) # Test found company_id - new_catalog = dict( - self.parsed_catalog, company={"name": "My Company (San Francisco)"} - ) + company_id = self.wiz_model._get_company_id(self.parsed_catalog) + self.assertEqual(company_id, self.company.id) + # Test not found company_id + new_catalog = dict(self.parsed_catalog) + del new_catalog["company"] company_id = self.wiz_model._get_company_id(new_catalog) - self.assertEqual(company_id, self.env.ref("base.main_company").id) + self.assertIs(company_id, False) def test_product_import(self): # product.product diff --git a/product_import/wizard/product_import.py b/product_import/wizard/product_import.py index ef89714b4c..89ecc13284 100644 --- a/product_import/wizard/product_import.py +++ b/product_import/wizard/product_import.py @@ -236,8 +236,7 @@ def import_button(self): if not catalogue.get("products"): raise UserError(_("This catalogue doesn't have any product!")) company_id = self._get_company_id(catalogue) - seller = self._get_seller(catalogue) - self.with_context(product_company_id=company_id)._create_products( - catalogue, seller, filename=self.product_filename - ) + wiz = self.with_company(company_id).with_context(product_company_id=company_id) + seller = wiz._get_seller(catalogue) + wiz._create_products(catalogue, seller, filename=self.product_filename) return {"type": "ir.actions.act_window_close"}