From ae81d17ed59c90b843b4333cb8775a98e273d1a9 Mon Sep 17 00:00:00 2001 From: Rodia Date: Sat, 22 Mar 2025 15:48:20 -0500 Subject: [PATCH] feat: Delete Order, Delete Order Line --- routes.py | 160 ++++++++++++++++++++--------------- tests/SaleOrderApiTest.py | 92 +++++++++++--------- tests/test_api_sale_order.py | 71 +++++++++++++--- 3 files changed, 206 insertions(+), 117 deletions(-) diff --git a/routes.py b/routes.py index bc88bd7..0464753 100644 --- a/routes.py +++ b/routes.py @@ -4,138 +4,162 @@ from trytond.protocols.wrappers import ( with_pool, with_transaction, user_application, - allow_null_origin) + allow_null_origin, +) import json -sale_order_application = user_application('sale_order') +sale_order_application = user_application("sale_order") @app.route( - '//sale_order/associate_party/', methods=[ + "//sale_order/associate_party/", methods=[ "GET"]) @allow_null_origin @with_pool @with_transaction() @sale_order_application def get_associate_party(request, pool, contact_method: str): - ContactMechanism = pool.get('party.contact_mechanism') + ContactMechanism = pool.get("party.contact_mechanism") associate_contact_method = ContactMechanism.search( - [('value', 'ilike', contact_method)]) + [("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', + "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', + "id": None, + "associate_party": None, + "status": "success", + "type": None, + "message": "Associate Party not found", } return json.dumps(response_data), 404 -@app.route( - '//sale_order/order/', methods=['GET']) +@app.route("//sale_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' - ]) + 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( - '//sale_order/order', methods=['POST']) +@app.route("//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)]) + 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', + "id": order.id, + "status": "success", + "message": "Order created successfully", } return json.dumps(response_data), 201 -@app.route( - '//sale_order//order_line', methods=['POST']) +@app.route("//sale_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() - ) + 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', + "order_lines": [line.id for line in order_lines], + "status": "success", + "message": "Order lines created successfully", } return json.dumps(response_data), 201 -@app.route( - '//sale_order/confirm_order/', methods=['POST']) +@app.route("//sale_order/confirm_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') - if request.method == 'POST': - order, = Order.search([ - ('id', '=', order) - ]) + Order = pool.get("sale.order") + if request.method == "POST": + (order,) = Order.search([("id", "=", order)]) Order.confirm([order]) response_data = { - 'id': order.id, - 'status': 'success', - 'state': order.state, - 'message': 'Order confirm successfully', + "id": order.id, + "status": "success", + "state": order.state, + "message": "Order confirm successfully", } return json.dumps(response_data), 201 + + +@app.route("//sale_order/", methods=["DELETE"]) +@allow_null_origin +@with_pool +@with_transaction() +@sale_order_application +def delete_order(request, pool, order: int): + Order = pool.get("sale.order") + if request.method == "DELETE": + (order,) = Order.search([("id", "=", order)]) + order.delete([order]) + + response_data = { + "id": order.id, + "status": "success", + "message": "Order deleted successfully", + } + return json.dumps(response_data), 200 + + +@app.route( + "//sale_order//order_line/", methods=[ + "DELETE"]) +@allow_null_origin +@with_pool +@with_transaction() +@sale_order_application +def delete_order_line(request, pool, order: int, order_line: int): + OrderLine = pool.get("order.line") + if request.method == "DELETE": + (order_line,) = OrderLine.search([("id", "=", order_line)]) + order_line.delete([order_line]) + + response_data = { + "id": order_line.id, + "status": "success", + "message": "Order line deleted successfully", + } + return json.dumps(response_data), 200 diff --git a/tests/SaleOrderApiTest.py b/tests/SaleOrderApiTest.py index ac39996..72fbb52 100644 --- a/tests/SaleOrderApiTest.py +++ b/tests/SaleOrderApiTest.py @@ -3,57 +3,73 @@ 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) +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', + base_url + "/associate_party/alejandro.ayala@gmail.com", headers={ - 'Authorization': f'bearer {key}', - }) + "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" - }]]] - }) + 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', + base_url + "/order/1", headers={ - 'Authorization': f'bearer {key}', - }) + "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" - })) + 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() diff --git a/tests/test_api_sale_order.py b/tests/test_api_sale_order.py index c999d64..6f43cea 100644 --- a/tests/test_api_sale_order.py +++ b/tests/test_api_sale_order.py @@ -47,16 +47,22 @@ class SaleOrderApiRouteTestCase(RouteTestCase): } ] ) - self.product, = Product.create([ - {"template": self.productTemplate.id}]) + (self.product,) = Product.create([ + {"template": self.productTemplate.id} + ]) - self.party, = Party.create([{ - "name": "Dunkan", - "contact_mechanisms": [['create', [{ - 'type': 'mobile', - 'value': '3102223334' - }]]] - }]) + (self.party,) = Party.create( + [ + { + "name": "Dunkan", + "contact_mechanisms": [[ + "create", [{ + "type": "mobile", + "value": "3102223334"}]] + ], + } + ] + ) def test_post_sale_order(self): client = self.client() @@ -228,8 +234,10 @@ class SaleOrderApiRouteTestCase(RouteTestCase): 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) + self.assertEqual(json.loads( + json.loads( + response.text + )[0])["associate_party"], 2) def test_create_contact_method(self): """ @@ -240,3 +248,44 @@ class SaleOrderApiRouteTestCase(RouteTestCase): Return: party: int """ + + def test_delete_sale(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() + ) + + order_id = json.loads(order[0])["id"] # Get the id of the order + delete_order = client.delete( + f"/{self.db_name}/sale_order/{order_id}", + headers={"Authorization": f"bearer {self.key}"}, + ) + + self.assertEqual(delete_order.status_code, HTTPStatus.OK) + self.assertEqual(json.loads(delete_order.text)[1], 200)