diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2022-10-12 16:52:46 +0700 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2022-10-12 16:52:46 +0700 |
| commit | c4b6e2d594b8cbd7d424673d04741a1a4cb2ff81 (patch) | |
| tree | da13382d8d442a991a23e058f2021e0a96c73c87 /indoteknik_api/controllers/api_v1 | |
| parent | dae117ce9bb219557c9a4fc995e93bc4a88ea03f (diff) | |
Update Feature:
- filter product_name, manufactures, categories in flash sale
- add categories in product response
- create get manufacture and category by page
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/__init__.py | 2 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/category.py | 37 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/flash_sale.py | 24 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/manufacture.py | 37 |
4 files changed, 97 insertions, 3 deletions
diff --git a/indoteknik_api/controllers/api_v1/__init__.py b/indoteknik_api/controllers/api_v1/__init__.py index 9bcb4f31..b569a012 100644 --- a/indoteknik_api/controllers/api_v1/__init__.py +++ b/indoteknik_api/controllers/api_v1/__init__.py @@ -1,3 +1,5 @@ +from . import category from . import flash_sale +from . import manufacture from . import product from . import sale_order
\ No newline at end of file diff --git a/indoteknik_api/controllers/api_v1/category.py b/indoteknik_api/controllers/api_v1/category.py new file mode 100644 index 00000000..4b5f558e --- /dev/null +++ b/indoteknik_api/controllers/api_v1/category.py @@ -0,0 +1,37 @@ +from .. import controller +from odoo import http +from odoo.http import request + + +class Category(controller.Controller): + prefix = '/api/v1/' + + @http.route(prefix + 'category/page/<page>', auth='public', methods=['GET']) + def get_category(self, **kw): + if not self.authenticate(): + return self.response(code=401, description='Unauthorized') + + category_ids = [] + page = kw.get('page') + if page == 'flash-sale': + active_flash_sale = request.env['product.pricelist'].get_active_flash_sale() + if active_flash_sale: + category_ids = [x.id for item in active_flash_sale.item_ids for x in item.product_id.public_categ_ids] + elif page == 'manufacture': + manufacture_id = kw.get('manufacture_id') + if not manufacture_id: + return self.response(code=400, description='manufacture_id is required') + product_variants = request.env['product.product'].search([('x_manufacture', '=', int(manufacture_id))]) + category_ids = [x.id for variant in product_variants for x in variant.public_categ_ids] + else: + return self.response(code=400, description='data not found') + + categories = request.env['product.public.category'].search([('id', 'in', category_ids)]) + data = [] + for category in categories: + data.append({ + 'id': category.id, + 'name': category.name + }) + return self.response(data) +
\ No newline at end of file diff --git a/indoteknik_api/controllers/api_v1/flash_sale.py b/indoteknik_api/controllers/api_v1/flash_sale.py index 5addd7fe..7f0166ee 100644 --- a/indoteknik_api/controllers/api_v1/flash_sale.py +++ b/indoteknik_api/controllers/api_v1/flash_sale.py @@ -1,6 +1,5 @@ from datetime import datetime import logging -from urllib import response from .. import controller from odoo import http from odoo.http import request @@ -14,12 +13,31 @@ class FlashSale(controller.Controller): @http.route(prefix + 'flash_sale', auth='public', methods=['GET']) def get_flash_sale(self, **kw): try: - self.authenticate() + if not self.authenticate(): + return self.response(code=401, description='Unauthorized') base_url = request.env['ir.config_parameter'].get_param('web.base.url') active_flash_sale = request.env['product.pricelist'].get_active_flash_sale() data = {} if active_flash_sale: - product_variant_ids = [x.product_id.id for x in active_flash_sale.item_ids] + flash_sale_product_variant_ids = [x.product_id.id for x in active_flash_sale.item_ids] + query = [('id', 'in', flash_sale_product_variant_ids)] + + product_name = kw.get('product_name') + if product_name: + product_name = '%' + product_name.replace(' ', '%') + '%' + query += ['|', ('name', 'ilike', product_name), ('default_code', 'ilike', product_name)] + + manufactures = kw.get('manufactures') + if manufactures: + query += [('x_manufacture', 'in', [int(x) for x in manufactures.split(',')])] + + categories = kw.get('categories') + if categories: + query += [('public_categ_ids', 'child_of', [int(x) for x in categories.split(',')])] + + 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)] product_templates = self.search_filter('product.template', kw, query) data = { diff --git a/indoteknik_api/controllers/api_v1/manufacture.py b/indoteknik_api/controllers/api_v1/manufacture.py new file mode 100644 index 00000000..3cb6ed0f --- /dev/null +++ b/indoteknik_api/controllers/api_v1/manufacture.py @@ -0,0 +1,37 @@ +from .. import controller +from odoo import http +from odoo.http import request + + +class Manufacture(controller.Controller): + prefix = '/api/v1/' + + @http.route(prefix + 'manufacture/page/<page>', auth='public', methods=['GET']) + def get_manufacture(self, **kw): + if not self.authenticate(): + return self.response(code=401, description='Unauthorized') + + manufacture_ids = [] + page = kw.get('page') + if page == 'flash-sale': + active_flash_sale = request.env['product.pricelist'].get_active_flash_sale() + if active_flash_sale: + manufacture_ids = [x.product_id.x_manufacture.id for x in active_flash_sale.item_ids] + elif page == 'category': + category_id = kw.get('category_id') + if not category_id: + return self.response(code=400, description='category_id is required') + product_variants = request.env['product.product'].search([('public_categ_ids', '=', int(category_id))]) + manufacture_ids = [x.x_manufacture.id for x in product_variants] + else: + return self.response(code=400, description='data not found') + + manufactures = request.env['x_manufactures'].search([('id', 'in', manufacture_ids)]) + data = [] + for manufacture in manufactures: + data.append({ + 'id': manufacture.id, + 'name': manufacture.x_name + }) + return self.response(data) +
\ No newline at end of file |
