diff options
Diffstat (limited to 'indoteknik_api/controllers/api_v1/product.py')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/product.py | 68 |
1 files changed, 60 insertions, 8 deletions
diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 28a63ed5..23dcf48e 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -1,33 +1,85 @@ from .. import controller from odoo import http from odoo.http import request +from datetime import datetime, timedelta import ast +import logging + +_logger = logging.getLogger(__name__) class Product(controller.Controller): prefix = '/api/v1/' @http.route(prefix + 'new_product', auth='public', methods=['GET', 'OPTIONS']) - def get_new_product(self): + def get_new_product(self, **kw): if not self.authenticate(): return self.response(code=401, description='Unauthorized') + + is_brand_only = int(kw.get('is_brand_only', 0)) + base_url = request.env['ir.config_parameter'].get_param('web.base.url') - query = [('show_as_new_product', '=', True)] - brands = request.env['x_manufactures'].search(query, order='sequence') + limit_new_products = request.env['ir.config_parameter'].get_param('limit.new.product') + limit_new_products = int(limit_new_products) + # current_time = datetime.now() + # delta_time = current_time - timedelta(days=30) + + # delta_time = delta_time.strftime('%Y-%m-%d %H:%M:%S') + query_products = [ + ('type', '=', 'product'), + ('active', '=', True), + ('image_128', '!=', False), + ('website_description', '!=', False), + # ('write_uid', '!=', 1), + ('x_manufacture', '!=', False), + # ('create_date', '>=', delta_time), + ] + new_products = request.env['product.template'].search(query_products, order='create_date desc', limit=limit_new_products) + brands = [] + for product in new_products: + brands.append(product.x_manufacture) + brands = list(dict.fromkeys(brands)) + data = [] + count = 0 for brand in brands: - query_products = [ - ('is_new_product', '=', True), + if is_brand_only == 1: + data.append({ + 'manufacture_id': brand.id, + 'sequence': brand.sequence if brand.sequence else count, + 'name': brand.x_name, + 'image': base_url + 'api/image/x_manufactures/x_logo_manufacture/' + str( + brand.id) if brand.x_logo_manufacture else '', + }) + continue + + if count == 11: + break + query = [ + ('type', '=', 'product'), + ('active', '=', True), ('x_manufacture', '=', brand.id), + ('image_128', '!=', False), + ('website_description', '!=', False), + # ('write_uid', '!=', 1), + ('x_manufacture', '!=', False), + # ('create_date', '>=', delta_time), ] - products = request.env['product.template'].search(query_products, order='name') + count_products = request.env['product.template'].search_count(query) + if count_products < 6: + _logger.info('Brand Skipped %s' % brand.x_name) + continue + products = request.env['product.template'].search(query, order='create_date desc', limit=12) data.append({ 'manufacture_id': brand.id, - 'sequence': brand.sequence, + 'sequence': brand.sequence if brand.sequence else count, 'name': brand.x_name, - 'image': base_url + 'api/image/x_manufactures/x_logo_manufacture/' + str(brand.id) if brand.x_logo_manufacture else '', + 'image': base_url + 'api/image/x_manufactures/x_logo_manufacture/' + str( + brand.id) if brand.x_logo_manufacture else '', + 'products_total': count_products, 'products': [request.env['product.template'].api_single_response(x) for x in products] }) + count += 1 return self.response(data) @http.route(prefix + 'product', auth='public', methods=['GET', 'OPTIONS']) |
