diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2024-08-22 04:18:12 +0000 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2024-08-22 04:18:12 +0000 |
| commit | ec2085733e14941ad62808e3e23e4c781a654c0a (patch) | |
| tree | b5b78c435b3928552388c52cae74618353106e61 /indoteknik_api/controllers/api_v1 | |
| parent | 1297a3f4af3c847bcaf04ec7c66f052678a88e84 (diff) | |
| parent | 1d2769930ce4113c4620f82d1417e4221e0ab465 (diff) | |
Merged in feature/iman-categories-homepage (pull request #202)
<iman> update api category
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/category.py | 63 |
1 files changed, 38 insertions, 25 deletions
diff --git a/indoteknik_api/controllers/api_v1/category.py b/indoteknik_api/controllers/api_v1/category.py index 09b8ff8c..7d66ad01 100644 --- a/indoteknik_api/controllers/api_v1/category.py +++ b/indoteknik_api/controllers/api_v1/category.py @@ -123,34 +123,47 @@ class Category(controller.Controller): return self.response(response_data) - @http.route(prefix + 'category/tree', auth='public', methods=['GET', 'OPTIONS']) - @controller.Controller.must_authorized() - def get_category_tree(self): - parent_categories = request.env['product.public.category'].search_read([('parent_frontend_id', '=', False)], ['id', 'name']) - data = [] - for parent_category in parent_categories: - parent_data = { - 'id': parent_category['id'], - 'name': parent_category['name'], - 'childs': [] - } - child_1_categories = request.env['product.public.category'].search_read([('parent_frontend_id', '=', parent_category['id'])], ['id', 'name']) - for child_1_category in child_1_categories: - child_1_data = { - 'id': child_1_category['id'], - 'name': child_1_category['name'], + class CategoryManagement(controller.Controller): + prefix = '/api/v1/' + + @http.route(prefix + 'category/tree', auth='public', methods=['GET', 'OPTIONS'], csrf=False) + @controller.Controller.must_authorized() + def get_category_tree(self): + parent_categories = request.env['product.public.category'].search_read([('parent_frontend_id', '=', False)], + ['id', 'name', 'image_1920']) + data = [] + for parent_category in parent_categories: + category_data_ids = [parent_category['id']] + parent_data = { + 'id': parent_category['id'], + 'name': parent_category['name'], + 'image': request.env['ir.attachment'].sudo().api_image('product.public.category', 'image_1920', + parent_category['id']), + 'category_data_ids': category_data_ids, 'childs': [] } - child_2_categories = request.env['product.public.category'].search_read([('parent_frontend_id', '=', child_1_category['id'])], ['id', 'name']) - for child_2_category in child_2_categories: - child_2_data = { - 'id': child_2_category['id'], - 'name': child_2_category['name'], + child_1_categories = request.env['product.public.category'].search_read( + [('parent_frontend_id', '=', parent_category['id'])], ['id', 'name']) + for child_1_category in child_1_categories: + child_1_data = { + 'id': child_1_category['id'], + 'name': child_1_category['name'], + 'childs': [] } - child_1_data['childs'].append(child_2_data) - parent_data['childs'].append(child_1_data) - data.append(parent_data) - return self.response(data, headers=[('Cache-Control', 'max-age=3600, public')]) + category_data_ids.append(child_1_category['id']) + child_2_categories = request.env['product.public.category'].search_read( + [('parent_frontend_id', '=', child_1_category['id'])], ['id', 'name']) + for child_2_category in child_2_categories: + child_2_data = { + 'id': child_2_category['id'], + 'name': child_2_category['name'], + } + child_1_data['childs'].append(child_2_data) + category_data_ids.append(child_2_category['id']) + parent_data['childs'].append(child_1_data) + parent_data['category_data_ids'] = category_data_ids + data.append(parent_data) + return self.response(data, headers=[('Cache-Control', 'max-age=3600, public')]) @http.route(prefix + 'categories_homepage/ids', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() |
