diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2022-10-21 10:32:47 +0700 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2022-10-21 10:32:47 +0700 |
| commit | 5f700464f6bbc7b38438a0a527554327f390fe78 (patch) | |
| tree | d057935ea016ee4a3a97f23c3ee2d9b715a8db7b /indoteknik_api/controllers/api_v1 | |
| parent | 752768d6cc29a231b47637653d4f3e4e293a4590 (diff) | |
Add 'attribute' on product detail result api
Add function get similar product by id
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/product.py | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 493677fd..bd67e380 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -47,17 +47,9 @@ class Product(controller.Controller): query = [('product_variant_ids', 'in', product_variant_ids)] limit = int(kw.get('limit', 0)) offset = int(kw.get('offset', 0)) - order = kw.get('order') + order = self.get_product_default_order(kw.get('order')) - orders = ['product_rating desc'] - if order != 'price-asc': - orders.append('web_price_sorting desc') - if order == 'price-asc': - orders.append('web_price_sorting asc') - elif order == 'latest': - orders.append('create_date desc') - orders = ','.join(orders) - product_templates = request.env['product.template'].search(query, limit=limit, offset=offset, order=orders) + 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] @@ -80,4 +72,47 @@ class Product(controller.Controller): 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 + + @http.route(prefix + 'product/<id>/similar', auth='public', methods=['GET']) + def get_product_similar_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') + + id = int(id) + product_template = request.env['product.template'].search([('id', '=', id)]) + + if not product_template: return self.response([]) + + query = [] + if product_template.x_manufacture: + query.append(('x_manufacture', '=', product_template.x_manufacture.id)) + if product_template.public_categ_ids: + query.append(('public_categ_ids', 'in', [x.id for x in product_template.public_categ_ids])) + if len(query) == 2: + query.insert(0, '|') + + limit = int(kw.get('limit', 0)) + offset = int(kw.get('offset', 0)) + order = self.get_product_default_order(kw.get('order')) + 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) + + def get_product_default_order(self, order): + orders = ['product_rating desc'] + if order != 'price-asc': + orders.append('web_price_sorting desc') + if order == 'price-asc': + orders.append('web_price_sorting asc') + elif order == 'latest': + orders.append('create_date desc') + return ','.join(orders) +
\ No newline at end of file |
