summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1/product.py
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2022-10-11 15:58:58 +0700
committerIT Fixcomart <it@fixcomart.co.id>2022-10-11 15:58:58 +0700
commitdae117ce9bb219557c9a4fc995e93bc4a88ea03f (patch)
tree62f51e1c8290651606759fc8d31a2662e7878590 /indoteknik_api/controllers/api_v1/product.py
parentfd6af0fbd83042c8471c3c58ff459f52bed45938 (diff)
init commit
Diffstat (limited to 'indoteknik_api/controllers/api_v1/product.py')
-rw-r--r--indoteknik_api/controllers/api_v1/product.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py
new file mode 100644
index 00000000..068a54b6
--- /dev/null
+++ b/indoteknik_api/controllers/api_v1/product.py
@@ -0,0 +1,82 @@
+from .. import controller
+from odoo import http
+from odoo.http import request
+
+
+class Product(controller.Controller):
+ prefix = '/api/v1/'
+
+ @http.route(prefix + 'product', auth='public', methods=['GET'])
+ def get_product(self, **kw):
+ if not self.authenticate():
+ return self.response(code=401, description='Unauthorized')
+
+ name = kw.get('name')
+ manufactures = kw.get('manufactures')
+ categories = kw.get('categories')
+
+ require_betweens = ['name', 'manufactures', 'categories']
+ is_fulfill = False
+ for required in require_betweens:
+ if kw.get(required):
+ is_fulfill = True
+
+ # If not fulfill in require_between
+ if not is_fulfill:
+ return self.response(code=400, description='name or manufactures or categories is required')
+
+ query = [('sale_ok', '=', True)]
+
+ if name:
+ name = '%' + name.replace(' ', '%') + '%'
+ query += [
+ '|',
+ ('default_code', 'ilike', name),
+ ('name', 'ilike', name),
+ ]
+
+ if manufactures:
+ query.append(('x_manufacture', 'in', [int(x) for x in manufactures.split(',')]))
+
+ if categories:
+ query.append(('public_categ_ids', 'child_of', [int(x) for x in categories.split(',')]))
+
+ product_variants = request.env['product.product'].search(query)
+ product_variant_ids = [x.id for x in product_variants]
+
+ query = [('product_variant_ids', 'in', product_variant_ids)]
+ limit = int(kw.get('limit', 0))
+ offset = int(kw.get('offset', 0))
+ order = kw.get('order')
+ if order == 'price-desc':
+ order = 'web_price_sorting desc'
+ elif order == 'price-asc':
+ order = 'web_price_sorting asc'
+ elif order == 'latest':
+ order = 'create_date desc'
+ else:
+ order = 'product_rating desc'
+ product_templates = request.env['product.template'].search(query, limit=limit, offset=offset, order=order)
+ data = {
+ 'product_total': request.env['product.template'].search_count(query),
+ 'products': [request.env['product.template'].api_single_response(x) for x in product_templates]
+ }
+ return self.response(data)
+
+ @http.route(prefix + 'product/<id>', auth='public', methods=['GET'])
+ def get_product_by_id(self, **kw):
+ if not self.authenticate():
+ return self.response(code=401, description='Unauthorized')
+
+ id = kw.get('id')
+ if not id:
+ return self.response(code=400, description='id is required')
+
+ data = []
+ id = [int(x) for x in id.split(',')]
+ product_templates = request.env['product.template'].search([('id', 'in', id)])
+ if product_templates:
+ data = [request.env['product.template'].api_single_response(x, with_detail=True) for x in product_templates]
+
+ return self.response(data)
+ \ No newline at end of file