diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-10-11 14:40:56 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-10-11 14:40:56 +0700 |
| commit | 53c28a95ef0ba225f83fadcb09aedb14c323b201 (patch) | |
| tree | 1118b9d2dfd39157238355f29a88e385820c9944 /indoteknik_api/controllers/api_v1 | |
| parent | 11d446d513b540184e02f26bca52b2c8e365c608 (diff) | |
Add get category breadcrumb API on product and category
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/category.py | 17 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/product.py | 182 |
2 files changed, 130 insertions, 69 deletions
diff --git a/indoteknik_api/controllers/api_v1/category.py b/indoteknik_api/controllers/api_v1/category.py index 3c5766e2..efa6b033 100644 --- a/indoteknik_api/controllers/api_v1/category.py +++ b/indoteknik_api/controllers/api_v1/category.py @@ -120,4 +120,19 @@ class Category(controller.Controller): 'name': category.name }) return self.response(data) -
\ No newline at end of file + + @http.route(prefix + 'category/<id>/category-breadcrumb', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def get_category_parents_by_id(self, id, **kw): + categories = [] + + category = request.env['product.public.category'].search([('id', '=', int(id))], limit=1) + if not category: + return self.response(code=400, description='Category not found') + + while category: + categories.append({'id': category.id, 'name': category.name}) + category = category.parent_frontend_id + + categories.reverse() + return self.response(categories)
\ No newline at end of file diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 2cbdede7..0c8a286b 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -2,7 +2,10 @@ from .. import controller from odoo import http from odoo.http import request from datetime import datetime, timedelta -import ast, logging, math, json +import ast +import logging +import math +import json _logger = logging.getLogger(__name__) @@ -10,20 +13,43 @@ _logger = logging.getLogger(__name__) class Product(controller.Controller): prefix = '/api/v1/' + @http.route(prefix + 'product/<id>/category-breadcrumb', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def get_product_categories_by_id(self, id, **kw): + categories = [] + + product = request.env['product.template'].search( + [('id', '=', int(id))], limit=1) + if not product: + return self.response(code=400, description='Product not found') + + if len(product.public_categ_ids) == 0: + return self.response(categories) + + category = product.public_categ_ids[0] + while category: + categories.append({'id': category.id, 'name': category.name}) + category = category.parent_frontend_id + + categories.reverse() + return self.response(categories) + @http.route(prefix + 'product_variant/<id>/stock', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() def get_product_template_stock_by_id(self, **kw): id = int(kw.get('id')) date_7_days_ago = datetime.now() - timedelta(days=7) - product = request.env['product.product'].search([('id', '=', id)], limit=1) - product_sla = request.env['product.sla'].search([('product_variant_id', '=', id)], limit=1) + product = request.env['product.product'].search( + [('id', '=', id)], limit=1) + product_sla = request.env['product.sla'].search( + [('product_variant_id', '=', id)], limit=1) stock_vendor = request.env['stock.vendor'].search([ ('product_variant_id', '=', id), ('write_date', '>=', date_7_days_ago.strftime("%Y-%m-%d %H:%M:%S")) ], limit=1) qty_available = product.qty_onhand_bandengan - + qty = 0 sla_date = '-' @@ -33,22 +59,23 @@ class Product(controller.Controller): qty_vendor = math.ceil(float(qty_vendor)) total_excell = qty_vendor - is_altama_product = product.x_manufacture.id in [10,122,89] + is_altama_product = product.x_manufacture.id in [10, 122, 89] if is_altama_product: try: # Qty Altama - qty_altama = request.env['product.template'].get_stock_altama(product.default_code) + qty_altama = request.env['product.template'].get_stock_altama( + product.default_code) qty_altama -= int(qty_altama * 0.1) qty_altama = math.ceil(float(qty_altama)) - total_adem = qty_altama - + total_adem = qty_altama + if qty_available > 0: qty = qty_available + total_adem + total_excell sla_date = '1 Hari' elif qty_altama > 0 or qty_vendor > 0: qty = total_adem if qty_altama > 0 else total_excell sla_date = '2-4 Hari' - else: + else: sla_date = '3-7 Hari' except: print('error') @@ -56,10 +83,9 @@ class Product(controller.Controller): if qty_available > 0: qty = qty_available sla_date = product_sla.sla or '-' - elif qty_vendor > 0: + elif qty_vendor > 0: qty = total_excell sla_date = '2-4 Hari' - data = { 'qty': qty, @@ -68,14 +94,14 @@ class Product(controller.Controller): return self.response(data, headers=[('Cache-Control', 'max-age=600, private')]) - @http.route(prefix + 'product/template/price/<id>', auth='public', methods=['GET', 'OPTIONS']) def get_product_template_price_by_id(self, **kw): if not self.authenticate(): return self.response(code=401, description='Unauthorized') id = kw.get('id') partner_id = int(kw.get('partner_id', 0)) - product_template = request.env['product.template'].search([('id', '=', id)], limit=1) + product_template = request.env['product.template'].search( + [('id', '=', id)], limit=1) data = { 'price_include': product_template.product_variant_id._get_website_price_include_tax(), @@ -106,14 +132,15 @@ class Product(controller.Controller): data.update(start_from) return self.response(data, headers=[('Cache-Control', 'max-age=180, private')]) - + @http.route(prefix + 'product/product/price/<id>', auth='public', methods=['GET', 'OPTIONS']) def get_product_product_price_by_id(self, **kw): if not self.authenticate(): return self.response(code=401, description='Unauthorized') id = kw.get('id') - partner_id = int(kw.get('partner_id', 0)) - product_product = request.env['product.product'].search([('id', '=', id)], limit=1) + partner_id = int(kw.get('partner_id', 0)) + product_product = request.env['product.product'].search( + [('id', '=', id)], limit=1) data = { 'price_include': product_product._get_website_price_include_tax(), @@ -132,7 +159,8 @@ class Product(controller.Controller): is_brand_only = int(kw.get('is_brand_only', 0)) base_url = request.env['ir.config_parameter'].get_param('web.base.url') - limit_new_products = request.env['ir.config_parameter'].get_param('limit.new.product') + limit_new_products = request.env['ir.config_parameter'].get_param( + 'limit.new.product') limit_new_products = int(limit_new_products) # current_time = datetime.now() # delta_time = current_time - timedelta(days=30) @@ -147,8 +175,9 @@ class Product(controller.Controller): ('x_manufacture', '!=', False), # ('create_date', '>=', delta_time), ] - new_products = request.env['product.template'].search(query_products, order='create_date desc', limit=limit_new_products) - brands = [] + new_products = request.env['product.template'].search( + query_products, order='create_date desc', limit=limit_new_products) + brands = [] for product in new_products: brands.append(product.x_manufacture) brands = list(dict.fromkeys(brands)) @@ -179,11 +208,13 @@ class Product(controller.Controller): ('x_manufacture', '!=', False), # ('create_date', '>=', delta_time), ] - count_products = request.env['product.template'].search_count(query) + count_products = request.env['product.template'].search_count( + query) if count_products < 6: _logger.info('Brand Skipped %s' % brand.x_name) continue - products = request.env['product.template'].search(query, order='create_date desc', limit=12) + products = request.env['product.template'].search( + query, order='create_date desc', limit=12) data.append({ 'manufacture_id': brand.id, 'sequence': brand.sequence if brand.sequence else count, @@ -205,18 +236,19 @@ class Product(controller.Controller): categories = kw.get('categories') promotions = kw.get('promotions') ready_stock = kw.get('ready_stock') - - require_betweens = ['name', 'manufactures', 'categories', 'ready_stock', 'promotions'] - is_fulfill = False + + require_betweens = ['name', 'manufactures', + 'categories', 'ready_stock', 'promotions'] + is_fulfill = False for required in require_betweens: if kw.get(required): is_fulfill = True - + if not is_fulfill: return self.response(code=400, description='name or manufactures or categories or ready_stock or promotions is required') - + query = [('sale_ok', '=', True)] - + if name: name = '%' + name.replace(' ', '%') + '%' query += [ @@ -224,44 +256,49 @@ class Product(controller.Controller): ('default_code', 'ilike', name), ('name', 'ilike', name), ] - + if manufactures: - query.append(('x_manufacture', 'in', [int(x) for x in manufactures.split(',')])) - + 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(',')])) - + query.append(('public_categ_ids', 'child_of', [ + int(x) for x in categories.split(',')])) + if ready_stock == '1': query.append(('virtual_qty', '>', 0)) - + if promotions: - coupon_programs = request.env['coupon.program'].search([('id', 'in', promotions.split(','))]) - promotion_query = [x for coupon_program in coupon_programs for x in ast.literal_eval(coupon_program.rule_products_domain)] + coupon_programs = request.env['coupon.program'].search( + [('id', 'in', promotions.split(','))]) + promotion_query = [x for coupon_program in coupon_programs for x in ast.literal_eval( + coupon_program.rule_products_domain)] query += promotion_query - + price_from = kw.get('price_from') if price_from and int(price_from): query.append(('web_price_sorting', '>=', int(price_from))) - + price_to = kw.get('price_to') if price_to and int(price_to): query.append(('web_price_sorting', '<=', int(price_to))) - + 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 = self.get_product_default_order(kw.get('order')) - - product_templates = request.env['product.template'].search(query, limit=limit, offset=offset, order=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) - + @http.route(prefix + 'product/solr', auth='public', methods=['GET']) @controller.Controller.must_authorized() def get_product_solr(self, **kw): @@ -269,10 +306,10 @@ class Product(controller.Controller): solr_flag = kw.get('flag') limit = int(kw.get('limit', 0)) offset = int(kw.get('offset', 0)) - + if not solr_flag: return self.response(code=400, description='flag is required') - + query = [ ('sale_ok', '=', True), ('solr_flag', '=', int(solr_flag)) @@ -284,13 +321,14 @@ class Product(controller.Controller): ('default_code', 'ilike', name), ('name', 'ilike', name), ] - product_templates = request.env['product.template'].search(query, limit=limit, offset=offset) + product_templates = request.env['product.template'].search( + query, limit=limit, offset=offset) data = { 'product_total': request.env['product.template'].search_count(query), 'products': [request.env['product.template'].api_single_response(x, with_detail='SOLR') for x in product_templates] } return self.response(data) - + @http.route(prefix + 'product/<id>', auth='public', methods=['GET']) @controller.Controller.must_authorized() def get_product_by_id(self, **kw): @@ -300,53 +338,60 @@ class Product(controller.Controller): data = [] id = [int(x) for x in id.split(',')] - product_templates = request.env['product.template'].search([('id', 'in', id)]) + product_templates = request.env['product.template'].search( + [('id', 'in', id)]) if product_templates: - data = [request.env['product.template'].api_single_response(x, with_detail='DEFAULT') for x in product_templates] - + data = [request.env['product.template'].api_single_response( + x, with_detail='DEFAULT') for x in product_templates] + return self.response(data) - + @http.route(prefix + 'product/<id>/similar', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() def get_product_similar_by_id(self, **kw): 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([]) - + product_template = request.env['product.template'].search( + [('id', '=', id)]) + + if not product_template: + return self.response([]) + query = [('id', '!=', id)] if product_template.x_manufacture: - query.append(('x_manufacture', '=', product_template.x_manufacture.id)) + 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])) + 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) - + 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': + if order == 'price-asc': orders.append('web_price_sorting asc') - elif order == 'latest': + elif order == 'latest': orders.append('create_date desc') return ','.join(orders) - + @http.route(prefix + 'product/category-homepage', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() def get_product_category_homepage(self, **kw): @@ -356,13 +401,14 @@ class Product(controller.Controller): id = kw.get('id') if id: query = f'id:{id}' - - categories = solr_model.connect('product_category_homepage').search(query, sort='sequence_i asc').docs + + categories = solr_model.connect('product_category_homepage').search( + query, sort='sequence_i asc').docs categories = solr_model.clean_key_docs(categories) for category in categories: product_ids = category.get('product_ids', []) category.pop('product_ids', None) products = request.env['product.template'].browse(product_ids) category['products'] = products.solr_results() - - return self.response(categories, headers=[('Cache-Control', 'max-age=3600, public')])
\ No newline at end of file + + return self.response(categories, headers=[('Cache-Control', 'max-age=3600, public')]) |
