#9 feat(Tryton): update products from tryton.
This commit is contained in:
parent
76e525735d
commit
c33c6f630a
@ -283,7 +283,7 @@ class ProductsFromTrytonView(APIView):
|
|||||||
created_products.append(product.id)
|
created_products.append(product.id)
|
||||||
continue
|
continue
|
||||||
if self.__need_update(product, tryton_product):
|
if self.__need_update(product, tryton_product):
|
||||||
self.update_product(product, tryton_product)
|
self.__update_product(product, tryton_product)
|
||||||
updated_products.append(product.id)
|
updated_products.append(product.id)
|
||||||
else:
|
else:
|
||||||
untouched_products.append(product.id)
|
untouched_products.append(product.id)
|
||||||
@ -312,6 +312,9 @@ class ProductsFromTrytonView(APIView):
|
|||||||
return True
|
return True
|
||||||
if not product.price == tryton_product.get('price'):
|
if not product.price == tryton_product.get('price'):
|
||||||
return True
|
return True
|
||||||
|
unit = tryton_product.get('default_uom.')
|
||||||
|
if not product.measuring_unit == unit.get('rec_name'):
|
||||||
|
return True
|
||||||
|
|
||||||
def __create_product(self, tryton_product):
|
def __create_product(self, tryton_product):
|
||||||
product = Product()
|
product = Product()
|
||||||
@ -323,3 +326,12 @@ class ProductsFromTrytonView(APIView):
|
|||||||
product.unit_external_id = unit.get('id')
|
product.unit_external_id = unit.get('id')
|
||||||
product.save()
|
product.save()
|
||||||
return product
|
return product
|
||||||
|
|
||||||
|
def __update_product(self, product, tryton_product):
|
||||||
|
product.name = tryton_product.get('name')
|
||||||
|
product.price = tryton_product.get('list_price')
|
||||||
|
product.external_id = tryton_product.get('id')
|
||||||
|
unit = tryton_product.get('default_uom.')
|
||||||
|
product.measuring_unit = unit.get('rec_name')
|
||||||
|
product.unit_external_id = unit.get('id')
|
||||||
|
product.save()
|
||||||
|
@ -8,29 +8,41 @@ from ..models import ProductCategory, Product
|
|||||||
|
|
||||||
class TestProductsFromTryton(TestCase):
|
class TestProductsFromTryton(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.client = Client()
|
self.product = Product.objects.create(
|
||||||
|
name='Panela',
|
||||||
|
price=5000,
|
||||||
|
measuring_unit='UNIT',
|
||||||
|
unit_external_id=1,
|
||||||
|
external_id=191
|
||||||
|
)
|
||||||
|
self.product.save()
|
||||||
|
|
||||||
@patch('sabatron_tryton_rpc_client.client.Client.call')
|
@patch('sabatron_tryton_rpc_client.client.Client.call')
|
||||||
@patch('sabatron_tryton_rpc_client.client.Client.connect')
|
@patch('sabatron_tryton_rpc_client.client.Client.connect')
|
||||||
def test_import_products(self, mock_connect, mock_call):
|
def test_create_import_products(self, mock_connect, mock_call):
|
||||||
mock_connect.return_value = None
|
mock_connect.return_value = None
|
||||||
|
|
||||||
def fake_call(*args, **kwargs):
|
def fake_call(*args, **kwargs):
|
||||||
product_search = 'model.product.product.search'
|
product_search = 'model.product.product.search'
|
||||||
search_args = [[], 0, 1000, [['rec_name', 'ASC'], ['id', None]], {'company': 1}]
|
search_args = [[], 0, 1000, [['rec_name', 'ASC'], ['id', None]], {'company': 1}]
|
||||||
if (args == (product_search, search_args)):
|
if (args == (product_search, search_args)):
|
||||||
return [190]
|
return [190, 191]
|
||||||
|
|
||||||
product_read = 'model.product.product.read'
|
product_read = 'model.product.product.read'
|
||||||
product_args = ([190],
|
product_args = ([190, 191],
|
||||||
['id', 'name', 'default_uom.id',
|
['id', 'name', 'default_uom.id',
|
||||||
'default_uom.rec_name', 'list_price'],
|
'default_uom.rec_name', 'list_price'],
|
||||||
{'company': 1}
|
{'company': 1}
|
||||||
)
|
)
|
||||||
if (args == (product_read, product_args)):
|
if (args == (product_read, product_args)):
|
||||||
return [{'id': 190, 'list_price': Decimal('25000'),
|
return [
|
||||||
'name': 'Producto 1',
|
{'id': 190, 'list_price': Decimal('25000'),
|
||||||
'default_uom.': {'id': 1, 'rec_name': 'Unit'}}]
|
'name': 'Producto 1',
|
||||||
|
'default_uom.': {'id': 1, 'rec_name': 'Unit'}},
|
||||||
|
{'id': 191, 'list_price': Decimal('6000'),
|
||||||
|
'name': 'Panela2',
|
||||||
|
'default_uom.': {'id': 1, 'rec_name': 'Unit'}},
|
||||||
|
]
|
||||||
|
|
||||||
raise Exception(f"Sorry, args non expected on this test: {args}")
|
raise Exception(f"Sorry, args non expected on this test: {args}")
|
||||||
|
|
||||||
@ -42,16 +54,22 @@ class TestProductsFromTryton(TestCase):
|
|||||||
|
|
||||||
content = json.loads(response.content.decode('utf-8'))
|
content = json.loads(response.content.decode('utf-8'))
|
||||||
expected_response = {
|
expected_response = {
|
||||||
'checked_tryton_products': [190],
|
'checked_tryton_products': [190, 191],
|
||||||
'created_products': [1],
|
'created_products': [2],
|
||||||
'untouched_products': [],
|
'untouched_products': [],
|
||||||
'failed_products': [],
|
'failed_products': [],
|
||||||
'updated_products': []
|
'updated_products': [1]
|
||||||
}
|
}
|
||||||
self.assertEqual(content, expected_response)
|
self.assertEqual(content, expected_response)
|
||||||
|
|
||||||
created_product = Product.objects.get(id=1)
|
created_product = Product.objects.get(id=2)
|
||||||
self.assertEqual(created_product.external_id, str(190))
|
self.assertEqual(created_product.external_id, str(190))
|
||||||
self.assertEqual(created_product.name, 'Producto 1')
|
self.assertEqual(created_product.name, 'Producto 1')
|
||||||
self.assertEqual(created_product.price, Decimal('25000'))
|
self.assertEqual(created_product.price, Decimal('25000'))
|
||||||
self.assertEqual(created_product.measuring_unit, 'Unit')
|
self.assertEqual(created_product.measuring_unit, 'Unit')
|
||||||
|
|
||||||
|
updated_product = Product.objects.get(id=1)
|
||||||
|
self.assertEqual(updated_product.external_id, str(191))
|
||||||
|
self.assertEqual(updated_product.name, 'Panela2')
|
||||||
|
self.assertEqual(updated_product.price, Decimal('6000'))
|
||||||
|
self.assertEqual(updated_product.measuring_unit, 'Unit')
|
||||||
|
Loading…
Reference in New Issue
Block a user