diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2022-10-11 15:58:58 +0700 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2022-10-11 15:58:58 +0700 |
| commit | dae117ce9bb219557c9a4fc995e93bc4a88ea03f (patch) | |
| tree | 62f51e1c8290651606759fc8d31a2662e7878590 /indoteknik_api | |
| parent | fd6af0fbd83042c8471c3c58ff459f52bed45938 (diff) | |
init commit
Diffstat (limited to 'indoteknik_api')
| -rw-r--r-- | indoteknik_api/controllers/__init__.py | 2 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/__init__.py | 3 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/flash_sale.py | 39 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/product.py | 82 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/sale_order.py (renamed from indoteknik_api/controllers/sale_order_controller.py) | 10 | ||||
| -rw-r--r-- | indoteknik_api/controllers/controller.py | 33 | ||||
| -rw-r--r-- | indoteknik_api/controllers/product_controller.py | 12 | ||||
| -rw-r--r-- | indoteknik_api/models/product_pricelist.py | 21 | ||||
| -rw-r--r-- | indoteknik_api/models/product_template.py | 48 |
9 files changed, 216 insertions, 34 deletions
diff --git a/indoteknik_api/controllers/__init__.py b/indoteknik_api/controllers/__init__.py index 4c317fdc..f56b2612 100644 --- a/indoteknik_api/controllers/__init__.py +++ b/indoteknik_api/controllers/__init__.py @@ -1,3 +1,3 @@ from . import controller from . import product_controller -from . import sale_order_controller +from . import api_v1
\ No newline at end of file diff --git a/indoteknik_api/controllers/api_v1/__init__.py b/indoteknik_api/controllers/api_v1/__init__.py new file mode 100644 index 00000000..9bcb4f31 --- /dev/null +++ b/indoteknik_api/controllers/api_v1/__init__.py @@ -0,0 +1,3 @@ +from . import flash_sale +from . import product +from . import sale_order
\ 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 new file mode 100644 index 00000000..5addd7fe --- /dev/null +++ b/indoteknik_api/controllers/api_v1/flash_sale.py @@ -0,0 +1,39 @@ +from datetime import datetime +import logging +from urllib import response +from .. import controller +from odoo import http +from odoo.http import request + +_logger = logging.getLogger(__name__) + + +class FlashSale(controller.Controller): + prefix = '/api/v1/' + + @http.route(prefix + 'flash_sale', auth='public', methods=['GET']) + def get_flash_sale(self, **kw): + try: + self.authenticate() + 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] + query = [('product_variant_ids', 'in', product_variant_ids)] + product_templates = self.search_filter('product.template', kw, query) + data = { + 'flash_sale': { + 'banner': base_url + 'api/image/product.pricelist/banner/' + str(active_flash_sale.id) if active_flash_sale.banner else '', + 'duration': round((active_flash_sale.end_date - datetime.now()).total_seconds()), + 'product_total': request.env['product.template'].search_count(query), + 'products': [request.env['product.template'].api_single_response(x) for x in product_templates] + } + } + return self.response(data) + else: + return self.response(code=404, description='Data not found') + except Exception as e: + _logger.info(self.prefix_url + '/flash_sale: ' + str(e)) + return self.response(code=500, description='Internal server error') +
\ No newline at end of file diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py new file mode 100644 index 00000000..068a54b6 --- /dev/null +++ b/indoteknik_api/controllers/api_v1/product.py @@ -0,0 +1,82 @@ +from .. import controller +from odoo import http +from odoo.http import request + + +class Product(controller.Controller): + prefix = '/api/v1/' + + @http.route(prefix + 'product', auth='public', methods=['GET']) + def get_product(self, **kw): + if not self.authenticate(): + return self.response(code=401, description='Unauthorized') + + name = kw.get('name') + manufactures = kw.get('manufactures') + categories = kw.get('categories') + + require_betweens = ['name', 'manufactures', 'categories'] + is_fulfill = False + for required in require_betweens: + if kw.get(required): + is_fulfill = True + + # If not fulfill in require_between + if not is_fulfill: + return self.response(code=400, description='name or manufactures or categories is required') + + query = [('sale_ok', '=', True)] + + if name: + name = '%' + name.replace(' ', '%') + '%' + query += [ + '|', + ('default_code', 'ilike', name), + ('name', 'ilike', name), + ] + + if manufactures: + query.append(('x_manufacture', 'in', [int(x) for x in manufactures.split(',')])) + + if categories: + query.append(('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)] + limit = int(kw.get('limit', 0)) + offset = int(kw.get('offset', 0)) + order = kw.get('order') + if order == 'price-desc': + order = 'web_price_sorting desc' + elif order == 'price-asc': + order = 'web_price_sorting asc' + elif order == 'latest': + order = 'create_date desc' + else: + order = 'product_rating desc' + product_templates = request.env['product.template'].search(query, limit=limit, offset=offset, order=order) + data = { + 'product_total': request.env['product.template'].search_count(query), + 'products': [request.env['product.template'].api_single_response(x) for x in product_templates] + } + return self.response(data) + + @http.route(prefix + 'product/<id>', auth='public', methods=['GET']) + def get_product_by_id(self, **kw): + if not self.authenticate(): + return self.response(code=401, description='Unauthorized') + + id = kw.get('id') + if not id: + return self.response(code=400, description='id is required') + + data = [] + id = [int(x) for x in id.split(',')] + product_templates = request.env['product.template'].search([('id', 'in', id)]) + if product_templates: + data = [request.env['product.template'].api_single_response(x, with_detail=True) for x in product_templates] + + return self.response(data) +
\ No newline at end of file diff --git a/indoteknik_api/controllers/sale_order_controller.py b/indoteknik_api/controllers/api_v1/sale_order.py index 741d4bf8..99302a66 100644 --- a/indoteknik_api/controllers/sale_order_controller.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -1,12 +1,12 @@ -from . import controller +from .. import controller from odoo import http from odoo.http import request -class SaleOrderController(controller.Controller): +class SaleOrder(controller.Controller): @http.route('/api/sale_order/invoiced', auth='public', methods=['GET']) def get_sale_order_invoiced_by_partner_id(self, **kw): - self.authenticate(kw) + self.authenticate() partner_id = kw.get('partner_id') if not partner_id: return self.response(code=400, description='Field partner_id is required') @@ -25,7 +25,7 @@ class SaleOrderController(controller.Controller): ('invoice_status', '=', 'invoiced'), ('invoice_status', '=', 'to_invoice') ] - sale_orders = self.search_with_api_params('sale.order', kw, default_domain) + sale_orders = self.search_filter('sale.order', kw, default_domain) for sale_order in sale_orders: pickings = [] for picking in sale_order.picking_ids: @@ -71,7 +71,7 @@ class SaleOrderController(controller.Controller): partner_child_ids = [v['id'] for v in partner_childs] + [int(partner_id)] default_domain.append(('partner_id', 'in', partner_child_ids)) - sale_order = self.search_with_api_params('sale.order', kw, default_domain) + sale_order = self.search_filter('sale.order', kw, default_domain) orders = [] for order in sale_order.order_line: orders.append({ diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py index fb5f1fce..c02f6b60 100644 --- a/indoteknik_api/controllers/controller.py +++ b/indoteknik_api/controllers/controller.py @@ -1,3 +1,4 @@ +from array import array import datetime import base64 import json @@ -8,11 +9,29 @@ from pytz import timezone class Controller(http.Controller): - def authenticate(self, kw): - db = kw.get('db') - username = kw.get('username') - password = kw.get('password') - request.session.authenticate(db, username, password) + def authenticate(self): + try: + wsgienv = request.httprequest.environ + db = wsgienv['HTTP_DB'] + username = wsgienv['HTTP_USERNAME'] + password = wsgienv['HTTP_PASSWORD'] + request.session.authenticate(db, username, password) + return True + except: + return False + + def validate_request(self, rules: dict, kw: dict): + validation = { + 'status': True, + 'reason': [] + } + for key in rules: + values = rules[key] + for value in values: + if value == 'required' and not kw.get(key): + validation['status'] = False + validation['reason'].append(key + ' is ' + value) + return validation def time_to_str(self, object, format): time = '' @@ -33,13 +52,13 @@ class Controller(http.Controller): response = json.dumps(response) return request.make_response(response, [('Content-Type', 'application/json')]) - def search_with_api_params(self, model: str, kw, domain=[]): + def search_filter(self, model: str, kw: dict, query: array = []): """ To search data by default API Params if exist """ limit = kw.get('limit', 0) offset = kw.get('offset', 0) order = kw.get('order', '') - return request.env[model].search(domain, limit=int(limit), offset=int(offset), order=order) + return request.env[model].search(query, limit=int(limit), offset=int(offset), order=order) @http.route('/api/image/<model>/<field>/<id>', auth='public', methods=['GET']) def get_image(self, model, field, id): diff --git a/indoteknik_api/controllers/product_controller.py b/indoteknik_api/controllers/product_controller.py index a57cf0a5..abb48f7d 100644 --- a/indoteknik_api/controllers/product_controller.py +++ b/indoteknik_api/controllers/product_controller.py @@ -13,7 +13,7 @@ class ProductController(controller.Controller): # TODO: separate function for get manufacture and promotion by product_id @http.route(prefix_url + '/search', auth='public', methods=['GET']) def search_product(self, **kw): - self.authenticate(kw) + self.authenticate() base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url') query = kw.get('query') @@ -42,7 +42,7 @@ class ProductController(controller.Controller): product_variant_ids = [x.id for x in product_variants] domain = [('product_variant_ids', 'in', product_variant_ids)] - product_templates = self.search_with_api_params('product.template', kw, domain) + product_templates = self.search_filter('product.template', kw, domain) data = { 'total_records': len(request.env['product.template'].search(domain)), 'products': [] @@ -51,9 +51,9 @@ class ProductController(controller.Controller): discount_price = 0 price = product_template.web_price if price > 0: - if product_template.taxes_id: - if not product_template.taxes_id.price_include: - price += (price * product_template.taxes_id.amount / 100) + if product_template.web_tax_id: + if not product_template.web_tax_id.price_include: + price += (price * product_template.web_tax_id.amount / 100) else: price += (price * 11 / 100) @@ -95,7 +95,7 @@ class ProductController(controller.Controller): } product_pricelist_item = request.env['product.pricelist.item'].search([('pricelist_id', '=', active_flash_sale.id)]) product_variant_ids = [x.product_id.id for x in product_pricelist_item] - product_templates = self.search_with_api_params('product.template', kw, [('product_variant_ids', 'in', product_variant_ids)]) + product_templates = self.search_filter('product.template', kw, [('product_variant_ids', 'in', product_variant_ids)]) for product in product_templates: flash_sale['products'].append({ 'id': product.id, diff --git a/indoteknik_api/models/product_pricelist.py b/indoteknik_api/models/product_pricelist.py index f40efd5d..a82fea1d 100644 --- a/indoteknik_api/models/product_pricelist.py +++ b/indoteknik_api/models/product_pricelist.py @@ -41,10 +41,10 @@ class ProductPricelist(models.Model): if price > 0: product = self.env['product.product'].browse(product_id) if product: - for tax in product.taxes_id: - if not tax.price_include: - price *= (100 + tax.amount) / 100 - + if product.web_tax_id and product.web_tax_id.price_include: + price *= (100 + product.web_tax_id.amount) / 100 + + price_discount = price for discount in discounts: price_discount *= (100 - discount) / 100 @@ -57,13 +57,12 @@ class ProductPricelist(models.Model): 'discount_percentage': discount_percentage } - def get_lowest_product_variant_price(self, product_id, pricelist_id): + def get_lowest_product_variant_price(self, product_template: dict, pricelist_id: int): """ - @param product_id: id of product.template + @param product_template: dict of product_template @param pricelist_id: id of pricelist which have default price @return price: object """ - product_template = self.env['product.template'].browse(product_id) product_variant_ids = [x.id for x in product_template.product_variant_ids] product = self.env['product.pricelist.item'].search([ ('pricelist_id', '=', pricelist_id), @@ -99,11 +98,3 @@ class ProductPricelist(models.Model): if product_id in pricelist_product_ids: return pricelist.id return False - - # TODO: need testing - def get_active_flash_sale_items(self): - pricelist = self.get_active_flash_sale() - pricelist_item = False - if pricelist: - pricelist_item = [x.product_id for x in pricelist.item_ids] - return pricelist_item diff --git a/indoteknik_api/models/product_template.py b/indoteknik_api/models/product_template.py index e69de29b..6b8973a2 100644 --- a/indoteknik_api/models/product_template.py +++ b/indoteknik_api/models/product_template.py @@ -0,0 +1,48 @@ +from odoo import models, fields + + +class ProductTemplate(models.Model): + _inherit = 'product.template' + + def api_single_response(self, product_template, with_detail=False): + base_url = self.env['ir.config_parameter'].get_param('web.base.url') + product_pricelist_default = self.env['ir.config_parameter'].get_param('product.pricelist.default') + product_pricelist_default = int(product_pricelist_default) + 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, + '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) + } + 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 + } + for variant in product_template.product_variant_ids: + detail_data['variants'].append({ + 'id': variant.id, + 'code': variant.default_code, + 'name': variant.name, + 'price': self.env['product.pricelist'].compute_price(product_pricelist_default, variant.id), + 'stock': variant.qty_stock_vendor + }) + data.update(detail_data) + return data + + def api_manufacture(self, product_template): + base_url = self.env['ir.config_parameter'].get_param('web.base.url') + if product_template.x_manufacture: + manufacture = product_template.x_manufacture + return { + 'id': manufacture.id, + 'name': manufacture.x_name, + '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 |
