diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2024-07-25 08:46:14 +0000 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2024-07-25 08:46:14 +0000 |
| commit | 2fc74a4bf4ce9f2b36c749fc251e648fb4112666 (patch) | |
| tree | 6aca07153aee02f9d80b50c745a5b3beae4f84cb /indoteknik_api/controllers | |
| parent | 34e3cc9c3307be1eacdc8744d377f03689aea0a7 (diff) | |
| parent | 855c29843be2b5ce9312b2ef9b18afe59e331644 (diff) | |
Merged in feature/iman-categories-homepage (pull request #178)
<iman> update api category
Diffstat (limited to 'indoteknik_api/controllers')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/category.py | 91 |
1 files changed, 90 insertions, 1 deletions
diff --git a/indoteknik_api/controllers/api_v1/category.py b/indoteknik_api/controllers/api_v1/category.py index 82347196..a673e42f 100644 --- a/indoteknik_api/controllers/api_v1/category.py +++ b/indoteknik_api/controllers/api_v1/category.py @@ -32,7 +32,96 @@ class Category(controller.Controller): 'product.public.category', 'image_1920', category['id'] ) return self.response(categories) - + + @http.route(prefix + 'category/numFound', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def get_category_numfound(self, **kw): + params = self.get_request_params(kw, { + 'parent_id': ['number', 'default:0'] + }) + if not params['valid']: + return self.response(code=400, description=params) + if params['value']['parent_id'] == 0: + params['value']['parent_id'] = False + + parent_id = params['value'].get('parent_id', 0) + if parent_id == 0: + parent_id = False + + def get_category_level(category_id): + """Return the level of the category based on its parent hierarchy.""" + category = request.env['product.public.category'].browse(category_id) + level = 0 + while category.parent_frontend_id: + category = category.parent_frontend_id + level += 1 + return level + + def compute_num_found(category, depth, start_depth): + """Recursively compute the total number of product templates in the category and its children starting from the given depth.""" + if depth > 2: + return 0 + child_categories = request.env['product.public.category'].search([('parent_frontend_id', '=', category.id)]) + num_found = len(category.product_tmpl_ids) + for child in child_categories: + num_found += compute_num_found(child, depth + 1, start_depth) + return num_found + + # Fetch the category and determine its level + category = request.env['product.public.category'].browse(parent_id) + category_level = get_category_level(category.id) if category else -1 + + if category: + start_depth = category_level + category_data = { + 'id': category.id, + 'name': category.name, + } + + if category_level == 0: + # Level 1 category, fetch child level 2 and their children level 3 + category_data['numFound'] = compute_num_found(category, 0, start_depth) + child_categories_level_2 = request.env['product.public.category'].search_read( + [('parent_frontend_id', '=', category.id)], + ['id', 'name'] + ) + for child in child_categories_level_2: + child_record = request.env['product.public.category'].browse(child['id']) + child['numFound'] = compute_num_found(child_record, 1, start_depth) + child['children'] = request.env['product.public.category'].search_read( + [('parent_frontend_id', '=', child['id'])], + ['id', 'name'] + ) + for grandchild in child['children']: + grandchild_record = request.env['product.public.category'].browse(grandchild['id']) + grandchild['numFound'] = compute_num_found(grandchild_record, 2, start_depth) + + category_data['children'] = child_categories_level_2 + + elif category_level == 1: + # Level 2 category, fetch child level 3 + category_data['numFound'] = compute_num_found(category, 1, start_depth) + child_categories_level_3 = request.env['product.public.category'].search_read( + [('parent_frontend_id', '=', category.id)], + ['id', 'name'] + ) + for child in child_categories_level_3: + child_record = request.env['product.public.category'].browse(child['id']) + child['numFound'] = compute_num_found(child_record, 2, start_depth) + + category_data['children'] = child_categories_level_3 + + else: + # Level 3 category or deeper, no children + category_data['numFound'] = compute_num_found(category, 2, start_depth) + category_data['children'] = [] + + response_data = category_data + else: + response_data = {} + + return self.response(response_data) + @http.route(prefix + 'category/tree', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() def get_category_tree(self): |
