diff --git a/__init__.py b/__init__.py index adfee55..1ad9771 100644 --- a/__init__.py +++ b/__init__.py @@ -1,11 +1,12 @@ from trytond.pool import Pool -from . import sale_order +from . import sale_order, user __all__ = ['register'] def register(): Pool.register( + user.UserApplication, sale_order.SaleOrder, sale_order.OrderLine, module='sale_order', type_='model') diff --git a/routes.py b/routes.py new file mode 100644 index 0000000..3878296 --- /dev/null +++ b/routes.py @@ -0,0 +1,25 @@ +from trytond.wsgi import app +from trytond.protocols.wrappers import ( + with_pool, + with_transaction, + user_application, + allow_null_origin) +import json + +sale_order = user_application('sale_order') + + +@app.route('//sale_order/order', methods=['POST']) +@allow_null_origin +@with_pool +@with_transaction() +@sale_order +def order(request, pool): + raise Exception(request) + Order = pool.get('sale.order') + if request.method == 'POST': + data = json.loads( + request.get_data().decode() + ) + + Order.create([dict(data)]) diff --git a/tests/test_api_sale_order.py b/tests/test_api_sale_order.py new file mode 100644 index 0000000..35b8fd7 --- /dev/null +++ b/tests/test_api_sale_order.py @@ -0,0 +1,53 @@ +from trytond.tests.test_tryton import RouteTestCase +from trytond.pool import Pool +from http import HTTPStatus +import uuid +import json +from trytond.tests.test_tryton import DB_NAME +from trytond.transaction import Transaction +from trytond.modules.company.tests.tools import create_company +from trytond.tests.tools import activate_modules + + +class SaleOrderApiRouteTestCase(RouteTestCase): + """Sale Order API Routes""" + + module = 'sale_order' + key = uuid.uuid4().hex + + @classmethod + def setUpClass(cls): + activate_modules('sale_order', create_company) + + def setUp(self): + pool = Pool(DB_NAME) + transaction = Transaction().start(DB_NAME, 0, _lock_tables=[ + 'product_product']) + with transaction: + Party = pool.get('party.party') + ProductTemplate = pool.get('product.template') + Product = pool.get('product.product') + Uom = pool.get('product.uom') + self.productTemplate, = ProductTemplate.create([{ + 'name': 'Product', + 'default_uom': Uom.search([('name', '=', 'Unit')])[0].id, + }]) + self.product, = Product.create([ + {'template': self.productTemplate.id} + ]) + + self.party, = Party.create([{ + 'name': "Dunkan" + }]) + + def test_post_sale_order(self): + client = self.client() + response = client.post( + f'/{self.db_name}/sale_order/order', + headers={ + 'Authorization': f'bearer {self.key}', + }, data=json.dumps({ + "party": self.party.id, + })) + + self.assertEqual(response.status_code, HTTPStatus.OK) diff --git a/user.py b/user.py new file mode 100644 index 0000000..3b9de2f --- /dev/null +++ b/user.py @@ -0,0 +1,14 @@ +# This file is part of Tryton. The COPYRIGHT file at the top level of +# this repository contains the full copyright notices and license terms. +from trytond.pool import PoolMeta + + +class UserApplication(metaclass=PoolMeta): + __name__ = 'res.user.application' + + @classmethod + def __setup__(cls): + super(UserApplication, cls).__setup__() + cls.application.selection.append( + ('sale_order', 'SaleOrder') + )