-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by Cedric-Pigeon
- Loading branch information
Showing
5 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,9 @@ | |
# Sébastien BEAU <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
# pylint: disable=consider-merging-classes-inherited | ||
|
||
import logging | ||
|
||
from cerberus import Validator | ||
from odoo.addons.base_rest.components.service import to_int | ||
from odoo.addons.component.core import Component | ||
from odoo.exceptions import UserError | ||
|
@@ -85,7 +85,85 @@ def clear(self): | |
cart = self._clear_cart(cart) | ||
return self._to_json(cart) | ||
|
||
def _get_validator_cart_by_id_domain(self, value): | ||
""" | ||
Get cart by id domain. Limiting to session partner. | ||
:param value: | ||
:return: | ||
""" | ||
return [("partner_id", "=", self.partner.id), ("id", "=", value)] | ||
|
||
def _get_line_copy_vals(self, line): | ||
""" | ||
Prepare copy values to be passed to _add_item | ||
:param line: sale.order.line | ||
:return: dict | ||
""" | ||
return { | ||
"product_id": line.product_id.id, | ||
"item_qty": line.product_uom_qty, | ||
} | ||
|
||
def _get_lines_to_copy(self, cart): | ||
return cart.order_line | ||
|
||
# pylint: disable=W8102,W8106 | ||
def copy(self, **params): | ||
""" | ||
This service allows | ||
:param params: | ||
:return: | ||
""" | ||
return self._copy(**params) | ||
|
||
def _copy(self, **params): | ||
""" | ||
Copy the cart given by id without the lines | ||
They will be re-added | ||
:return: dict/json | ||
""" | ||
cart = self.env["sale.order"].search( | ||
self._get_validator_cart_by_id_domain(params.get("id")) | ||
) | ||
# Copy the existing cart | ||
# Delete all lines and re-add them with 'shopinvader' flavour | ||
new_cart = cart.copy({"order_line": False, "typology": "cart"}) | ||
for line in self._get_lines_to_copy(cart): | ||
vals = self._get_line_copy_vals(line) | ||
self._add_item(new_cart, vals) | ||
|
||
return self._to_json(new_cart) | ||
|
||
# Validator | ||
def _cart_validator_exists(self, field, value, error): | ||
""" | ||
Implements 'check_with' validation | ||
:param field: | ||
:param value: | ||
:param error: | ||
:return: | ||
""" | ||
cart = self.env["sale.order"].search( | ||
self._get_validator_cart_by_id_domain(value) | ||
) | ||
if len(cart) != 1: | ||
error( | ||
field, _("The cart does not exists or does not belong to you!") | ||
) | ||
|
||
def _validator_copy(self): | ||
return { | ||
"id": { | ||
"coerce": to_int, | ||
"required": True, | ||
"type": "integer", | ||
"check_with": self._cart_validator_exists, | ||
} | ||
} | ||
|
||
def _validator_return_copy(self): | ||
return Validator({}, allow_unknown=True) | ||
|
||
def _validator_search(self): | ||
return {} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2020 ACSONE SA/NV (<http://acsone.eu>) | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
from odoo.exceptions import UserError | ||
|
||
from .test_cart import CommonConnectedCartCase | ||
|
||
|
||
class TestCartCopy(CommonConnectedCartCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
super(TestCartCopy, cls).setUpClass() | ||
cls.product_copy = cls.env.ref("product.product_product_24") | ||
cls.product_copy.list_price = 500.0 | ||
|
||
def test_cart_copy(self): | ||
# Copy existing cart | ||
# Check if we return a new one with new values | ||
result = self.service.dispatch("copy", params={"id": self.cart.id}) | ||
cart_data = result.get("data") | ||
new_id = cart_data.get("id") | ||
self.assertNotEquals(new_id, self.cart.id) | ||
copy_cart = self.env["sale.order"].browse(new_id) | ||
self.assertEquals("cart", copy_cart.typology) | ||
line = copy_cart.order_line.filtered( | ||
lambda l: l.product_id == self.product_copy | ||
) | ||
self.assertEquals(500.0, line.price_unit) | ||
|
||
def test_cart_copy_does_not_exist(self): | ||
cart_id = self.cart.id | ||
self.cart.unlink() | ||
# Check validator | ||
with self.assertRaises(UserError): | ||
self.service.dispatch("copy", params={"id": cart_id}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters