Skip to content

Commit

Permalink
[FIX] shopinvader_delivery_carrier: Adapt code after forward port of #…
Browse files Browse the repository at this point in the history
  • Loading branch information
lmignon committed Oct 31, 2019
1 parent 039b337 commit b2ee12b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 46 deletions.
8 changes: 5 additions & 3 deletions shopinvader_delivery_carrier/models/delivery_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
24 changes: 17 additions & 7 deletions shopinvader_delivery_carrier/services/delivery_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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"]
67 changes: 35 additions & 32 deletions shopinvader_delivery_carrier/tests/test_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -125,25 +125,25 @@ 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
)
order_line.read()
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):
"""
Expand All @@ -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,
)
Expand All @@ -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)
Expand All @@ -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
)
Expand All @@ -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):
"""
Expand All @@ -258,23 +261,23 @@ 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
)
order_line.read()
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)
8 changes: 4 additions & 4 deletions shopinvader_delivery_carrier/tests/test_delivery_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit b2ee12b

Please sign in to comment.