feat: Delete Order, Delete Order Line

This commit is contained in:
Rodia 2025-03-22 15:48:20 -05:00
parent 09cabd6798
commit ae81d17ed5
3 changed files with 206 additions and 117 deletions

160
routes.py
View File

@ -4,138 +4,162 @@ from trytond.protocols.wrappers import (
with_pool, with_pool,
with_transaction, with_transaction,
user_application, user_application,
allow_null_origin) allow_null_origin,
)
import json import json
sale_order_application = user_application('sale_order') sale_order_application = user_application("sale_order")
@app.route( @app.route(
'/<database_name>/sale_order/associate_party/<contact_method>', methods=[ "/<database_name>/sale_order/associate_party/<contact_method>", methods=[
"GET"]) "GET"])
@allow_null_origin @allow_null_origin
@with_pool @with_pool
@with_transaction() @with_transaction()
@sale_order_application @sale_order_application
def get_associate_party(request, pool, contact_method: str): 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( associate_contact_method = ContactMechanism.search(
[('value', 'ilike', contact_method)]) [("value", "ilike", contact_method)]
)
if associate_contact_method: if associate_contact_method:
response_data = { response_data = {
'id': associate_contact_method[0].id, "id": associate_contact_method[0].id,
'associate_party': associate_contact_method[0].party.id, "associate_party": associate_contact_method[0].party.id,
'status': 'success', "status": "success",
'type': associate_contact_method[0].type, "type": associate_contact_method[0].type,
'message': 'Associate Party found', "message": "Associate Party found",
} }
return json.dumps(response_data), 200 return json.dumps(response_data), 200
response_data = { response_data = {
'id': None, "id": None,
'associate_party': None, "associate_party": None,
'status': 'success', "status": "success",
'type': None, "type": None,
'message': 'Associate Party not found', "message": "Associate Party not found",
} }
return json.dumps(response_data), 404 return json.dumps(response_data), 404
@app.route( @app.route("/<database_name>/sale_order/order/<order>", methods=["GET"])
'/<database_name>/sale_order/order/<order>', methods=['GET'])
@allow_null_origin @allow_null_origin
@with_pool @with_pool
@with_transaction() @with_transaction()
@sale_order_application @sale_order_application
def get_order(request, pool, order: int): def get_order(request, pool, order: int):
Order = pool.get('sale.order') Order = pool.get("sale.order")
with Transaction().set_context({ with Transaction().set_context({"company": 1, "locations": [3]}):
'company': 1, if request.method == "GET":
'locations': [3]}): orders = Order.search_read(
if request.method == 'GET': [("id", "=", order)],
orders = Order.search_read([ order=[("id", "ASC")],
('id', '=', order) fields_names=["id", "party", "lines"],
], order=[ )
('id', 'ASC')
], fields_names=[
'id',
'party',
'lines'
])
return orders return orders
@app.route( @app.route("/<database_name>/sale_order/order", methods=["POST"])
'/<database_name>/sale_order/order', methods=['POST'])
@allow_null_origin @allow_null_origin
@with_pool @with_pool
@with_transaction() @with_transaction()
@sale_order_application @sale_order_application
def post_order(request, pool): def post_order(request, pool):
Order = pool.get('sale.order') Order = pool.get("sale.order")
with Transaction().set_context( with Transaction().set_context({"company": 1, "locations": [3]}):
{'company': 1, 'locations': [3]}): if request.method == "POST":
if request.method == 'POST': data = json.loads(request.get_data().decode())
data = json.loads( (order,) = Order.create([dict(data)])
request.get_data().decode()
)
order, = Order.create([dict(data)])
response_data = { response_data = {
'id': order.id, "id": order.id,
'status': 'success', "status": "success",
'message': 'Order created successfully', "message": "Order created successfully",
} }
return json.dumps(response_data), 201 return json.dumps(response_data), 201
@app.route( @app.route("/<database_name>/sale_order/<order>/order_line", methods=["POST"])
'/<database_name>/sale_order/<order>/order_line', methods=['POST'])
@allow_null_origin @allow_null_origin
@with_pool @with_pool
@with_transaction() @with_transaction()
@sale_order_application @sale_order_application
def order_line(request, pool, order: int): def order_line(request, pool, order: int):
OrderLine = pool.get('order.line') OrderLine = pool.get("order.line")
with Transaction().set_context( with Transaction().set_context({"company": 1, "locations": [3]}):
{'company': 1, if request.method == "POST":
'locations': [3]}): data = json.loads(request.get_data().decode())
if request.method == 'POST':
data = json.loads(
request.get_data().decode()
)
order_lines = OrderLine.create([dict(data)]) order_lines = OrderLine.create([dict(data)])
response_data = { response_data = {
'order_lines': [line.id for line in order_lines], "order_lines": [line.id for line in order_lines],
'status': 'success', "status": "success",
'message': 'Order lines created successfully', "message": "Order lines created successfully",
} }
return json.dumps(response_data), 201 return json.dumps(response_data), 201
@app.route( @app.route("/<database_name>/sale_order/confirm_order/<order>", methods=[
'/<database_name>/sale_order/confirm_order/<order>', methods=['POST']) "POST"])
@allow_null_origin @allow_null_origin
@with_pool @with_pool
@with_transaction() @with_transaction()
@sale_order_application @sale_order_application
def confirm_order(request, pool, order: int): 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":
order, = Order.search([ (order,) = Order.search([("id", "=", order)])
('id', '=', order)
])
Order.confirm([order]) Order.confirm([order])
response_data = { response_data = {
'id': order.id, "id": order.id,
'status': 'success', "status": "success",
'state': order.state, "state": order.state,
'message': 'Order confirm successfully', "message": "Order confirm successfully",
} }
return json.dumps(response_data), 201 return json.dumps(response_data), 201
@app.route("/<database_name>/sale_order/<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(
"/<database_name>/sale_order/<order>/order_line/<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

View File

@ -3,57 +3,73 @@ import requests
import json import json
url = 'http://localhost:8000' url = "http://localhost:8000"
key = 'f46f14d77db646b0ac0802e7bdab9cbb' + ( key = (
'1d53ad96387242e1918c45854dce5238707fed31daa64cab88569d119512153') + ( "f46f14d77db646b0ac0802e7bdab9cbb"
'64db6ced393b44f198ab9a3967b6f4ddf') + ("1d53ad96387242e1918c45854dce5238707fed31daa64cab88569d119512153")
db = 'tryton' + ("64db6ced393b44f198ab9a3967b6f4ddf")
application_name = 'sale_order' )
base_url = '{}/{}/{}'.format(url, db, application_name) db = "tryton"
application_name = "sale_order"
base_url = "{}/{}/{}".format(url, db, application_name)
get_associate_party = requests.get( get_associate_party = requests.get(
base_url + '/associate_party/alejandro.ayala@gmail.com', base_url + "/associate_party/alejandro.ayala@gmail.com",
headers={ headers={
'Authorization': f'bearer {key}', "Authorization": f"bearer {key}",
}) },
)
post_sale_order = requests.post( post_sale_order = requests.post(
base_url + '/order', base_url + "/order",
headers={ headers={
'Authorization': f'bearer {key}', "Authorization": f"bearer {key}",
}, data=json.dumps({ },
"party": 2573, data=json.dumps(
"pickup_location": 'on_site', {
"lines": [[ "party": 2573,
"create", [{ "pickup_location": "on_site",
"product": "1", "lines": [
"unit": "1", [
"quantity": "5", "create",
"unitprice": "10" [
}]]] {
}) "product": "1",
"unit": "1",
"quantity": "5",
"unitprice": "10"
}],
]
],
}
),
) )
order = json.loads(json.loads(post_sale_order.text)[0]).get("id") order = json.loads(json.loads(post_sale_order.text)[0]).get("id")
get_sale_order = requests.get( get_sale_order = requests.get(
base_url + '/order/1', base_url + "/order/1",
headers={ headers={
'Authorization': f'bearer {key}', "Authorization": f"bearer {key}",
}) },
)
post_line_order = requests.post( post_line_order = requests.post(
base_url.replace( base_url.replace(
'sale_don_confiao', 'sale_order') + f'/{order}/order_line', "sale_don_confiao", "sale_order") + f"/{order}/order_line",
headers={ headers={
'Authorization': f'bearer {key}', "Authorization": f"bearer {key}",
}, data=json.dumps({ },
"order": order, data=json.dumps(
"product": "1", {
"unit": "1", "order": order,
"quantity": "5", "product": "1",
"unitprice": "10" "unit": "1",
})) "quantity": "5",
"unitprice": "10",
}
),
)
pudb.set_trace() pudb.set_trace()

View File

@ -47,16 +47,22 @@ class SaleOrderApiRouteTestCase(RouteTestCase):
} }
] ]
) )
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", [
"contact_mechanisms": [['create', [{ {
'type': 'mobile', "name": "Dunkan",
'value': '3102223334' "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()
@ -228,8 +234,10 @@ class SaleOrderApiRouteTestCase(RouteTestCase):
self.assertEqual(response.status_code, HTTPStatus.OK) self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(json.loads(response.text)[1], 200) self.assertEqual(json.loads(response.text)[1], 200)
self.assertEqual(json.loads(json.loads(response.text)[0])[ self.assertEqual(json.loads(
'associate_party'], 2) json.loads(
response.text
)[0])["associate_party"], 2)
def test_create_contact_method(self): def test_create_contact_method(self):
""" """
@ -240,3 +248,44 @@ class SaleOrderApiRouteTestCase(RouteTestCase):
Return: Return:
party: int 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)