From a6b1dbb36267d9fa213a4f85c9ffa05acfdd8418 Mon Sep 17 00:00:00 2001 From: ngkiencuong <85164180+ngkiencuong@users.noreply.github.com> Date: Tue, 2 Apr 2024 00:36:30 +0700 Subject: [PATCH] Update CRUD and README --- sample-api-python-code/README.md | 52 +++++++++++++++++++ sample-api-python-code/product_template.py | 57 ++++++++++++++++---- sample-api-python-code/res_partner.py | 55 +++++++++++++++++--- sample-api-python-code/sale_order.py | 60 ++++++++++++++++++---- sample-api-python-code/stock_quant.py | 57 ++++++++++++++++---- 5 files changed, 245 insertions(+), 36 deletions(-) create mode 100644 sample-api-python-code/README.md diff --git a/sample-api-python-code/README.md b/sample-api-python-code/README.md new file mode 100644 index 0000000..355f747 --- /dev/null +++ b/sample-api-python-code/README.md @@ -0,0 +1,52 @@ +# Odoo CRUD Scripts + +This repository contains four Python scripts that demonstrate CRUD (Create, Read, Update, Delete) operations on different Odoo models using the `xmlrpc` library. + +## Prerequisites + +Before running these scripts, make sure you have the following: + +- Python installed on your system +- Access to an Odoo server and database + +## Setup + +1. Clone or download this repository to your local machine. +2. Open each Python script (`res_partner.py`, `product_template.py`, `sale_order.py`, and `stock_quant.py`) and replace the following placeholders with your actual values: + - `'your_database'`: Your Odoo database name + - `'your_username'`: Your Odoo username + - `'your_password'`: Your Odoo password + +## Usage + +To run a specific script, navigate to the repository folder in your terminal or command prompt, and execute the following command: + +python .py. + +Replace `` with the name of the script you want to run (e.g., `res_partner.py`, `product_template.py`, `sale_order.py`, or `stock_quant.py`). + +The script will prompt you to enter an action (`create`, `read`, `update`, or `delete`). Enter the desired action, and the script will execute the corresponding function for the specified Odoo model. + +**Note:** For the `create`, `update`, and `delete` actions, you might need to modify the default values (e.g., partner IDs, product IDs, location IDs) in the respective functions to match the data in your Odoo database. + +## Scripts + +### 1. `res_partner.py` + +This script demonstrates CRUD operations on the `res.partner` model in Odoo, which represents partners (customers, suppliers, etc.). + +### 2. `product_template.py` + +This script demonstrates CRUD operations on the `product.template` model in Odoo, which represents product templates. + +### 3. `sale_order.py` + +This script demonstrates CRUD operations on the `sale.order` model in Odoo, which represents sales orders. + +### 4. `stock_quant.py` + +This script demonstrates CRUD operations on the `stock.quant` model in Odoo, which represents stock quantities. + +## Dependencies + +These scripts use the `xmlrpc.client` module, which is part of the Python standard library and does not require any additional installation. \ No newline at end of file diff --git a/sample-api-python-code/product_template.py b/sample-api-python-code/product_template.py index 18ede21..9ca3d64 100644 --- a/sample-api-python-code/product_template.py +++ b/sample-api-python-code/product_template.py @@ -10,15 +10,54 @@ common = xmlrpc.client.ServerProxy(f'{url}/xmlrpc/2/common') uid = common.authenticate(db, username, password, {}) models = xmlrpc.client.ServerProxy(f'{url}/xmlrpc/2/object') +model_name = 'product.template' -# Search for all product templates -product_ids = models.execute_kw(db, uid, password, 'product.template', 'search', [[]]) -# Read partner data -products = models.execute_kw(db, uid, password, 'product.template', 'read', [product_ids], {'fields': ['name', 'categ_id', 'list_price', 'type']}) +def create_product(): + product_data = { + 'name': 'New Product', + 'categ_id': 1, # Replace with the desired category ID + 'list_price': 10.0, + 'weight': 0.5, + } + product_id = models.execute_kw(db, uid, password, model_name, 'create', [product_data]) + print(f"Product created with ID: {product_id}") -# Display product template data -print("Product Templates:") -for product in products: - category_id, category_name = product['categ_id'] - print(f"Name: {product['name']}, Category: {category_name}, Price: {product['list_price']}, Type: {product['type']}") \ No newline at end of file + +def read_products(): + product_ids = models.execute_kw(db, uid, password, model_name, 'search', [[]]) + products = models.execute_kw(db, uid, password, model_name, 'read', [product_ids], ['name', 'categ_id', 'list_price', 'weight']) + print("Product Templates:") + for product in products: + category_id, category_name = product['categ_id'] + print(f"Name: {product['name']}, Category: {category_name}, Price: {product['list_price']}, Weight: {product['weight']}") + + +def update_product(): + product_id = 1 # Replace with the desired product template ID + product_data = { + 'name': 'Updated Product Name', + 'list_price': 15.0, + } + models.execute_kw(db, uid, password, model_name, 'write', [[product_id], product_data]) + print(f"Product with ID {product_id} updated successfully") + + +def delete_product(): + product_id = 1 # Replace with the desired product template ID + models.execute_kw(db, uid, password, model_name, 'unlink', [[product_id]]) + print(f"Product with ID {product_id} deleted successfully") + + +if __name__ == '__main__': + action = input("Enter action (create, read, update, delete): ").lower() + if action == 'create': + create_product() + elif action == 'read': + read_products() + elif action == 'update': + update_product() + elif action == 'delete': + delete_product() + else: + print("Invalid action. Please use create, read, update, or delete.") diff --git a/sample-api-python-code/res_partner.py b/sample-api-python-code/res_partner.py index d98619f..b9661be 100644 --- a/sample-api-python-code/res_partner.py +++ b/sample-api-python-code/res_partner.py @@ -10,14 +10,53 @@ common = xmlrpc.client.ServerProxy(f'{url}/xmlrpc/2/common') uid = common.authenticate(db, username, password, {}) models = xmlrpc.client.ServerProxy(f'{url}/xmlrpc/2/object') +model_name = 'res.partner' -# Search for all partners -partner_ids = models.execute_kw(db, uid, password, 'res.partner', 'search', [[]]) -# Read partner data -partners = models.execute_kw(db, uid, password, 'res.partner', 'read', [partner_ids], {'fields': ['name', 'email', 'phone']}) +def create_partner(): + partner_data = { + 'name': 'New Partner', + 'email': 'new_partner@example.com', + 'phone': '1234567890', + } + partner_id = models.execute_kw(db, uid, password, model_name, 'create', [partner_data]) + print(f"Partner created with ID: {partner_id}") + + +def read_partners(): + partner_ids = models.execute_kw(db, uid, password, model_name, 'search', [[]]) + partners = models.execute_kw(db, uid, password, model_name, 'read', [partner_ids], ['name', 'email', 'phone']) + print("Partners:") + for partner in partners: + print(f"Name: {partner['name']}, Email: {partner['email']}, Phone: {partner['phone']}") + + +def update_partner(): + partner_id = 1 # Replace with the desired partner ID + partner_data = { + 'name': 'Updated Partner Name', + 'phone': '9876543210', + } + models.execute_kw(db, uid, password, model_name, 'write', [[partner_id], partner_data]) + print(f"Partner with ID {partner_id} updated successfully") + + +def delete_partner(): + partner_id = 1 # Replace with the desired partner ID + models.execute_kw(db, uid, password, model_name, 'unlink', [[partner_id]]) + print(f"Partner with ID {partner_id} deleted successfully") + + +if __name__ == '__main__': + action = input("Enter action (create, read, update, delete): ").lower() + if action == 'create': + create_partner() + elif action == 'read': + read_partners() + elif action == 'update': + update_partner() + elif action == 'delete': + delete_partner() + else: + print("Invalid action. Please use create, read, update, or delete.") -# Display partner data -print("Partners:") -for partner in partners: - print(f"Name: {partner['name']}, Email: {partner['email']}, Phone: {partner['phone']}") diff --git a/sample-api-python-code/sale_order.py b/sample-api-python-code/sale_order.py index 540ff65..8f5fab9 100644 --- a/sample-api-python-code/sale_order.py +++ b/sample-api-python-code/sale_order.py @@ -10,15 +10,57 @@ common = xmlrpc.client.ServerProxy(f'{url}/xmlrpc/2/common') uid = common.authenticate(db, username, password, {}) models = xmlrpc.client.ServerProxy(f'{url}/xmlrpc/2/object') +model_name = 'sale.order' -# Search for all sale orders -sale_ids = models.execute_kw(db, uid, password, 'sale.order', 'search', [[]]) -# Read partner data -sales = models.execute_kw(db, uid, password, 'sale.order', 'read', [sale_ids], {'fields': ['name', 'partner_id', 'amount_total']}) +def create_sale_order(): + sale_order_data = { + 'partner_id': 1, # Replace with the desired partner ID + 'order_line': [ + (0, 0, { + 'product_id': 1, # Replace with the desired product ID + 'product_uom_qty': 2, + 'price_unit': 10.0, + }), + ], + } + sale_order_id = models.execute_kw(db, uid, password, model_name, 'create', [sale_order_data]) + print(f"Sale Order created with ID: {sale_order_id}") -# Display sale order data -print("Sale Orders:") -for sale in sales: - partner_id, partner_name = sale['partner_id'] - print(f"Order: {sale['name']}, Customer: {partner_name}, Total: {sale['amount_total']}") \ No newline at end of file + +def read_sale_orders(): + sale_order_ids = models.execute_kw(db, uid, password, model_name, 'search', [[]]) + sale_orders = models.execute_kw(db, uid, password, model_name, 'read', [sale_order_ids], ['name', 'partner_id', 'amount_total']) + print("Sale Orders:") + for sale_order in sale_orders: + partner_id, partner_name = sale_order['partner_id'] + print(f"Order: {sale_order['name']}, Customer: {partner_name}, Total: {sale_order['amount_total']}") + + +def update_sale_order(): + sale_order_id = 1 # Replace with the desired sale order ID + sale_order_data = { + 'partner_id': 2, # Replace with the desired partner ID + } + models.execute_kw(db, uid, password, model_name, 'write', [[sale_order_id], sale_order_data]) + print(f"Sale Order with ID {sale_order_id} updated successfully") + + +def delete_sale_order(): + sale_order_id = 1 # Replace with the desired sale order ID + models.execute_kw(db, uid, password, model_name, 'unlink', [[sale_order_id]]) + print(f"Sale Order with ID {sale_order_id} deleted successfully") + + +if __name__ == '__main__': + action = input("Enter action (create, read, update, delete): ").lower() + if action == 'create': + create_sale_order() + elif action == 'read': + read_sale_orders() + elif action == 'update': + update_sale_order() + elif action == 'delete': + delete_sale_order() + else: + print("Invalid action. Please use create, read, update, or delete.") diff --git a/sample-api-python-code/stock_quant.py b/sample-api-python-code/stock_quant.py index c97ef31..9f73ea0 100644 --- a/sample-api-python-code/stock_quant.py +++ b/sample-api-python-code/stock_quant.py @@ -10,16 +10,53 @@ common = xmlrpc.client.ServerProxy(f'{url}/xmlrpc/2/common') uid = common.authenticate(db, username, password, {}) models = xmlrpc.client.ServerProxy(f'{url}/xmlrpc/2/object') +model_name = 'stock.quant' -# Search for all stock quants -quant_ids = models.execute_kw(db, uid, password, 'stock.quant', 'search', [[]]) -# Read partner data -quants = models.execute_kw(db, uid, password, 'stock.quant', 'read', [quant_ids], {'fields': ['product_id', 'location_id', 'quantity']}) +def create_stock_quant(): + stock_quant_data = { + 'product_id': 1, # Replace with the desired product ID + 'location_id': 1, # Replace with the desired location ID + 'quantity': 10.0, + } + stock_quant_id = models.execute_kw(db, uid, password, model_name, 'create', [stock_quant_data]) + print(f"Stock Quant created with ID: {stock_quant_id}") -# Display stock quant data -print("Stock Quants:") -for quant in quants: - product_id, product_name = quant['product_id'] - location_id, location_name = quant['location_id'] - print(f"Product: {product_name}, Location: {location_name}, Quantity: {quant['quantity']}") \ No newline at end of file + +def read_stock_quants(): + stock_quant_ids = models.execute_kw(db, uid, password, model_name, 'search', [[]]) + stock_quants = models.execute_kw(db, uid, password, model_name, 'read', stock_quant_ids, ['product_id', 'location_id', 'quantity']) + print("Stock Quants:") + for stock_quant in stock_quants: + product_id, product_name = stock_quant['product_id'] + location_id, location_name = stock_quant['location_id'] + print(f"Product: {product_name}, Location: {location_name}, Quantity: {stock_quant['quantity']}") + + +def update_stock_quant(): + stock_quant_id = 1 # Replace with the desired stock quant ID + stock_quant_data = { + 'quantity': 20.0, + } + models.execute_kw(db, uid, password, model_name, 'write', [[stock_quant_id], stock_quant_data]) + print(f"Stock Quant with ID {stock_quant_id} updated successfully") + + +def delete_stock_quant(): + stock_quant_id = 1 # Replace with the desired stock quant ID + models.execute_kw(db, uid, password, model_name, 'unlink', [[stock_quant_id]]) + print(f"Stock Quant with ID {stock_quant_id} deleted successfully") + + +if __name__ == '__main__': + action = input("Enter action (create, read, update, delete): ").lower() + if action == 'create': + create_stock_quant() + elif action == 'read': + read_stock_quants() + elif action == 'update': + update_stock_quant() + elif action == 'delete': + delete_stock_quant() + else: + print("Invalid action. Please use create, read, update, or delete.")