diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-07-29 14:53:32 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-07-29 14:53:32 +0700 |
| commit | 7f4895929e9b5221a83673b55e1609f7ffbc9699 (patch) | |
| tree | 714e6e2ab853a36afdfaacbc4cd7b24a67b203bd /indoteknik_api/controllers/api_v1 | |
| parent | 007d5dc88b6e76321ad0d314d78767a24dc4f69c (diff) | |
<iman> update get api lob category
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/category_management_lob.py | 61 |
1 files changed, 39 insertions, 22 deletions
diff --git a/indoteknik_api/controllers/api_v1/category_management_lob.py b/indoteknik_api/controllers/api_v1/category_management_lob.py index 2931a4b5..7f1b389b 100644 --- a/indoteknik_api/controllers/api_v1/category_management_lob.py +++ b/indoteknik_api/controllers/api_v1/category_management_lob.py @@ -33,38 +33,54 @@ class BrandHomepage(controller.Controller): return self.response(data, headers=[('Cache-Control', 'max-age=3600, public')]) - @http.route(prefix + 'lob_homepage', auth='public', methods=['GET', 'OPTIONS'], csrf=False) + @http.route(prefix + 'lob_homepage/<id>/category_id', auth='public', methods=['GET', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized() - def get_lob_homepage_id(self, **kw): + def get_lob_homepage_id(self, id, **kw): base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url') - params = self.get_request_params(kw, { - 'lob_id': ['number', 'default:0'] - }) - - if not params['valid']: - return self.response(code=400, description=params) - lob_id = params['value']['lob_id'] - if lob_id == 0: - lob_id = False - - # Modify search to include browsing based on the lob_id - if lob_id: - query = [('id', '=', lob_id), ('status', '=', 'tayang')] - else: - query = [('status', '=', 'tayang')] + # Query for lob records + query = [('status', '=', 'tayang')] + if id: + query.append(('id', '=', id)) lob_records = request.env['website.categories.lob'].search(query) data = [] for category in lob_records: - category_ids = [ - { + category_ids = [] + child_ids = set() # Use a set to avoid duplicate IDs + + for cat in category.category_ids: + # Add category information to the list + category_ids.append({ 'id': cat.id, 'name': cat.name, 'image': base_url + 'api/image/product.public.category/image/' + str(cat.id) if cat.image else '', - } for cat in category.category_ids - ] + 'child_id': [] + }) + + # Find level 1 child categories with non-empty product_tmpl_ids + level_1_children = request.env['product.public.category'].search([ + ('parent_frontend_id', '=', cat.id), + ('product_tmpl_ids', '!=', False) + ]) + for child in level_1_children: + child_ids.add(child.id) + + # Find level 2 child categories with non-empty product_tmpl_ids + level_2_children = request.env['product.public.category'].search([ + ('parent_frontend_id', '=', child.id), + ('product_tmpl_ids', '!=', False) + ]) + for sub_child in level_2_children: + child_ids.add(sub_child.id) + + # Convert child_ids set to list and include the current category id + all_child_ids = list(child_ids) + for cat in category_ids: + cat['child_id'] = [cat['id']] + all_child_ids + + # Add lob record information data.append({ 'id': category.id, 'sequence': category.sequence, @@ -74,4 +90,5 @@ class BrandHomepage(controller.Controller): 'category_ids': category_ids }) - return self.response(data, headers=[('Cache-Control', 'max-age=3600, public')])
\ No newline at end of file + # Response with formatted JSON + return self.response(data, headers=[('Cache-Control', 'max-age=3600, public')]) |
