From f785e7605f4d0151a0f48e3d871b996c40e51351 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 31 Jul 2023 16:31:42 +0700 Subject: Add flash sale tag on model, view, api response --- indoteknik_api/models/product_template.py | 7 +++-- indoteknik_custom/models/product_pricelist.py | 7 +++-- indoteknik_custom/models/product_template.py | 8 +++--- indoteknik_custom/views/product_pricelist.xml | 37 +++++++++++---------------- 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/indoteknik_api/models/product_template.py b/indoteknik_api/models/product_template.py index b01e957b..fb77769f 100644 --- a/indoteknik_api/models/product_template.py +++ b/indoteknik_api/models/product_template.py @@ -70,9 +70,12 @@ class ProductTemplate(models.Model): for variant in variants: if variant["price"]["price_discount"] < lowest_price["price_discount"]: lowest_price = variant['price'] - + template_pricelist = product_template._get_active_flash_sale() data_with_detail = { - 'flash_sale_remaining_time': product_template._get_flash_sale_remaining_time(), + 'flash_sale': { + 'remaining_time': template_pricelist._remaining_time_in_second() if template_pricelist else 0, + 'tag': template_pricelist.flashsale_tag if template_pricelist else None + }, '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 49927d6b..95e63cf0 100644 --- a/indoteknik_custom/models/product_pricelist.py +++ b/indoteknik_custom/models/product_pricelist.py @@ -14,13 +14,16 @@ class ProductPricelist(models.Model): flashsale_option = fields.Selection([ ('all', 'For All User'), ('registered_user', 'Only for Registered User') - ], string='Flashsale Option') + ], string='Flash Sale Option') banner_top = fields.Binary(string='Banner Top') + flashsale_tag = fields.Char(string='Flash Sale Tag') def _remaining_time_in_second(self): if not self.end_date: return 0 - return round((self.end_date - datetime.now()).total_seconds()) + remaining_time = (self.end_date - datetime.now()).total_seconds() + remaining_time = round(remaining_time) + return max(remaining_time, 0) class ProductPricelistItem(models.Model): diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index 24264366..0b9b1945 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -154,15 +154,13 @@ class ProductTemplate(models.Model): else: template.have_promotion_program = False - def _get_flash_sale_remaining_time(self): + def _get_active_flash_sale(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() + ], limit=1) + return pricelist @api.model def _calculate_rating_product(self): diff --git a/indoteknik_custom/views/product_pricelist.xml b/indoteknik_custom/views/product_pricelist.xml index 0ad9e200..55139a24 100644 --- a/indoteknik_custom/views/product_pricelist.xml +++ b/indoteknik_custom/views/product_pricelist.xml @@ -7,29 +7,22 @@ - - - - - - - - - - + + + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3