summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2022-10-12 16:52:46 +0700
committerIT Fixcomart <it@fixcomart.co.id>2022-10-12 16:52:46 +0700
commitc4b6e2d594b8cbd7d424673d04741a1a4cb2ff81 (patch)
treeda13382d8d442a991a23e058f2021e0a96c73c87
parentdae117ce9bb219557c9a4fc995e93bc4a88ea03f (diff)
Update Feature:
- filter product_name, manufactures, categories in flash sale - add categories in product response - create get manufacture and category by page
-rw-r--r--indoteknik_api/controllers/api_v1/__init__.py2
-rw-r--r--indoteknik_api/controllers/api_v1/category.py37
-rw-r--r--indoteknik_api/controllers/api_v1/flash_sale.py24
-rw-r--r--indoteknik_api/controllers/api_v1/manufacture.py37
-rw-r--r--indoteknik_api/models/product_template.py23
-rwxr-xr-xindoteknik_custom/models/product_template.py2
6 files changed, 116 insertions, 9 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
diff --git a/indoteknik_api/models/product_template.py b/indoteknik_api/models/product_template.py
index 6b8973a2..0cbf605f 100644
--- a/indoteknik_api/models/product_template.py
+++ b/indoteknik_api/models/product_template.py
@@ -11,23 +11,24 @@ class ProductTemplate(models.Model):
data = {
'id': product_template.id,
'image': base_url + 'api/image/product.template/image_128/' + str(product_template.id) if product_template.image_128 else '',
- 'code': product_template.default_code,
+ 'code': product_template.default_code or '',
'name': product_template.name,
'lowest_price': self.env['product.pricelist'].get_lowest_product_variant_price(product_template, product_pricelist_default),
'variant_total': len(product_template.product_variant_ids),
'stock_total': product_template.qty_stock_vendor,
- 'manufacture': self.api_manufacture(product_template)
+ 'manufacture': self.api_manufacture(product_template),
+ 'categories': self.api_categories(product_template),
}
if with_detail:
detail_data = {
'image': base_url + 'api/image/product.template/image_512/' + str(product_template.id) if product_template.image_512 else '',
'variants': [],
- 'description': product_template.website_description
+ 'description': product_template.website_description or '',
}
for variant in product_template.product_variant_ids:
detail_data['variants'].append({
'id': variant.id,
- 'code': variant.default_code,
+ 'code': variant.default_code or '',
'name': variant.name,
'price': self.env['product.pricelist'].compute_price(product_pricelist_default, variant.id),
'stock': variant.qty_stock_vendor
@@ -45,4 +46,16 @@ class ProductTemplate(models.Model):
'image_promotion_1': base_url + 'api/image/x_manufactures/image_promotion_1/' + str(manufacture.id) if manufacture.image_promotion_1 else '',
'image_promotion_2': base_url + 'api/image/x_manufactures/image_promotion_2/' + str(manufacture.id) if manufacture.image_promotion_2 else '',
}
- return {} \ No newline at end of file
+ return {}
+
+ def api_categories(self, product_template):
+ if product_template.public_categ_ids:
+ categories = []
+ for category in product_template.public_categ_ids:
+ categories.append({
+ 'id': category.id,
+ 'name': category.name
+ })
+ return categories
+ return []
+ \ No newline at end of file
diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py
index a51f0600..5c75718a 100755
--- a/indoteknik_custom/models/product_template.py
+++ b/indoteknik_custom/models/product_template.py
@@ -32,7 +32,7 @@ class ProductTemplate(models.Model):
for product_template in self:
product_template.qty_stock_vendor = 0
for product_variant in product_template.product_variant_ids:
- product_template.qty_stock_vendor += product_variant.qty_stock_vendor
+ product_template.qty_stock_vendor += int(product_variant.qty_stock_vendor)
def _compute_web_price(self):
for template in self: