From a233c02233b5ed5229289cebed67350b921159db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Honor=C3=A9?= Date: Wed, 18 Sep 2019 15:46:41 +0200 Subject: [PATCH] Update after cherry-pick --- .../models/delivery_carrier.py | 10 ++-- shopinvader_delivery_carrier/services/cart.py | 47 ------------------- .../services/delivery_carrier.py | 42 +++++++++++++++-- shopinvader_delivery_carrier/tests/common.py | 1 + .../tests/test_carrier.py | 28 ++++++----- .../tests/test_delivery_carrier.py | 8 ++-- 6 files changed, 64 insertions(+), 72 deletions(-) diff --git a/shopinvader_delivery_carrier/models/delivery_carrier.py b/shopinvader_delivery_carrier/models/delivery_carrier.py index 7f9b0b35a1..0cd8453246 100644 --- a/shopinvader_delivery_carrier/models/delivery_carrier.py +++ b/shopinvader_delivery_carrier/models/delivery_carrier.py @@ -17,7 +17,7 @@ def _simulate_delivery_cost(self, partner): :param partner: res.partner recordset :return: """ - partner.read() + partner.read(["country_id", "zip"]) partner_values = partner._convert_to_write(partner._cache) with partner.env.do_in_draft(): yield @@ -25,7 +25,7 @@ def _simulate_delivery_cost(self, partner): partner.update(partner_values) @api.model - def _load_country(self): + def _get_country_from_context(self): """ Load the country from context :return: res.country recordset @@ -34,7 +34,7 @@ def _load_country(self): return self.env["res.country"].browse(country_id) @api.model - def _load_zip_code(self): + def _get_zip_from_context(self): """ Load the zip code from context :return: str @@ -49,8 +49,8 @@ def verify_carrier(self, contact): :param contact: res.partner recordset :return: False or self """ - country = self._load_country() - zip_code = self._load_zip_code() + country = self._get_country_from_context() + zip_code = self._get_zip_from_context() if country or zip_code: with self._simulate_delivery_cost(contact): # Edit country and zip diff --git a/shopinvader_delivery_carrier/services/cart.py b/shopinvader_delivery_carrier/services/cart.py index 783c38538b..8ce0bd2551 100644 --- a/shopinvader_delivery_carrier/services/cart.py +++ b/shopinvader_delivery_carrier/services/cart.py @@ -12,21 +12,6 @@ class CartService(Component): _inherit = "shopinvader.cart.service" - def set_carrier(self, **params): - """ - This service will set the given delivery method to the current - cart - :param params: The carrier_id to set - :return: - """ - cart = self._get() - if not cart: - raise UserError(_("There is not cart")) - else: - self._set_carrier(cart, params["carrier_id"]) - return self._to_json(cart) - - def get_delivery_methods(self, **params): def set_carrier(self, **params): """ This service will set the given delivery method to the current @@ -44,12 +29,6 @@ def set_carrier(self, **params): # DEPRECATED METHODS # def get_delivery_methods(self): """ - This service will return all possible delivery methods for the - current cart (depending on country/zip) - The cart is not updated with the given country/zip. The change is done - only in memory. - :param params: dict - :return: dict !!!!DEPRECATED!!!!! Uses delivery_carrier.search This service will return all possible delivery methods for the @@ -57,16 +36,6 @@ def get_delivery_methods(self): :return: """ - cart = self._get() - country = self._load_country(params) - zip_code = self._load_zip_code(params) - if country or zip_code: - cart = cart.with_context( - delivery_force_country_id=country.id, - delivery_force_zip_code=zip_code, - ) - result = self._get_available_carrier(cart) - return result return self.component("delivery_carrier").search( target="current_cart" )["rows"] @@ -119,22 +88,6 @@ def _validator_set_carrier(self): } # internal methods - def _load_country(self, params): - """ - Load the country from given params - :param params: dict - :return: res.country recordset - """ - country_id = params.pop("country_id", 0) - return self.env["res.country"].browse(country_id) - - def _load_zip_code(self, params): - """ - Load the country from given params - :param params: dict - :return: str - """ - return params.pop("zip_code", "") def _add_item(self, cart, params): res = super(CartService, self)._add_item(cart, params) diff --git a/shopinvader_delivery_carrier/services/delivery_carrier.py b/shopinvader_delivery_carrier/services/delivery_carrier.py index cf23279ff2..98dae3bce4 100644 --- a/shopinvader_delivery_carrier/services/delivery_carrier.py +++ b/shopinvader_delivery_carrier/services/delivery_carrier.py @@ -4,7 +4,8 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # pylint: disable=consider-merging-classes-inherited,method-required-super -from openerp.addons.component.core import Component +from odoo.addons.base_rest.components.service import to_int +from odoo.addons.component.core import Component class DeliveryCarrierService(Component): @@ -43,7 +44,13 @@ def _validator_search(self): "type": "string", "required": False, "allowed": ["current_cart"], - } + }, + "country_id": { + "coerce": to_int, + "required": False, + "type": "integer", + }, + "zip_code": {"required": False, "type": "string"}, } def _validator_return_search(self): @@ -82,12 +89,20 @@ def _validator_return_search(self): def _search(self, **params): """ - Search for delively carriers + Search for delivery carriers :param params: see _validator_search - :return: a list of delivery.carriers + :return: delivery.carriers recordset """ if params.get("target") == "current_cart": - return self.component(usage="cart")._get()._get_available_carrier() + cart = self.component(usage="cart")._get() + country = self._load_country(params) + zip_code = self._load_zip_code(params) + if country or zip_code: + cart = cart.with_context( + delivery_force_country_id=country.id, + delivery_force_zip_code=zip_code, + ) + return cart._get_available_carrier() return self.shopinvader_backend.carrier_ids def _prepare_carrier(self, carrier): @@ -95,6 +110,23 @@ def _prepare_carrier(self, carrier): res["type"] = None return res + def _load_country(self, params): + """ + Load the country from given params + :param params: dict + :return: res.country recordset + """ + country_id = params.pop("country_id", 0) + return self.env["res.country"].browse(country_id) + + def _load_zip_code(self, params): + """ + Load the country from given params + :param params: dict + :return: str + """ + return params.pop("zip_code", "") + @property def allowed_carrier_types(self): return [] diff --git a/shopinvader_delivery_carrier/tests/common.py b/shopinvader_delivery_carrier/tests/common.py index 2e9d021bc8..434bb6b952 100644 --- a/shopinvader_delivery_carrier/tests/common.py +++ b/shopinvader_delivery_carrier/tests/common.py @@ -13,6 +13,7 @@ def setUpClass(cls): cls.free_carrier = cls.env.ref("delivery.free_delivery_carrier") cls.poste_carrier = cls.env.ref("delivery.delivery_carrier") cls.product_1 = cls.env.ref("product.product_product_4b") + cls.precision = 2 def extract_cart(self, response): self.shopinvader_session["cart_id"] = response["set_session"][ diff --git a/shopinvader_delivery_carrier/tests/test_carrier.py b/shopinvader_delivery_carrier/tests/test_carrier.py index 719678cc75..956a20b6dc 100644 --- a/shopinvader_delivery_carrier/tests/test_carrier.py +++ b/shopinvader_delivery_carrier/tests/test_carrier.py @@ -7,6 +7,10 @@ class CarrierCase(CommonCarrierCase): + def setUp(self): + super(CarrierCase, self).setUp() + self.carrier_service = self.service.component("delivery_carrier") + def test_available_carriers(self): response = self.service.dispatch("get_delivery_methods") self.assertEqual(len(response), 2) @@ -101,8 +105,8 @@ def test_get_cart_price_by_country1(self): lines.update({line.id: line._convert_to_write(line._cache)}) nb_lines_before = self.env["sale.order.line"].search_count([]) self.service.shopinvader_session.update({"cart_id": self.cart.id}) - params = {"country_id": belgium.id} - result = self.service.dispatch("get_delivery_methods", params=params) + params = {"country_id": belgium.id, "target": "current_cart"} + result = self.carrier_service.dispatch("search", params=params) self.cart.read() cart_values_after = self.cart._convert_to_write(self.cart._cache) nb_lines_after = self.env["sale.order.line"].search_count([]) @@ -120,8 +124,8 @@ def test_get_cart_price_by_country1(self): lines.update({line.id: line._convert_to_write(line._cache)}) nb_lines_before = self.env["sale.order.line"].search_count([]) self.service.shopinvader_session.update({"cart_id": self.cart.id}) - params = {"country_id": belgium.id} - result = self.service.dispatch("get_delivery_methods", params=params) + params = {"country_id": belgium.id, "target": "current_cart"} + result = self.carrier_service.dispatch("search", params=params) self.assertEquals(self.cart.name, cart_values_before.get("name", "")) self.cart.read() cart_values_after = self.cart._convert_to_write(self.cart._cache) @@ -152,6 +156,7 @@ def test_get_cart_price_by_country_anonymous(self): partner=self.backend.anonymous_partner_id, shopinvader_session={} ) as work: self.service = work.component(usage="cart") + # Update with anonymous user self.test_get_cart_price_by_country1() def _check_carriers(self, result): @@ -163,18 +168,19 @@ def _check_carriers(self, result): available_carriers = self.backend.carrier_ids.with_context( order_id=self.cart.id ).filtered(lambda c: c.available) - self.assertEquals(len(available_carriers), len(result)) - for carrier_result in result: + carrier_rows = result.get("rows") + self.assertEquals(len(available_carriers), len(carrier_rows)) + for carrier_result in carrier_rows: carrier = available_carriers.filtered( lambda c: c.id == carrier_result.get("id") ) self.assertEquals(len(carrier), 1) + self.assertEquals(carrier.name, carrier_result.get("name")) self.assertAlmostEquals( carrier.price, carrier_result.get("price"), places=self.precision, ) - self.assertEquals(carrier.name, carrier_result.get("name")) return True def test_get_cart_price_by_country2(self): @@ -199,8 +205,8 @@ def test_get_cart_price_by_country2(self): lines.update({line.id: line._convert_to_write(line._cache)}) nb_lines_before = self.env["sale.order.line"].search_count([]) self.service.shopinvader_session.update({"cart_id": self.cart.id}) - params = {"country_id": belgium.id} - result = self.service.dispatch("get_delivery_methods", params=params) + params = {"country_id": belgium.id, "target": "current_cart"} + result = self.carrier_service.dispatch("search", params=params) self.assertEquals(self.cart.name, cart_values_before.get("name", "")) self.cart.read() cart_values_after = self.cart._convert_to_write(self.cart._cache) @@ -251,8 +257,8 @@ def test_get_cart_price_by_country3(self): lines.update({line.id: line._convert_to_write(line._cache)}) nb_lines_before = self.env["sale.order.line"].search_count([]) self.service.shopinvader_session.update({"cart_id": self.cart.id}) - params = {"country_id": belgium.id} - result = self.service.dispatch("get_delivery_methods", params=params) + params = {"country_id": belgium.id, "target": "current_cart"} + result = self.carrier_service.dispatch("search", params=params) self.assertEquals(self.cart.name, cart_values_before.get("name", "")) self.cart.read() cart_values_after = self.cart._convert_to_write(self.cart._cache) diff --git a/shopinvader_delivery_carrier/tests/test_delivery_carrier.py b/shopinvader_delivery_carrier/tests/test_delivery_carrier.py index 0e1fab3686..8f380e7ea0 100644 --- a/shopinvader_delivery_carrier/tests/test_delivery_carrier.py +++ b/shopinvader_delivery_carrier/tests/test_delivery_carrier.py @@ -17,14 +17,14 @@ def test_search_all(self): "rows": [ { "price": 0.0, - "description": None, + "description": self.free_carrier.description or None, "id": self.free_carrier.id, "name": self.free_carrier.name, "type": None, }, { "price": 0.0, - "description": None, + "description": self.poste_carrier.description or None, "id": self.poste_carrier.id, "name": self.poste_carrier.name, "type": None, @@ -40,14 +40,14 @@ def test_search_current_cart(self): "rows": [ { "price": 0.0, - "description": None, + "description": self.free_carrier.description or None, "id": self.free_carrier.id, "name": self.free_carrier.name, "type": None, }, { "price": 20.0, - "description": None, + "description": self.poste_carrier.description or None, "id": self.poste_carrier.id, "name": self.poste_carrier.name, "type": None,