from trytond.wsgi import app from trytond.transaction import Transaction from trytond.protocols.wrappers import ( with_pool, with_transaction, user_application, allow_null_origin) import json don_confiao_application = user_application( 'sale_don_confiao') @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': parties = Party.search_read( [], order=[('id', 'ASC')], fields_names=['id', 'name']) return parties if request.method == 'POST': data = json.loads( request.get_data().decode() ) Party.create([dict(data)]) @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']) return parties @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' ]) return categories @app.route('//sale_don_confiao/products', methods=['GET']) @allow_null_origin @with_pool @with_transaction() @don_confiao_application def products(request, pool): Product = pool.get('product.product') products = Product.search_read([ ('salable', '=', True) ], order=[ ('id', 'ASC') ], fields_names=[ 'id', 'name' ]) return products @app.route( '//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', 'description', 'quantity', ]) return products @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', ]) return sales @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.create([dict(data)]) @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', ]) return sales