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 /indoteknik_custom/models/solr/product_template.py | |
| 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
Diffstat (limited to 'indoteknik_custom/models/solr/product_template.py')
| -rw-r--r-- | indoteknik_custom/models/solr/product_template.py | 103 |
1 files changed, 85 insertions, 18 deletions
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 |
