#9 feat(Tryton): update products from tryton.
This commit is contained in:
		| @@ -283,7 +283,7 @@ class ProductsFromTrytonView(APIView): | ||||
|                 created_products.append(product.id) | ||||
|                 continue | ||||
|             if self.__need_update(product, tryton_product): | ||||
|                 self.update_product(product, tryton_product) | ||||
|                 self.__update_product(product, tryton_product) | ||||
|                 updated_products.append(product.id) | ||||
|             else: | ||||
|                 untouched_products.append(product.id) | ||||
| @@ -312,6 +312,9 @@ class ProductsFromTrytonView(APIView): | ||||
|             return True | ||||
|         if not product.price == tryton_product.get('price'): | ||||
|             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): | ||||
|         product = Product() | ||||
| @@ -323,3 +326,12 @@ class ProductsFromTrytonView(APIView): | ||||
|         product.unit_external_id = unit.get('id') | ||||
|         product.save() | ||||
|         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): | ||||
|     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.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 | ||||
|  | ||||
|         def fake_call(*args, **kwargs): | ||||
|             product_search = 'model.product.product.search' | ||||
|             search_args = [[], 0, 1000, [['rec_name', 'ASC'], ['id', None]], {'company': 1}] | ||||
|             if (args == (product_search, search_args)): | ||||
|                 return [190] | ||||
|                 return [190, 191] | ||||
|  | ||||
|             product_read = 'model.product.product.read' | ||||
|             product_args = ([190], | ||||
|             product_args = ([190, 191], | ||||
|                             ['id', 'name', 'default_uom.id', | ||||
|                              'default_uom.rec_name', 'list_price'], | ||||
|                             {'company': 1} | ||||
|                             ) | ||||
|             if (args == (product_read, product_args)): | ||||
|                 return [{'id': 190, 'list_price': Decimal('25000'), | ||||
|                          'name': 'Producto 1', | ||||
|                          'default_uom.': {'id': 1, 'rec_name': 'Unit'}}] | ||||
|                 return [ | ||||
|                     {'id': 190, 'list_price': Decimal('25000'), | ||||
|                      '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}") | ||||
|  | ||||
| @@ -42,16 +54,22 @@ class TestProductsFromTryton(TestCase): | ||||
|  | ||||
|         content = json.loads(response.content.decode('utf-8')) | ||||
|         expected_response = { | ||||
|             'checked_tryton_products': [190], | ||||
|             'created_products': [1], | ||||
|             'checked_tryton_products': [190, 191], | ||||
|             'created_products': [2], | ||||
|             'untouched_products': [], | ||||
|             'failed_products': [], | ||||
|             'updated_products': [] | ||||
|             'updated_products': [1] | ||||
|         } | ||||
|         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.name, 'Producto 1') | ||||
|         self.assertEqual(created_product.price, Decimal('25000')) | ||||
|         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') | ||||
|   | ||||
		Reference in New Issue
	
	Block a user