Skip to content

Commit

Permalink
Update after cherry-pick
Browse files Browse the repository at this point in the history
  • Loading branch information
acsonefho committed Sep 20, 2019
1 parent 17a6737 commit a233c02
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 72 deletions.
10 changes: 5 additions & 5 deletions shopinvader_delivery_carrier/models/delivery_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ 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
# Restore values
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
Expand All @@ -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
Expand All @@ -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
Expand Down
47 changes: 0 additions & 47 deletions shopinvader_delivery_carrier/services/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,29 +29,13 @@ 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
current cart
: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"]
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 37 additions & 5 deletions shopinvader_delivery_carrier/services/delivery_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -82,19 +89,44 @@ 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):
res = carrier.jsonify(self._json_parser_carrier)[0]
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 []
Expand Down
1 change: 1 addition & 0 deletions shopinvader_delivery_carrier/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"][
Expand Down
28 changes: 17 additions & 11 deletions shopinvader_delivery_carrier/tests/test_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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([])
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
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": 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,
Expand All @@ -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,
Expand Down

0 comments on commit a233c02

Please sign in to comment.