diff --git a/modules/sale_don_confiao/routes.py b/modules/sale_don_confiao/routes.py index 1454c6a..c9983ae 100644 --- a/modules/sale_don_confiao/routes.py +++ b/modules/sale_don_confiao/routes.py @@ -4,163 +4,161 @@ from trytond.protocols.wrappers import ( with_pool, with_transaction, user_application, - allow_null_origin) + allow_null_origin, +) import json -don_confiao_application = user_application( - 'sale_don_confiao') +don_confiao_application = user_application("sale_don_confiao") -@app.route('//sale_don_confiao/parties', methods=[ - 'GET', 'POST']) +@app.route("//sale_don_confiao/parties", methods=["GET", "POST"]) @allow_null_origin @with_pool @with_transaction() @don_confiao_application def parties(request, pool): - Party = pool.get('party.party') - if request.method == 'GET': + Party = pool.get("party.party") + if request.method == "GET": parties = Party.search_read( - [], - order=[('id', 'ASC')], - fields_names=['id', 'name']) + [], order=[("id", "ASC")], fields_names=["id", "name"] + ) return parties - if request.method == 'POST': - data = json.loads( - request.get_data().decode() - ) + if request.method == "POST": + data = json.loads(request.get_data().decode()) - Party.create([dict(data)]) + party = Party.create([dict(data)]) + + if party: + party = party[0].id + + response_data = { + "id": party, + "status": "success", + "message": "Party created successfully", + } + + return json.dumps(response_data), 201 + + response_data = { + "id": None, + "status": "success", + "message": "Party not created", + } + return json.dumps(response_data), 404 -@app.route( - '//sale_don_confiao/party/', - methods=['GET']) +@app.route("//sale_don_confiao/party/", methods=["GET"]) @allow_null_origin @with_pool @with_transaction() @don_confiao_application def party(request, pool, party: int): - Party = pool.get('party.party') - if request.method == 'GET': - parties = Party.search_read([ - ('id', '=', int(party)) - ], order=[ - ('id', 'ASC') - ], fields_names=[ - 'id', - 'name']) + Party = pool.get("party.party") + if request.method == "GET": + parties = Party.search_read( + [("id", "=", int(party))], + order=[("id", "ASC")], + fields_names=["id", "name"], + ) return parties -@app.route('//sale_don_confiao/categories', methods=['GET']) +@app.route("//sale_don_confiao/categories", methods=["GET"]) @allow_null_origin @with_pool @with_transaction() @don_confiao_application def categories(request, pool): - Category = pool.get('product.category') - categories = Category.search_read([ - ], order=[ - ('id', 'ASC') - ], fields_names=[ - 'id', - 'name' - ]) + Category = pool.get("product.category") + categories = Category.search_read( + [], order=[("id", "ASC")], fields_names=["id", "name"] + ) return categories -@app.route('//sale_don_confiao/products', methods=['GET']) +@app.route("//sale_don_confiao/products", methods=["GET"]) @allow_null_origin @with_pool @with_transaction() @don_confiao_application def products(request, pool): - with Transaction().set_context( - {'company': 1, 'locations': [3]}): - Product = pool.get('product.product') - products = Product.search_read([ - ('salable', '=', True) - ], order=[ - ('id', 'ASC') - ], fields_names=[ - 'id', - 'name', - 'template.list_price', - 'default_uom.name', - 'description', - 'quantity', - ]) + with Transaction().set_context({"company": 1, "locations": [3]}): + Product = pool.get("product.product") + products = Product.search_read( + [("salable", "=", True)], + order=[("id", "ASC")], + fields_names=[ + "id", + "name", + "template.list_price", + "default_uom.name", + "description", + "quantity", + ], + ) return products @app.route( - '//sale_don_confiao/search_products/', - methods=['GET']) + "//sale_don_confiao/search_products/", methods=["GET"] +) @allow_null_origin @with_pool @with_transaction() @don_confiao_application def search_products(request, pool, product_name: str): - Product = pool.get('product.product') - with Transaction().set_context( - {'company': 1, 'locations': [3]}): - products = Product.search_read([ - ('name', 'ilike', f'%{product_name}%') - ], order=[ - ('id', 'ASC') - ], fields_names=[ - 'id', - 'name', - 'template.list_price', - 'default_uom.name', - 'description', - 'quantity', - ]) + Product = pool.get("product.product") + with Transaction().set_context({"company": 1, "locations": [3]}): + products = Product.search_read( + [("name", "ilike", f"%{product_name}%")], + order=[("id", "ASC")], + fields_names=[ + "id", + "name", + "template.list_price", + "default_uom.name", + "description", + "quantity", + ], + ) return products -@app.route( - '//sale_don_confiao/sale/', - methods=['GET']) +@app.route("//sale_don_confiao/sale/", methods=["GET"]) @allow_null_origin @with_pool @with_transaction() @don_confiao_application def sale(request, pool, sale_id: int): - Sale = pool.get('sale.sale') - if request.method == 'GET': - sales = Sale.search_read([ - ('id', '=', sale_id) - ], order=[ - ('id', 'ASC') - ], fields_names=[ - 'id', - 'party.name', - 'sale_date', - 'shipment_address.street', - 'untaxed_amount_cache', - 'tax_amount_cache', - 'total_amount_cache', - ]) + Sale = pool.get("sale.sale") + if request.method == "GET": + sales = Sale.search_read( + [("id", "=", sale_id)], + order=[("id", "ASC")], + fields_names=[ + "id", + "party.name", + "sale_date", + "shipment_address.street", + "untaxed_amount_cache", + "tax_amount_cache", + "total_amount_cache", + ], + ) return sales -@app.route( - '//sale_don_confiao/post_sale', - methods=['POST']) +@app.route("//sale_don_confiao/post_sale", methods=["POST"]) @allow_null_origin @with_pool @with_transaction() @don_confiao_application def post_sale(request, pool): - Sale = pool.get('sale.sale') - data = json.loads( - request.get_data().decode() - ) + Sale = pool.get("sale.sale") + data = json.loads(request.get_data().decode()) sales = Sale.create([dict(data)]) @@ -168,54 +166,43 @@ def post_sale(request, pool): return sales[0].id -@app.route('//sale_don_confiao/sales', methods=['GET']) +@app.route("//sale_don_confiao/sales", methods=["GET"]) @allow_null_origin @with_pool @with_transaction() @don_confiao_application def sales(request, pool): - Sale = pool.get('sale.sale') - sales = Sale.search_read([ - ], order=[ - ('id', 'ASC') - ], fields_names=[ - 'id', - 'party.name', - 'sale_date', - 'shipment_address.street', - 'untaxed_amount_cache', - 'tax_amount_cache', - 'total_amount_cache', - ]) + Sale = pool.get("sale.sale") + sales = Sale.search_read( + [], + order=[("id", "ASC")], + fields_names=[ + "id", + "party.name", + "sale_date", + "shipment_address.street", + "untaxed_amount_cache", + "tax_amount_cache", + "total_amount_cache", + ], + ) return sales -@app.route('//sale_don_confiao/payment_methods', methods=[ - 'GET']) +@app.route("//sale_don_confiao/payment_methods", methods=["GET"]) @allow_null_origin @with_pool @with_transaction() @don_confiao_application def payment_methods(request, pool): - if request.method == 'GET': + if request.method == "GET": payment_methods = [ - { - "text": "Efectivo", - "value": "CASH" - }, - { - "text": "Confiar", - "value": "CONFIAR" - }, - { - "text": "Bancolombia", - "value": "BANCOLOMBIA" - } + {"text": "Efectivo", "value": "CASH"}, + {"text": "Confiar", "value": "CONFIAR"}, + {"text": "Bancolombia", "value": "BANCOLOMBIA"}, ] - response_data = { - "payment_methods": payment_methods - } + response_data = {"payment_methods": payment_methods} return response_data, 200