From 1563299905b3e0cf97129739c0ee0a6269ce4bc8 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 28 Jul 2023 16:53:32 +0700 Subject: Add flash sale remaining time API response --- indoteknik_api/controllers/api_v1/flash_sale.py | 5 ++--- indoteknik_api/models/product_template.py | 1 + indoteknik_custom/models/product_pricelist.py | 6 ++++++ indoteknik_custom/models/product_template.py | 13 +++++++++++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/indoteknik_api/controllers/api_v1/flash_sale.py b/indoteknik_api/controllers/api_v1/flash_sale.py index dc7c3928..dff8bec3 100644 --- a/indoteknik_api/controllers/api_v1/flash_sale.py +++ b/indoteknik_api/controllers/api_v1/flash_sale.py @@ -1,4 +1,3 @@ -from datetime import datetime import logging from .. import controller from odoo import http @@ -28,7 +27,7 @@ class FlashSale(controller.Controller): 'banner': request.env['ir.attachment'].api_image('product.pricelist', 'banner', pricelist.id), 'banner_mobile': request.env['ir.attachment'].api_image('product.pricelist', 'banner_mobile', pricelist.id), 'banner_top': request.env['ir.attachment'].api_image('product.pricelist', 'banner_top', pricelist.id), - 'duration': round((pricelist.end_date - datetime.now()).total_seconds()), + 'duration': pricelist._remaining_time_in_second(), 'product_total': request.env['product.pricelist.item'].search_count(query), }) return self.response(data) @@ -94,7 +93,7 @@ class FlashSale(controller.Controller): 'name': active_flash_sale.name, 'banner': base_url + 'api/image/product.pricelist/banner/' + str(active_flash_sale.id) if active_flash_sale.banner else '', 'banner_mobile': base_url + 'api/image/product.pricelist/banner_mobile/' + str(active_flash_sale.id) if active_flash_sale.banner_mobile else '', - 'duration': round((active_flash_sale.end_date - datetime.now()).total_seconds()), + 'duration': active_flash_sale._remaining_time_in_second(), 'flashsale_option': active_flash_sale.flashsale_option, 'product_total': request.env['product.template'].search_count(query), 'products': [request.env['product.template'].api_single_response(x) for x in product_templates] diff --git a/indoteknik_api/models/product_template.py b/indoteknik_api/models/product_template.py index 68ab79c2..b01e957b 100644 --- a/indoteknik_api/models/product_template.py +++ b/indoteknik_api/models/product_template.py @@ -72,6 +72,7 @@ class ProductTemplate(models.Model): lowest_price = variant['price'] data_with_detail = { + 'flash_sale_remaining_time': product_template._get_flash_sale_remaining_time(), 'lowest_price': lowest_price, 'image': self.env['ir.attachment'].api_image('product.template', 'image_512', product_template.id), 'display_name': product_template.display_name, diff --git a/indoteknik_custom/models/product_pricelist.py b/indoteknik_custom/models/product_pricelist.py index 2edaeb80..49927d6b 100644 --- a/indoteknik_custom/models/product_pricelist.py +++ b/indoteknik_custom/models/product_pricelist.py @@ -1,5 +1,6 @@ from odoo import models, fields, api from odoo.exceptions import UserError +from datetime import datetime class ProductPricelist(models.Model): @@ -16,6 +17,11 @@ class ProductPricelist(models.Model): ], string='Flashsale Option') banner_top = fields.Binary(string='Banner Top') + def _remaining_time_in_second(self): + if not self.end_date: + return 0 + return round((self.end_date - datetime.now()).total_seconds()) + class ProductPricelistItem(models.Model): _inherit = 'product.pricelist.item' diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index 1a83b702..24264366 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -29,8 +29,7 @@ class ProductTemplate(models.Model): digits='Product Price', inverse='_set_product_lst_price', help="Web Price with pricelist_id = 1") qty_stock_vendor = fields.Float('QTY Stock Vendor', compute='_compute_qty_stock_vendor') - have_promotion_program = fields.Boolean('Have Promotion Program', compute='_have_promotion_program', - help="Punya promotion program gak?") + have_promotion_program = fields.Boolean('Have Promotion Program', compute='_have_promotion_program', help="Punya promotion program gak?") product_rating = fields.Float('Product Rating', help="Digunakan untuk sorting product di website", default=0.0) virtual_rating = fields.Float('Virtual Rating', compute='_compute_virtual_rating', help="Column Virtual untuk product rating, digunakan oleh Solr", default=0.0) last_calculate_rating = fields.Datetime("Last Calculate Rating") @@ -155,6 +154,16 @@ class ProductTemplate(models.Model): else: template.have_promotion_program = False + def _get_flash_sale_remaining_time(self): + variant_ids = [x.id for x in self.product_variant_ids] + pricelist = self.env['product.pricelist'].search([ + ('is_flash_sale', '=', True), + ('item_ids.product_id', 'in', variant_ids) + ]) + if not pricelist: + return 0 + return pricelist._remaining_time_in_second() + @api.model def _calculate_rating_product(self): #["&","&",["type","=","product"],["active","=",True],"|",["last_calculate_rating","=",False],["last_calculate_rating","<","2023-01-01 00:00:00"]] -- cgit v1.2.3