Compare commits
	
		
			7 Commits
		
	
	
		
			d7f746db0a
			...
			09cabd6798
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 09cabd6798 | |||
| becf5f929a | |||
| 2c64f80f9c | |||
| 58a600c17d | |||
| 0ce8f2e3fb | |||
| b78ec4549b | |||
| 13618e7dda | 
| @@ -1,7 +1,8 @@ | |||||||
| from trytond.pool import Pool | from trytond.pool import Pool | ||||||
| from . import sale_order, user | from . import sale_order, user, routes | ||||||
|  |  | ||||||
| __all__ = ['register'] | __all__ = [ | ||||||
|  |     'register', 'routes'] | ||||||
|  |  | ||||||
|  |  | ||||||
| def register(): | def register(): | ||||||
|   | |||||||
							
								
								
									
										134
									
								
								routes.py
									
									
									
									
									
								
							
							
						
						
									
										134
									
								
								routes.py
									
									
									
									
									
								
							| @@ -1,4 +1,5 @@ | |||||||
| from trytond.wsgi import app | from trytond.wsgi import app | ||||||
|  | from trytond.transaction import Transaction | ||||||
| from trytond.protocols.wrappers import ( | from trytond.protocols.wrappers import ( | ||||||
|     with_pool, |     with_pool, | ||||||
|     with_transaction, |     with_transaction, | ||||||
| @@ -6,20 +7,135 @@ from trytond.protocols.wrappers import ( | |||||||
|     allow_null_origin) |     allow_null_origin) | ||||||
| import json | import json | ||||||
|  |  | ||||||
| sale_order = user_application('sale_order') | sale_order_application = user_application('sale_order') | ||||||
|  |  | ||||||
|  |  | ||||||
| @app.route('/<database_name>/sale_order/order', methods=['POST']) | @app.route( | ||||||
|  |     '/<database_name>/sale_order/associate_party/<contact_method>', methods=[ | ||||||
|  |         "GET"]) | ||||||
| @allow_null_origin | @allow_null_origin | ||||||
| @with_pool | @with_pool | ||||||
| @with_transaction() | @with_transaction() | ||||||
| @sale_order | @sale_order_application | ||||||
| def order(request, pool): | def get_associate_party(request, pool, contact_method: str): | ||||||
|     raise Exception(request) |     ContactMechanism = pool.get('party.contact_mechanism') | ||||||
|  |     associate_contact_method = ContactMechanism.search( | ||||||
|  |         [('value', 'ilike', contact_method)]) | ||||||
|  |     if associate_contact_method: | ||||||
|  |         response_data = { | ||||||
|  |             'id': associate_contact_method[0].id, | ||||||
|  |             'associate_party': associate_contact_method[0].party.id, | ||||||
|  |             'status': 'success', | ||||||
|  |             'type': associate_contact_method[0].type, | ||||||
|  |             'message': 'Associate Party found', | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return json.dumps(response_data), 200 | ||||||
|  |     response_data = { | ||||||
|  |             'id': None, | ||||||
|  |             'associate_party': None, | ||||||
|  |             'status': 'success', | ||||||
|  |             'type': None, | ||||||
|  |             'message': 'Associate Party not found', | ||||||
|  |     } | ||||||
|  |     return json.dumps(response_data), 404 | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @app.route( | ||||||
|  |     '/<database_name>/sale_order/order/<order>', methods=['GET']) | ||||||
|  | @allow_null_origin | ||||||
|  | @with_pool | ||||||
|  | @with_transaction() | ||||||
|  | @sale_order_application | ||||||
|  | def get_order(request, pool, order: int): | ||||||
|  |     Order = pool.get('sale.order') | ||||||
|  |     with Transaction().set_context({ | ||||||
|  |             'company': 1, | ||||||
|  |             'locations': [3]}): | ||||||
|  |         if request.method == 'GET': | ||||||
|  |             orders = Order.search_read([ | ||||||
|  |                 ('id', '=', order) | ||||||
|  |             ], order=[ | ||||||
|  |                 ('id', 'ASC') | ||||||
|  |             ], fields_names=[ | ||||||
|  |                 'id', | ||||||
|  |                 'party', | ||||||
|  |                 'lines' | ||||||
|  |             ]) | ||||||
|  |  | ||||||
|  |             return orders | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @app.route( | ||||||
|  |     '/<database_name>/sale_order/order', methods=['POST']) | ||||||
|  | @allow_null_origin | ||||||
|  | @with_pool | ||||||
|  | @with_transaction() | ||||||
|  | @sale_order_application | ||||||
|  | def post_order(request, pool): | ||||||
|  |     Order = pool.get('sale.order') | ||||||
|  |     with Transaction().set_context( | ||||||
|  |             {'company': 1, 'locations': [3]}): | ||||||
|  |         if request.method == 'POST': | ||||||
|  |             data = json.loads( | ||||||
|  |                 request.get_data().decode() | ||||||
|  |             ) | ||||||
|  |             order, = Order.create([dict(data)]) | ||||||
|  |  | ||||||
|  |             response_data = { | ||||||
|  |                     'id': order.id, | ||||||
|  |                     'status': 'success', | ||||||
|  |                     'message': 'Order created successfully', | ||||||
|  |             } | ||||||
|  |             return json.dumps(response_data), 201 | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @app.route( | ||||||
|  |     '/<database_name>/sale_order/<order>/order_line', methods=['POST']) | ||||||
|  | @allow_null_origin | ||||||
|  | @with_pool | ||||||
|  | @with_transaction() | ||||||
|  | @sale_order_application | ||||||
|  | def order_line(request, pool, order: int): | ||||||
|  |     OrderLine = pool.get('order.line') | ||||||
|  |     with Transaction().set_context( | ||||||
|  |             {'company': 1, | ||||||
|  |              'locations': [3]}): | ||||||
|  |         if request.method == 'POST': | ||||||
|  |             data = json.loads( | ||||||
|  |                 request.get_data().decode() | ||||||
|  |             ) | ||||||
|  |  | ||||||
|  |             order_lines = OrderLine.create([dict(data)]) | ||||||
|  |  | ||||||
|  |             response_data = { | ||||||
|  |                     'order_lines': [line.id for line in order_lines], | ||||||
|  |                     'status': 'success', | ||||||
|  |                     'message': 'Order lines created successfully', | ||||||
|  |             } | ||||||
|  |             return json.dumps(response_data), 201 | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @app.route( | ||||||
|  |     '/<database_name>/sale_order/confirm_order/<order>', methods=['POST']) | ||||||
|  | @allow_null_origin | ||||||
|  | @with_pool | ||||||
|  | @with_transaction() | ||||||
|  | @sale_order_application | ||||||
|  | def confirm_order(request, pool, order: int): | ||||||
|     Order = pool.get('sale.order') |     Order = pool.get('sale.order') | ||||||
|     if request.method == 'POST': |     if request.method == 'POST': | ||||||
|         data = json.loads( |         order, = Order.search([ | ||||||
|             request.get_data().decode() |             ('id', '=', order) | ||||||
|         ) |         ]) | ||||||
|  |  | ||||||
|         Order.create([dict(data)]) |         Order.confirm([order]) | ||||||
|  |  | ||||||
|  |         response_data = { | ||||||
|  |             'id': order.id, | ||||||
|  |             'status': 'success', | ||||||
|  |             'state': order.state, | ||||||
|  |             'message': 'Order confirm successfully', | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return json.dumps(response_data), 201 | ||||||
|   | |||||||
| @@ -33,12 +33,15 @@ class SaleOrder (Workflow, ModelView, ModelSQL): | |||||||
|     party = fields.Many2One( |     party = fields.Many2One( | ||||||
|         'party.party', "Party", required=True, states=_states) |         'party.party', "Party", required=True, states=_states) | ||||||
|     order_address = fields.Many2One( |     order_address = fields.Many2One( | ||||||
|         'party.address', 'Address', required=True, states=_states) |         'party.address', 'Address', states=_states) | ||||||
|     pickup_location = fields.Selection([ |     pickup_location = fields.Selection([ | ||||||
|         ("on_site", "On Site"), |         ("on_site", "On Site"), | ||||||
|         ("at_home", "At Home")], 'Pickup Location', states=_states) |         ("at_home", "At Home")], 'Pickup Location', states=_states) | ||||||
|     order_mobile = fields.Char('Mobile', states=_states) |     order_mobile = fields.Char('Mobile', states=_states) | ||||||
|     date = fields.Date("Date", required=True, states=_states) |     date = fields.Date("Date", required=True, states={ | ||||||
|  |         'readonly': Eval('state').in_(['confirmed', 'done']), | ||||||
|  |         'required': Eval('pickup_location') == 'at_home' | ||||||
|  |     }) | ||||||
|     lines = fields.One2Many( |     lines = fields.One2Many( | ||||||
|         'order.line', 'order', 'Lines', states=_states) |         'order.line', 'order', 'Lines', states=_states) | ||||||
|     total_order = fields.Function( |     total_order = fields.Function( | ||||||
|   | |||||||
							
								
								
									
										2
									
								
								setup.py
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								setup.py
									
									
									
									
									
								
							| @@ -107,7 +107,7 @@ setup(name=name, | |||||||
|         ], |         ], | ||||||
|     license='GPL-3', |     license='GPL-3', | ||||||
|     python_requires='>=3.8', |     python_requires='>=3.8', | ||||||
|     install_requires=requires, |     # install_requires=requires, | ||||||
|     extras_require={ |     extras_require={ | ||||||
|         'test': tests_require, |         'test': tests_require, | ||||||
|         }, |         }, | ||||||
|   | |||||||
							
								
								
									
										59
									
								
								tests/SaleOrderApiTest.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								tests/SaleOrderApiTest.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | |||||||
|  | import pudb | ||||||
|  | import requests | ||||||
|  | import json | ||||||
|  |  | ||||||
|  |  | ||||||
|  | url = 'http://localhost:8000' | ||||||
|  | key = 'f46f14d77db646b0ac0802e7bdab9cbb' + ( | ||||||
|  |        '1d53ad96387242e1918c45854dce5238707fed31daa64cab88569d119512153') + ( | ||||||
|  |        '64db6ced393b44f198ab9a3967b6f4ddf') | ||||||
|  | db = 'tryton' | ||||||
|  | application_name = 'sale_order' | ||||||
|  | base_url = '{}/{}/{}'.format(url, db, application_name) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | get_associate_party = requests.get( | ||||||
|  |     base_url + '/associate_party/alejandro.ayala@gmail.com', | ||||||
|  |     headers={ | ||||||
|  |         'Authorization': f'bearer {key}', | ||||||
|  |     }) | ||||||
|  |  | ||||||
|  | post_sale_order = requests.post( | ||||||
|  |             base_url + '/order', | ||||||
|  |             headers={ | ||||||
|  |                 'Authorization': f'bearer {key}', | ||||||
|  |             }, data=json.dumps({ | ||||||
|  |                 "party": 2573, | ||||||
|  |                 "pickup_location": 'on_site', | ||||||
|  |                 "lines": [[ | ||||||
|  |                     "create", [{ | ||||||
|  |                         "product": "1", | ||||||
|  |                         "unit": "1", | ||||||
|  |                         "quantity": "5", | ||||||
|  |                         "unitprice": "10" | ||||||
|  |                     }]]] | ||||||
|  |             }) | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | order = json.loads(json.loads(post_sale_order.text)[0]).get("id") | ||||||
|  |  | ||||||
|  | get_sale_order = requests.get( | ||||||
|  |     base_url + '/order/1', | ||||||
|  |     headers={ | ||||||
|  |         'Authorization': f'bearer {key}', | ||||||
|  |     }) | ||||||
|  |  | ||||||
|  | post_line_order = requests.post( | ||||||
|  |         base_url.replace( | ||||||
|  |             'sale_don_confiao', 'sale_order') + f'/{order}/order_line', | ||||||
|  |         headers={ | ||||||
|  |             'Authorization': f'bearer {key}', | ||||||
|  |             }, data=json.dumps({ | ||||||
|  |                 "order": order, | ||||||
|  |                 "product": "1", | ||||||
|  |                 "unit": "1", | ||||||
|  |                 "quantity": "5", | ||||||
|  |                 "unitprice": "10" | ||||||
|  |             })) | ||||||
|  |  | ||||||
|  | pudb.set_trace() | ||||||
| @@ -12,42 +12,231 @@ from trytond.tests.tools import activate_modules | |||||||
| class SaleOrderApiRouteTestCase(RouteTestCase): | class SaleOrderApiRouteTestCase(RouteTestCase): | ||||||
|     """Sale Order API Routes""" |     """Sale Order API Routes""" | ||||||
|  |  | ||||||
|     module = 'sale_order' |     module = "sale_order" | ||||||
|  |  | ||||||
|     key = uuid.uuid4().hex |     key = uuid.uuid4().hex | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     def setUpClass(cls): |     def setUpClass(cls): | ||||||
|         activate_modules('sale_order', create_company) |         activate_modules("sale_order", create_company) | ||||||
|  |  | ||||||
|     def setUp(self): |     def setUp(self): | ||||||
|         pool = Pool(DB_NAME) |         pool = Pool(DB_NAME) | ||||||
|         transaction = Transaction().start(DB_NAME, 0, _lock_tables=[ |         transaction = Transaction().start( | ||||||
|             'product_product']) |             DB_NAME, 0, _lock_tables=["product_product"]) | ||||||
|         with transaction: |         with transaction: | ||||||
|             Party = pool.get('party.party') |             Party = pool.get("party.party") | ||||||
|             ProductTemplate = pool.get('product.template') |             ProductTemplate = pool.get("product.template") | ||||||
|             Product = pool.get('product.product') |             Product = pool.get("product.product") | ||||||
|             Uom = pool.get('product.uom') |             Uom = pool.get("product.uom") | ||||||
|             self.productTemplate, =  ProductTemplate.create([{ |             Application = pool.get("res.user.application") | ||||||
|                 'name': 'Product', |  | ||||||
|                 'default_uom': Uom.search([('name', '=', 'Unit')])[0].id, |             Application( | ||||||
|             }]) |                 key=self.key, | ||||||
|  |                 user=1, | ||||||
|  |                 application="sale_order", | ||||||
|  |                 state="validated" | ||||||
|  |             ).save() | ||||||
|  |  | ||||||
|  |             self.unit = Uom.search([("name", "=", "Unit")])[0].id | ||||||
|  |             (self.productTemplate,) = ProductTemplate.create( | ||||||
|  |                 [ | ||||||
|  |                     { | ||||||
|  |                         "name": "Product", | ||||||
|  |                         "default_uom": self.unit, | ||||||
|  |                     } | ||||||
|  |                 ] | ||||||
|  |             ) | ||||||
|             self.product, = Product.create([ |             self.product, = Product.create([ | ||||||
|                 {'template': self.productTemplate.id} |                 {"template": self.productTemplate.id}]) | ||||||
|             ]) |  | ||||||
|  |  | ||||||
|             self.party, = Party.create([{ |             self.party, = Party.create([{ | ||||||
|                 'name': "Dunkan" |                 "name": "Dunkan", | ||||||
|  |                 "contact_mechanisms": [['create', [{ | ||||||
|  |                     'type': 'mobile', | ||||||
|  |                     'value': '3102223334' | ||||||
|  |                 }]]] | ||||||
|             }]) |             }]) | ||||||
|  |  | ||||||
|     def test_post_sale_order(self): |     def test_post_sale_order(self): | ||||||
|         client = self.client() |         client = self.client() | ||||||
|         response = client.post( |         response = client.post( | ||||||
|             f'/{self.db_name}/sale_order/order', |             f"/{self.db_name}/sale_order/order", | ||||||
|             headers={ |             headers={ | ||||||
|                 'Authorization': f'bearer {self.key}', |                 "Authorization": f"bearer {self.key}", | ||||||
|             }, data=json.dumps({ |             }, | ||||||
|                 "party": self.party.id, |             data=json.dumps( | ||||||
|             })) |                 { | ||||||
|  |                     "party": self.party.id, | ||||||
|  |                     "pickup_location": "on_site", | ||||||
|  |                     "lines": [ | ||||||
|  |                         [ | ||||||
|  |                             "create", | ||||||
|  |                             [ | ||||||
|  |                                 { | ||||||
|  |                                     "product": self.product.id, | ||||||
|  |                                     "unit": self.unit, | ||||||
|  |                                     "quantity": "5", | ||||||
|  |                                     "unitprice": "10", | ||||||
|  |                                 } | ||||||
|  |                             ], | ||||||
|  |                         ] | ||||||
|  |                     ], | ||||||
|  |                 } | ||||||
|  |             ), | ||||||
|  |         ) | ||||||
|  |  | ||||||
|         self.assertEqual(response.status_code, HTTPStatus.OK) |         self.assertEqual(response.status_code, HTTPStatus.OK) | ||||||
|  |  | ||||||
|  |     def test_get_sale_orders(self): | ||||||
|  |         client = self.client() | ||||||
|  |  | ||||||
|  |         order = json.loads( | ||||||
|  |             client.post( | ||||||
|  |                 f"/{self.db_name}/sale_order/order", | ||||||
|  |                 headers={ | ||||||
|  |                     "Authorization": f"bearer {self.key}", | ||||||
|  |                 }, | ||||||
|  |                 data=json.dumps( | ||||||
|  |                     { | ||||||
|  |                         "party": self.party.id, | ||||||
|  |                         "pickup_location": "on_site", | ||||||
|  |                         "lines": [ | ||||||
|  |                             [ | ||||||
|  |                                 "create", | ||||||
|  |                                 [ | ||||||
|  |                                     { | ||||||
|  |                                         "product": self.product.id, | ||||||
|  |                                         "unit": self.unit, | ||||||
|  |                                         "quantity": "5", | ||||||
|  |                                         "unitprice": "10", | ||||||
|  |                                     } | ||||||
|  |                                 ], | ||||||
|  |                             ] | ||||||
|  |                         ], | ||||||
|  |                     } | ||||||
|  |                 ), | ||||||
|  |             ) | ||||||
|  |             .get_data() | ||||||
|  |             .decode() | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         response = client.get( | ||||||
|  |             f"/{self.db_name}/sale_order/order/{json.loads(order[0])['id']}", | ||||||
|  |             headers={ | ||||||
|  |                 "Authorization": f"bearer {self.key}", | ||||||
|  |             }, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.assertEqual(response.status_code, HTTPStatus.OK) | ||||||
|  |         self.assertEqual(len(json.loads(response.text)), 1) | ||||||
|  |  | ||||||
|  |     def test_confirm_sale_order(self): | ||||||
|  |         client = self.client() | ||||||
|  |  | ||||||
|  |         order = json.loads( | ||||||
|  |             client.post( | ||||||
|  |                 f"/{self.db_name}/sale_order/order", | ||||||
|  |                 headers={ | ||||||
|  |                     "Authorization": f"bearer {self.key}", | ||||||
|  |                 }, | ||||||
|  |                 data=json.dumps( | ||||||
|  |                     { | ||||||
|  |                         "party": self.party.id, | ||||||
|  |                         "pickup_location": "on_site", | ||||||
|  |                         "lines": [ | ||||||
|  |                             [ | ||||||
|  |                                 "create", | ||||||
|  |                                 [ | ||||||
|  |                                     { | ||||||
|  |                                         "product": self.product.id, | ||||||
|  |                                         "unit": self.unit, | ||||||
|  |                                         "quantity": "5", | ||||||
|  |                                         "unitprice": "10", | ||||||
|  |                                     } | ||||||
|  |                                 ], | ||||||
|  |                             ] | ||||||
|  |                         ], | ||||||
|  |                     } | ||||||
|  |                 ), | ||||||
|  |             ) | ||||||
|  |             .get_data() | ||||||
|  |             .decode() | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         confirm_url = f"/{self.db_name}/sale_order/confirm_order/" | ||||||
|  |         order_id = json.loads(order[0])["id"] | ||||||
|  |         response = client.post( | ||||||
|  |             f"{confirm_url}{order_id}", | ||||||
|  |             headers={ | ||||||
|  |                 "Authorization": f"bearer {self.key}", | ||||||
|  |             }, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         response_data = json.loads(json.loads(response.text)[0]) | ||||||
|  |  | ||||||
|  |         self.assertEqual(response.status_code, HTTPStatus.OK) | ||||||
|  |         self.assertEqual(response_data["state"], "confirmed") | ||||||
|  |  | ||||||
|  |     def test_post_line_order(self): | ||||||
|  |         client = self.client() | ||||||
|  |  | ||||||
|  |         response = client.post( | ||||||
|  |             f"/{self.db_name}/sale_order/order", | ||||||
|  |             headers={"Authorization": f"bearer {self.key}"}, | ||||||
|  |             data=json.dumps({ | ||||||
|  |                 "party": self.party.id, | ||||||
|  |                 "pickup_location": "on_site"}), | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         order = json.loads(json.loads(response.text)[0]).get("id") | ||||||
|  |  | ||||||
|  |         response = client.post( | ||||||
|  |             f"/{self.db_name}/sale_order/{order}/order_line", | ||||||
|  |             headers={ | ||||||
|  |                 "Authorization": f"bearer {self.key}", | ||||||
|  |             }, | ||||||
|  |             data=json.dumps( | ||||||
|  |                 { | ||||||
|  |                     "order": order, | ||||||
|  |                     "product": self.product.id, | ||||||
|  |                     "unit": self.unit, | ||||||
|  |                     "quantity": "5", | ||||||
|  |                     "unitprice": "10", | ||||||
|  |                 } | ||||||
|  |             ), | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.assertEqual(response.status_code, HTTPStatus.OK) | ||||||
|  |  | ||||||
|  |     def test_get_party_asociate_contact_method(self): | ||||||
|  |         """ | ||||||
|  |         Recibe un numbero de telofono y retorna el tercero asociado | ||||||
|  |         a este numero de telofono. | ||||||
|  |         Args: | ||||||
|  |             phone: string | ||||||
|  |         Return: | ||||||
|  |             party: int | ||||||
|  |         """ | ||||||
|  |         client = self.client() | ||||||
|  |         response = client.get( | ||||||
|  |             f"/{self.db_name}/sale_order/associate_party/3102223334", | ||||||
|  |             headers={ | ||||||
|  |                 "Authorization": f"bearer {self.key}", | ||||||
|  |             }, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.assertEqual(response.status_code, HTTPStatus.OK) | ||||||
|  |         self.assertEqual(json.loads(response.text)[1], 200) | ||||||
|  |         self.assertEqual(json.loads(json.loads(response.text)[0])[ | ||||||
|  |                          'associate_party'], 2) | ||||||
|  |  | ||||||
|  |     def test_create_contact_method(self): | ||||||
|  |         """ | ||||||
|  |         Crea un tercero asociandolo a un metodo de contacto | ||||||
|  |  | ||||||
|  |         Args: | ||||||
|  |             email, phone : string | ||||||
|  |         Return: | ||||||
|  |             party: int | ||||||
|  |         """ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user