feat (Tryton): get customers from tryton.
This commit is contained in:
		| @@ -335,3 +335,77 @@ class ProductsFromTrytonView(APIView): | ||||
|         product.measuring_unit = unit.get('rec_name') | ||||
|         product.unit_external_id = unit.get('id') | ||||
|         product.save() | ||||
|  | ||||
|  | ||||
| class CustomersFromTrytonView(APIView): | ||||
|     def post(self, request): | ||||
|         tryton_client = Client( | ||||
|             hostname=TRYTON_HOST, | ||||
|             database=TRYTON_DATABASE, | ||||
|             username=TRYTON_USERNAME, | ||||
|             password=TRYTON_PASSWORD | ||||
|         ) | ||||
|         tryton_client.connect() | ||||
|         method = 'model.party.party.search' | ||||
|         context = {'company': 1} | ||||
|         params = [[], 0, 1000, [["rec_name", "ASC"], ["id", None]], context] | ||||
|         party_ids = tryton_client.call(method, params) | ||||
|         tryton_parties = self.__get_party_datails( | ||||
|             party_ids, tryton_client, context | ||||
|         ) | ||||
|         checked_tryton_parties = party_ids | ||||
|         failed_parties = [] | ||||
|         updated_customers = [] | ||||
|         created_customers = [] | ||||
|         untouched_customers = [] | ||||
|         print('aqui') | ||||
|         print(tryton_parties) | ||||
|  | ||||
|         for tryton_party in tryton_parties: | ||||
|             try: | ||||
|                 customer = Customer.objects.get( | ||||
|                     external_id=tryton_party.get('id') | ||||
|                 ) | ||||
|             except Customer.DoesNotExist: | ||||
|                 customer = self.__create_customer(tryton_party) | ||||
|                 created_customers.append(customer.id) | ||||
|                 continue | ||||
|             if self.__need_update(customer, tryton_party): | ||||
|                 self.__update_customer(customer, tryton_party) | ||||
|                 updated_customers.append(customer.id) | ||||
|             else: | ||||
|                 untouched_customers.append(customer.id) | ||||
|  | ||||
|         return Response( | ||||
|             { | ||||
|                 'checked_tryton_parties': checked_tryton_parties, | ||||
|                 'failed_parties': failed_parties, | ||||
|                 'updated_customers': updated_customers, | ||||
|                 'created_customers': created_customers, | ||||
|                 'untouched_customers': untouched_customers, | ||||
|             }, | ||||
|             status=200 | ||||
|         ) | ||||
|  | ||||
|     def __get_party_datails(self, party_ids, tryton_client, context): | ||||
|         tryton_fields = ['id', 'name'] | ||||
|         method = 'model.party.party.read' | ||||
|         params = (party_ids, tryton_fields, context) | ||||
|         response = tryton_client.call(method, params) | ||||
|         return response | ||||
|  | ||||
|     def __need_update(self, customer, tryton_party): | ||||
|         if not customer.name == tryton_party.get('name'): | ||||
|             return True | ||||
|  | ||||
|     def __create_customer(self, tryton_party): | ||||
|         customer = Customer() | ||||
|         customer.name = tryton_party.get('name') | ||||
|         customer.external_id = tryton_party.get('id') | ||||
|         customer.save() | ||||
|         return customer | ||||
|  | ||||
|     def __update_customer(self, customer, tryton_party): | ||||
|         customer.name = tryton_party.get('name') | ||||
|         customer.external_id = tryton_party.get('id') | ||||
|         customer.save() | ||||
|   | ||||
| @@ -0,0 +1,66 @@ | ||||
| import json | ||||
| from unittest.mock import patch | ||||
|  | ||||
| from django.test import Client, TestCase | ||||
| from ..models import Customer | ||||
|  | ||||
|  | ||||
| class TestCustomersFromTryton(TestCase): | ||||
|     def setUp(self): | ||||
|         self.customer = Customer.objects.create( | ||||
|             name='Calos', | ||||
|             external_id=5 | ||||
|         ) | ||||
|         self.customer.save() | ||||
|  | ||||
|         self.customer2 = Customer.objects.create( | ||||
|             name='Cristian', | ||||
|             external_id=6 | ||||
|         ) | ||||
|         self.customer2.save() | ||||
|  | ||||
|     @patch('sabatron_tryton_rpc_client.client.Client.call') | ||||
|     @patch('sabatron_tryton_rpc_client.client.Client.connect') | ||||
|     def test_create_import_customer(self, mock_connect, mock_call): | ||||
|         def fake_call(*args, **kwargs): | ||||
|             party_search = 'model.party.party.search' | ||||
|             search_args = [[], 0, 1000, [['rec_name', 'ASC'], ['id', None]], {'company': 1}] | ||||
|  | ||||
|             if (args == (party_search, search_args)): | ||||
|                 return [5, 6, 7, 8] | ||||
|  | ||||
|             party_read = 'model.party.party.read' | ||||
|             read_args = ([5, 6, 7, 8], ['id', 'name'], {'company': 1}) | ||||
|             if (args == (party_read, read_args)): | ||||
|                 return [ | ||||
|                     {'id': 5, 'name': 'Carlos'}, | ||||
|                     {'id': 6, 'name': 'Cristian'}, | ||||
|                     {'id': 7, 'name': 'Ana'}, | ||||
|                     {'id': 8, 'name': 'José'}, | ||||
|                 ] | ||||
|  | ||||
|             raise Exception(f"Sorry, args non expected on  this test: {args}") | ||||
|         mock_call.side_effect = fake_call | ||||
|  | ||||
|         url = '/don_confiao/api/importar_clientes_de_tryton' | ||||
|         response = self.client.post(url) | ||||
|         self.assertEqual(response.status_code, 200) | ||||
|  | ||||
|  | ||||
|         content = json.loads(response.content.decode('utf-8')) | ||||
|         expected_response = { | ||||
|             'checked_tryton_parties': [5, 6, 7, 8], | ||||
|             'created_customers': [3, 4], | ||||
|             'untouched_customers': [2], | ||||
|             'failed_parties': [], | ||||
|             'updated_customers': [1] | ||||
|         } | ||||
|         self.assertEqual(content, expected_response) | ||||
|  | ||||
|         created_customer = Customer.objects.get(id=3) | ||||
|         self.assertEqual(created_customer.external_id, str(7)) | ||||
|         self.assertEqual(created_customer.name, 'Ana') | ||||
|  | ||||
|         updated_customer = Customer.objects.get(id=1) | ||||
|         self.assertEqual(updated_customer.external_id, str(5)) | ||||
|         self.assertEqual(updated_customer.name, 'Carlos') | ||||
| @@ -24,6 +24,9 @@ urlpatterns = [ | ||||
|          api_views.ProductsFromTrytonView.as_view(), | ||||
|          name="products_from_tryton"), | ||||
|     path("importar_terceros", views.import_customers, name="import_customers"), | ||||
|     path('api/importar_clientes_de_tryton', | ||||
|          api_views.CustomersFromTrytonView.as_view(), | ||||
|          name="customers_from_tryton"), | ||||
|     path("exportar_ventas_para_tryton", | ||||
|          views.exportar_ventas_para_tryton, | ||||
|          name="exportar_ventas_para_tryton"), | ||||
|   | ||||
		Reference in New Issue
	
	Block a user