diff --git a/shopinvader_delivery_carrier/models/delivery_carrier.py b/shopinvader_delivery_carrier/models/delivery_carrier.py index 0cd8453246..5f51d9c9b5 100644 --- a/shopinvader_delivery_carrier/models/delivery_carrier.py +++ b/shopinvader_delivery_carrier/models/delivery_carrier.py @@ -42,7 +42,7 @@ def _get_zip_from_context(self): return self.env.context.get("delivery_force_zip_code", "") @api.multi - def verify_carrier(self, contact): + def available_carriers(self, contact): """ Inherit the function to force some values on the given contact (only in cache). @@ -60,7 +60,9 @@ def verify_carrier(self, contact): # current zip could be related to another country and simulate # a wrong price. contact.update({"country_id": country.id, "zip": zip_code}) - result = super(DeliveryCarrier, self).verify_carrier(contact) + result = super(DeliveryCarrier, self).available_carriers( + contact + ) else: - result = super(DeliveryCarrier, self).verify_carrier(contact) + result = super(DeliveryCarrier, self).available_carriers(contact) return result diff --git a/shopinvader_delivery_carrier/services/delivery_carrier.py b/shopinvader_delivery_carrier/services/delivery_carrier.py index 98dae3bce4..4e0953b8e5 100644 --- a/shopinvader_delivery_carrier/services/delivery_carrier.py +++ b/shopinvader_delivery_carrier/services/delivery_carrier.py @@ -30,10 +30,15 @@ def search(self, **params): The field type is a technical field only use inform if the carrier provides some specialized functionalities """ - delivery_carriers = self._search(**params) + cart = None + if params.get("target") == "current_cart": + cart = self.component(usage="cart")._get() + delivery_carriers = self._search(cart=cart, **params) return { "count": len(delivery_carriers), - "rows": [self._prepare_carrier(dc) for dc in delivery_carriers], + "rows": [ + self._prepare_carrier(dc, cart) for dc in delivery_carriers + ], } # Validators @@ -87,14 +92,15 @@ def _validator_return_search(self): # Services implementation - def _search(self, **params): + def _search(self, cart, **params): """ Search for delivery carriers + :param: cart: if provided, the list will be limited to the carrier + applying to the given cart :param params: see _validator_search :return: delivery.carriers recordset """ - if params.get("target") == "current_cart": - cart = self.component(usage="cart")._get() + if cart: country = self._load_country(params) zip_code = self._load_zip_code(params) if country or zip_code: @@ -105,9 +111,13 @@ def _search(self, **params): return cart._get_available_carrier() return self.shopinvader_backend.carrier_ids - def _prepare_carrier(self, carrier): + def _prepare_carrier(self, carrier, cart=None): res = carrier.jsonify(self._json_parser_carrier)[0] res["type"] = None + price = 0.0 + if cart: + price = carrier.rate_shipment(cart).get("price", 0.0) + res["price"] = price return res def _load_country(self, params): @@ -133,4 +143,4 @@ def allowed_carrier_types(self): @property def _json_parser_carrier(self): - return ["id", "name", "description", "price"] + return ["id", "name", "name:description"] diff --git a/shopinvader_delivery_carrier/tests/test_carrier.py b/shopinvader_delivery_carrier/tests/test_carrier.py index 8597cfcb20..a43680ac87 100644 --- a/shopinvader_delivery_carrier/tests/test_carrier.py +++ b/shopinvader_delivery_carrier/tests/test_carrier.py @@ -110,7 +110,7 @@ def test_get_cart_price_by_country1(self): cart_values_after = self.cart._convert_to_write(self.cart._cache) nb_lines_after = self.env["sale.order.line"].search_count([]) self.assertDictEqual(cart_values_before, cart_values_after) - self.assertEquals(nb_lines_after, nb_lines_before) + self.assertEqual(nb_lines_after, nb_lines_before) partner.write({"country_id": french_country.id}) self.cart.write({"carrier_id": self.poste_carrier.id}) @@ -125,15 +125,15 @@ def test_get_cart_price_by_country1(self): self.service.shopinvader_session.update({"cart_id": self.cart.id}) 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.assertEqual(self.cart.name, cart_values_before.get("name", "")) self.cart.read() cart_values_after = self.cart._convert_to_write(self.cart._cache) self.assertDictEqual(cart_values_before, cart_values_after) nb_lines_after = self.env["sale.order.line"].search_count([]) - self.assertEquals(nb_lines_after, nb_lines_before) + self.assertEqual(nb_lines_after, nb_lines_before) # Ensure lines still ok - self.assertEquals(len(lines), len(self.cart.order_line)) - for line_id, line_values in lines.iteritems(): + self.assertEqual(len(lines), len(self.cart.order_line)) + for line_id, line_values in lines.items(): order_line = self.cart.order_line.filtered( lambda l, lid=line_id: l.id == lid ) @@ -141,9 +141,9 @@ def test_get_cart_price_by_country1(self): self.assertDictEqual( order_line._convert_to_write(order_line._cache), line_values ) - self.assertEquals(self.cart.partner_id, partner) - self.assertEquals(french_country, partner.country_id) - self._check_carriers(result) + self.assertEqual(self.cart.partner_id, partner) + self.assertEqual(french_country, partner.country_id) + self._check_carriers(result, country=belgium) def test_get_cart_price_by_country_anonymous(self): """ @@ -158,25 +158,27 @@ def test_get_cart_price_by_country_anonymous(self): # Update with anonymous user self.test_get_cart_price_by_country1() - def _check_carriers(self, result): + def _check_carriers(self, result, country): """ Check carrier for current cart based on given result list of dict. :param result: list of dict :return: bool """ - available_carriers = self.backend.carrier_ids.with_context( - order_id=self.cart.id - ).filtered(lambda c: c.available) + available_carriers = self.backend.carrier_ids + available_carriers = available_carriers.filtered( + lambda c, country=country: country in c.country_ids + or not c.country_ids + ) carrier_rows = result.get("rows") - self.assertEquals(len(available_carriers), len(carrier_rows)) + self.assertEqual(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, + self.assertEqual(len(carrier), 1) + self.assertEqual(carrier.name, carrier_result.get("name")) + self.assertAlmostEqual( + carrier.rate_shipment(self.cart).get("price", 0.0), carrier_result.get("price"), places=self.precision, ) @@ -193,7 +195,8 @@ def test_get_cart_price_by_country2(self): partner = self.cart.partner_id partner.write({"country_id": french_country.id}) self.cart.write({"carrier_id": self.poste_carrier.id}) - self.cart.delivery_set() + self.cart.get_delivery_price() + self.cart.set_delivery_line() # Force load every fields self.cart.read() cart_values_before = self.cart._convert_to_write(self.cart._cache) @@ -206,16 +209,17 @@ def test_get_cart_price_by_country2(self): self.service.shopinvader_session.update({"cart_id": self.cart.id}) 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._check_carriers(result, country=belgium) + self.assertEqual(self.cart.name, cart_values_before.get("name", "")) self.cart.read() cart_values_after = self.cart._convert_to_write(self.cart._cache) cart_values_after.pop("order_line", None) self.assertDictEqual(cart_values_before, cart_values_after) nb_lines_after = self.env["sale.order.line"].search_count([]) - self.assertEquals(nb_lines_after, nb_lines_before) + self.assertEqual(nb_lines_after, nb_lines_before) # Ensure lines still ok - self.assertEquals(len(lines), len(self.cart.order_line)) - for line_id, line_values in lines.iteritems(): + self.assertEqual(len(lines), len(self.cart.order_line)) + for line_id, line_values in lines.items(): order_line = self.cart.order_line.filtered( lambda l, lid=line_id: l.id == lid ) @@ -230,9 +234,8 @@ def test_get_cart_price_by_country2(self): self.assertDictEqual( order_line._convert_to_write(order_line._cache), line_values ) - self.assertEquals(self.cart.partner_id, partner) - self.assertEquals(french_country, partner.country_id) - self._check_carriers(result) + self.assertEqual(self.cart.partner_id, partner) + self.assertEqual(french_country, partner.country_id) def test_get_cart_price_by_country3(self): """ @@ -258,16 +261,17 @@ def test_get_cart_price_by_country3(self): self.service.shopinvader_session.update({"cart_id": self.cart.id}) 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._check_carriers(result, country=belgium) + self.assertEqual(self.cart.name, cart_values_before.get("name", "")) self.cart.read() cart_values_after = self.cart._convert_to_write(self.cart._cache) cart_values_after.pop("order_line", None) self.assertDictEqual(cart_values_before, cart_values_after) nb_lines_after = self.env["sale.order.line"].search_count([]) - self.assertEquals(nb_lines_after, nb_lines_before) + self.assertEqual(nb_lines_after, nb_lines_before) # Ensure lines still ok - self.assertEquals(len(lines), len(self.cart.order_line)) - for line_id, line_values in lines.iteritems(): + self.assertEqual(len(lines), len(self.cart.order_line)) + for line_id, line_values in lines.items(): order_line = self.cart.order_line.filtered( lambda l, lid=line_id: l.id == lid ) @@ -275,6 +279,5 @@ def test_get_cart_price_by_country3(self): self.assertDictEqual( order_line._convert_to_write(order_line._cache), line_values ) - self.assertEquals(self.cart.partner_id, partner) - self.assertEquals(french_country, partner.country_id) - self._check_carriers(result) + self.assertEqual(self.cart.partner_id, partner) + self.assertEqual(french_country, partner.country_id) diff --git a/shopinvader_delivery_carrier/tests/test_delivery_carrier.py b/shopinvader_delivery_carrier/tests/test_delivery_carrier.py index 8f380e7ea0..dc7d40d389 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": self.free_carrier.description or None, + "description": self.free_carrier.name or None, "id": self.free_carrier.id, "name": self.free_carrier.name, "type": None, }, { "price": 0.0, - "description": self.poste_carrier.description or None, + "description": self.poste_carrier.name 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": self.free_carrier.description or None, + "description": self.free_carrier.name or None, "id": self.free_carrier.id, "name": self.free_carrier.name, "type": None, }, { "price": 20.0, - "description": self.poste_carrier.description or None, + "description": self.poste_carrier.name or None, "id": self.poste_carrier.id, "name": self.poste_carrier.name, "type": None,