trytondo-sale_order/routes.py

142 lines
3.9 KiB
Python

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
sale_order_application = user_application('sale_order')
@app.route(
'/<database_name>/sale_order/associate_party/<contact_method>', 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')
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')
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',
}
return json.dumps(response_data), 201