diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-08-29 10:41:22 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-08-29 10:41:22 +0700 |
| commit | 6ed2316f6aa446bcd5bc7e6cd4d0c0a1136096dd (patch) | |
| tree | 62e6556b8c7fa6fd6f6085b3b3e6ef00b78ee713 | |
| parent | 315b832420eb8314e809b1c0f549304d423b45f3 (diff) | |
Update apache solr
- Create get tier name pricelist
- Create solr results on product.template and product.product
- Fix get active flash sale on product template
- Update name "get_single_doc" to "get_doc" in apache solr model
- Add product ids on sync category homepage to solr
| -rw-r--r-- | indoteknik_api/controllers/api_v1/product.py | 5 | ||||
| -rw-r--r-- | indoteknik_api/models/product_product.py | 11 | ||||
| -rw-r--r-- | indoteknik_custom/models/product_pricelist.py | 12 | ||||
| -rwxr-xr-x | indoteknik_custom/models/product_template.py | 5 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/apache_solr.py | 3 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/product_product.py | 59 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/product_template.py | 103 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/website_categories_homepage.py | 6 |
8 files changed, 165 insertions, 39 deletions
diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 98e131da..e3b7701a 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -360,6 +360,9 @@ class Product(controller.Controller): categories = solr_model.connect('product_category_homepage').search(query, sort='sequence_i asc').docs categories = solr_model.clean_key_docs(categories) for category in categories: - category['products'] = json.loads(category['products']) + product_ids = category.get('product_ids', []) + category.pop('product_ids', None) + products = request.env['product.template'].browse(product_ids) + category['products'] = products.solr_results() return self.response(categories, headers=[('Cache-Control', 'max-age=3600, public')])
\ No newline at end of file diff --git a/indoteknik_api/models/product_product.py b/indoteknik_api/models/product_product.py index a2d2f8a2..bc1ce9a6 100644 --- a/indoteknik_api/models/product_product.py +++ b/indoteknik_api/models/product_product.py @@ -58,25 +58,16 @@ class ProductProduct(models.Model): def calculate_website_price(self, pricelist=False): pricelist = pricelist or self.env.user_pricelist - config = self.env['ir.config_parameter'] - product_pricelist_tier1 = int(config.get_param('product.pricelist.tier1')) - product_pricelist_tier2 = int(config.get_param('product.pricelist.tier2')) - product_pricelist_tier3 = int(config.get_param('product.pricelist.tier3')) - discount_percentage = self._get_website_disc(0) price_discount = self._get_website_price_after_disc_and_tax() - price_tier = False pricelists = { 'tier1': self._get_pricelist_tier1, 'tier2': self._get_pricelist_tier2, 'tier3': self._get_pricelist_tier3, } - pricelist_id = pricelist.id if pricelist else False - if pricelist_id == product_pricelist_tier1: price_tier = 'tier1' - if pricelist_id == product_pricelist_tier2: price_tier = 'tier2' - if pricelist_id == product_pricelist_tier3: price_tier = 'tier3' + price_tier = pricelist.get_tier_name() if price_tier: price = pricelists[price_tier]() discount_key = 'discount_%s' % price_tier diff --git a/indoteknik_custom/models/product_pricelist.py b/indoteknik_custom/models/product_pricelist.py index a55e3fdd..988b38b5 100644 --- a/indoteknik_custom/models/product_pricelist.py +++ b/indoteknik_custom/models/product_pricelist.py @@ -24,6 +24,18 @@ class ProductPricelist(models.Model): remaining_time = (self.end_date - datetime.now()).total_seconds() remaining_time = round(remaining_time) return max(remaining_time, 0) + + def get_tier_name(self): + config = self.env['ir.config_parameter'] + product_pricelist_tier1 = int(config.get_param('product.pricelist.tier1', 0)) + product_pricelist_tier2 = int(config.get_param('product.pricelist.tier2', 0)) + product_pricelist_tier3 = int(config.get_param('product.pricelist.tier3', 0)) + + price_tier = None + if self.id == product_pricelist_tier1: price_tier = 'tier1' + if self.id == product_pricelist_tier2: price_tier = 'tier2' + if self.id == product_pricelist_tier3: price_tier = 'tier3' + return price_tier 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 f72fa763..c7be3378 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -144,10 +144,13 @@ class ProductTemplate(models.Model): template.have_promotion_program = False def _get_active_flash_sale(self): + current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') 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) + ('item_ids.product_id', 'in', variant_ids), + ('start_date', '<=', current_time), + ('end_date', '>=', current_time) ], limit=1) return pricelist diff --git a/indoteknik_custom/models/solr/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py index cb44e7ed..3be37a5a 100644 --- a/indoteknik_custom/models/solr/apache_solr.py +++ b/indoteknik_custom/models/solr/apache_solr.py @@ -27,7 +27,7 @@ class ApacheSolr(models.Model): return pysolr.Solr(url + schema, always_commit=True, timeout=30) - def get_single_doc(self, schema, id): + def get_doc(self, schema, id): try: return self.connect(schema).search(f'id:{id}').docs[0] except: @@ -53,7 +53,6 @@ class ApacheSolr(models.Model): new_dict[cleaned_key] = value return new_dict - def _update_stock_product_to_solr(self, limit=10000): current_time = datetime.now() delta_time = current_time - timedelta(days=3) diff --git a/indoteknik_custom/models/solr/product_product.py b/indoteknik_custom/models/solr/product_product.py index f3107afa..48ee9daa 100644 --- a/indoteknik_custom/models/solr/product_product.py +++ b/indoteknik_custom/models/solr/product_product.py @@ -35,7 +35,7 @@ class ProductProduct(models.Model): category_name = category.name break - document = solr_model.get_single_doc('variants', variant.id) + document = solr_model.get_doc('variants', variant.id) document.update({ 'id': variant.id, 'display_name_s': variant.display_name, @@ -83,7 +83,7 @@ class ProductProduct(models.Model): tier2 = variant._get_pricelist_tier2() tier3 = variant._get_pricelist_tier3() - document = solr_model.get_single_doc('variants', variant.id) + document = solr_model.get_doc('variants', variant.id) document.update({ 'id': variant.id, 'flashsale_id_i': flashsale_data.get('flashsale_id', 0), @@ -112,4 +112,57 @@ class ProductProduct(models.Model): def _sync_delete_solr(self): for rec in self: - self.solr().delete(rec.id)
\ No newline at end of file + self.solr().delete(rec.id) + + def solr_results(self): + solr_model = self.env['apache.solr'] + pricelist = self.env.user_pricelist + price_tier = pricelist.get_tier_name() + + results = [] + for product in self: + doc = solr_model.get_doc('variants', product.id) + if len(doc) == 0: continue + + discount_key = 'discount_f' + price_discount_key = 'price_discount_f' + if price_tier: + discount_key = f'discount_{price_tier}_f' + price_discount_key = f'price_{price_tier}_f' + + flashsale = product._get_active_flash_sale() + if flashsale: + discount_key = 'flashsale_discount_f' + price_discount_key = 'flashsale_price_f' + + result = { + 'id': doc.get('id'), + 'parent': { + 'id': doc.get('template_id_i'), + 'name': doc.get('name_s'), + 'image': doc.get('image_s'), + }, + 'code': doc.get('default_code_s'), + 'name': doc.get('display_name_s'), + 'price': { + 'price': doc.get('price_f'), + 'discount_percentage': doc.get(discount_key), + 'price_discount': doc.get(price_discount_key) + }, + 'stock': doc.get('stock_total_f'), + 'weight': doc.get('weight_f'), + 'manufacture': None + } + + manufacture_id = doc.get('manufacture_id_i') + if manufacture_id: + result['manufacture'] = { + 'id': manufacture_id, + 'name': doc.get('manufacture_name_s'), + 'image_promotion_1': doc.get('image_promotion_1_s'), + 'image_promotion_2': doc.get('image_promotion_2_s'), + } + + results.append(result) + + return results
\ No newline at end of file diff --git a/indoteknik_custom/models/solr/product_template.py b/indoteknik_custom/models/solr/product_template.py index ba670a81..6ae0bec2 100644 --- a/indoteknik_custom/models/solr/product_template.py +++ b/indoteknik_custom/models/solr/product_template.py @@ -14,7 +14,7 @@ class ProductTemplate(models.Model): def solr(self): return self.env['apache.solr'].connect('product') - + def _create_solr_queue(self, function_name): for rec in self: self.env['apache.solr.queue'].create_unique({ @@ -22,11 +22,11 @@ class ProductTemplate(models.Model): 'res_id': rec.id, 'function_name': function_name }) - + @api.constrains('active') def _create_solr_queue_sync_active(self): self._create_solr_queue('_sync_active_template_solr') - + @api.constrains('name', 'default_code', 'weight', 'x_manufacture', 'public_categ_ids', 'search_rank', 'search_rank_weekly', 'image_1920') def _create_solr_queue_sync_product_template(self): self._create_solr_queue('_sync_product_template_to_solr') @@ -43,13 +43,14 @@ class ProductTemplate(models.Model): else: template._sync_product_template_to_solr() template._sync_price_to_solr() - - products = self.env['product.product'].search([('product_tmpl_id', '=', template.id), ('active', 'in', [True, False])]) + + products = self.env['product.product'].search( + [('product_tmpl_id', '=', template.id), ('active', 'in', [True, False])]) products._sync_variants_to_solr() def _sync_product_template_to_solr(self): solr_model = self.env['apache.solr'] - + for template in self: if not template.active or template.type != 'product': continue @@ -63,7 +64,7 @@ class ProductTemplate(models.Model): category_id, category_name = category.id, category.name break - document = solr_model.get_single_doc('product', template.id) + document = solr_model.get_doc('product', template.id) document.update({ 'id': template.id, 'display_name_s': template.display_name, @@ -81,7 +82,7 @@ class ProductTemplate(models.Model): 'image_promotion_1_s': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_1', template.x_manufacture.id), 'image_promotion_2_s': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_2', template.x_manufacture.id), 'variants_name_t': variant_names, - 'variants_code_t': variant_codes, + 'variants_code_t': variant_codes, 'search_rank_i': template.search_rank, 'search_rank_weekly_i': template.search_rank_weekly, 'category_id_i': category_id, @@ -95,14 +96,14 @@ class ProductTemplate(models.Model): if not document.get('has_price_info_b'): template._sync_price_to_solr() - + self.solr().commit() - + def _sync_price_to_solr(self): solr_model = self.env['apache.solr'] solr = self.solr() - - for template in self: + + for template in self: price_excl_after_disc = price_excl = discount = tax = 0 flashsale_data = tier1 = tier2 = tier3 = {} @@ -117,7 +118,7 @@ class ProductTemplate(models.Model): tier1 = variant._get_pricelist_tier1() tier2 = variant._get_pricelist_tier2() tier3 = variant._get_pricelist_tier3() - + if template.product_variant_count == 1: price_excl = template.product_variant_id._get_website_price_exclude_tax() discount = template.product_variant_id._get_website_disc(0) @@ -127,8 +128,8 @@ class ProductTemplate(models.Model): tier1 = template.product_variant_id._get_pricelist_tier1() tier2 = template.product_variant_id._get_pricelist_tier2() tier3 = template.product_variant_id._get_pricelist_tier3() - - document = solr_model.get_single_doc('product', template.id) + + document = solr_model.get_doc('product', template.id) document.update({ 'id': template.id, 'flashsale_id_i': flashsale_data.get('flashsale_id', 0), @@ -147,14 +148,14 @@ class ProductTemplate(models.Model): 'price_tier2_f': tier2.get('price_tier2', 0), 'discount_tier3_f': tier3.get('discount_tier3', 0), 'price_tier3_f': tier3.get('price_tier3', 0), - 'has_price_info_b':True + 'has_price_info_b': True }) self.solr().add([document]) template.change_solr_data('Ada perubahan pada harga product') if not document.get('has_product_info_b'): template._sync_product_template_to_solr() - + self.solr().commit() def _sync_delete_solr(self): @@ -163,4 +164,70 @@ class ProductTemplate(models.Model): for variant in rec.product_variant_ids: variant._sync_delete_solr() self.solr().optimize() - self.solr().commit()
\ No newline at end of file + self.solr().commit() + + def solr_results(self, detail=False): + solr_model = self.env['apache.solr'] + pricelist = self.env.user_pricelist + price_tier = pricelist.get_tier_name() + + results = [] + for template in self: + doc = solr_model.get_doc('product', template.id) + if len(doc) == 0: continue + + discount_key = 'discount_f' + price_discount_key = 'price_discount_f' + if price_tier: + discount_key = f'discount_{price_tier}_f' + price_discount_key = f'price_{price_tier}_f' + + flashsale = template._get_active_flash_sale() + if flashsale: + discount_key = 'flashsale_discount_f' + price_discount_key = 'flashsale_price_f' + + result = { + 'id': doc.get('id'), + 'image': doc.get('image_s'), + 'code': doc.get('default_code_s'), + 'display_name': doc.get('display_name_s'), + 'name': doc.get('name_s'), + 'variant_total': doc.get('variant_total_i'), + 'weight': doc.get('weight_f'), + 'manufacture': None, + 'categories': [], + 'flash_sale': { + 'remaining_time': flashsale._remaining_time_in_second() or 0, + 'tag': flashsale.flashsale_tag or None + }, + 'lowest_price': { + 'price': doc.get('price_f'), + 'discount_percentage': doc.get(discount_key), + 'price_discount': doc.get(price_discount_key) + } + } + + manufacture_id = doc.get('manufacture_id_i') + if manufacture_id: + result['manufacture'] = { + 'id': manufacture_id, + 'name': doc.get('manufacture_name_s'), + 'image_promotion_1': doc.get('image_promotion_1_s'), + 'image_promotion_2': doc.get('image_promotion_2_s'), + } + + category_id = doc.get('category_id_i') + if category_id: + result['categories'] = [{ + 'id': category_id, + 'name': doc.get('category_name_s'), + }] + + if detail == True: + result['variants'] = template.product_variant_ids.solr_results() + result['description'] = template.website_description or '' + + results.append(result) + + return results diff --git a/indoteknik_custom/models/solr/website_categories_homepage.py b/indoteknik_custom/models/solr/website_categories_homepage.py index 21812acf..1b517ae6 100644 --- a/indoteknik_custom/models/solr/website_categories_homepage.py +++ b/indoteknik_custom/models/solr/website_categories_homepage.py @@ -59,9 +59,7 @@ class WebsiteCategoriesHomepage(models.Model): if category.status == 'tidak_tayang': continue - document = solr_model.get_single_doc('product_category_homepage', category.id) - products = [self.env['product.template'].api_single_response(x) for x in category.product_ids] - + document = solr_model.get_doc('product_category_homepage', category.id) document.update({ 'id': category.id, 'category_id_i': category.category_id.id, @@ -69,7 +67,7 @@ class WebsiteCategoriesHomepage(models.Model): 'image_s': self.env['ir.attachment'].api_image('website.categories.homepage', 'image', category.id), 'sequence_i': category.sequence or '', 'url_s': category.url or '', - 'products_s': json.dumps(products, indent=None), + 'product_ids': [x.id for x in category.product_ids] }) self.solr().add([document]) category.update_last_update_solr() |
