diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2023-09-12 15:10:09 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2023-09-12 15:10:09 +0700 |
| commit | 888a3ca6337a1622f4f16b98b700cdd437ab6023 (patch) | |
| tree | e9ebe92f715cce391481a2edf79b539ab5c82d87 | |
| parent | 6fa6d7028011dc5dcfbc14fea3eab9ea53f6b2e3 (diff) | |
change code send data to solr
| -rw-r--r-- | indoteknik_custom/models/solr/product_product.py | 99 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/product_template.py | 102 |
2 files changed, 98 insertions, 103 deletions
diff --git a/indoteknik_custom/models/solr/product_product.py b/indoteknik_custom/models/solr/product_product.py index 54b098e5..a3bd3d04 100644 --- a/indoteknik_custom/models/solr/product_product.py +++ b/indoteknik_custom/models/solr/product_product.py @@ -35,40 +35,43 @@ class ProductProduct(models.Model): category_name = category.name break - document = solr_model.get_doc('variants', variant.id) - document.update({ + partial_update_data = { 'id': variant.id, - 'display_name_s': variant.display_name, - 'name_s': variant.name, - 'default_code_s': variant.default_code or '', - 'product_rating_f': variant.product_tmpl_id.virtual_rating, - 'product_id_i': variant.id, - 'template_id_i': variant.product_tmpl_id.id, - 'image_s': self.env['ir.attachment'].api_image('product.template', 'image_512', variant.product_tmpl_id.id), - 'stock_total_f': variant.qty_stock_vendor, - 'weight_f': variant.product_tmpl_id.weight, - 'manufacture_id_i': variant.product_tmpl_id.x_manufacture.id or 0, - 'manufacture_name_s': variant.product_tmpl_id.x_manufacture.x_name or '', - 'manufacture_name': variant.product_tmpl_id.x_manufacture.x_name or '', - 'image_promotion_1_s': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_1', variant.product_tmpl_id.x_manufacture.id), - 'image_promotion_2_s': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_2', variant.product_tmpl_id.x_manufacture.id), - 'category_id_i': category_id, - 'category_name_s': category_name, - 'category_name': category_name, - 'search_rank_i': variant.product_tmpl_id.search_rank, - 'search_rank_weekly_i': variant.product_tmpl_id.search_rank_weekly, - 'has_product_info_b': True - }) - self.solr().add([document]) + 'display_name_s': {"set": variant.display_name}, + 'name_s': {"set": variant.name}, + 'default_code_s': {"set": variant.default_code or ''}, + 'product_rating_f': {"set": variant.product_tmpl_id.virtual_rating}, + 'product_id_i': {"set": variant.id}, + 'template_id_i': {"set": variant.product_tmpl_id.id}, + 'image_s': {"set": self.env['ir.attachment'].api_image('product.template', 'image_512', variant.product_tmpl_id.id)}, + 'stock_total_f': {"set": variant.qty_stock_vendor}, + 'weight_f': {"set": variant.product_tmpl_id.weight}, + 'manufacture_id_i': {"set": variant.product_tmpl_id.x_manufacture.id or 0}, + 'manufacture_name_s': {"set": variant.product_tmpl_id.x_manufacture.x_name or ''}, + 'manufacture_name': {"set": variant.product_tmpl_id.x_manufacture.x_name or ''}, + 'image_promotion_1_s': {"set": self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_1', variant.product_tmpl_id.x_manufacture.id)}, + 'image_promotion_2_s': {"set": self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_2', variant.product_tmpl_id.x_manufacture.id)}, + 'category_id_i': {"set": category_id}, + 'category_name_s': {"set": category_name}, + 'category_name': {"set": category_name}, + 'search_rank_i': {"set": variant.product_tmpl_id.search_rank}, + 'search_rank_weekly_i': {"set": variant.product_tmpl_id.search_rank_weekly} + } + + self.solr().add([partial_update_data]) variant.change_solr_data('Perubahan pada data product') - - if not document.get('has_price_info_b'): - variant._sync_price_to_solr() - + + self.solr().commit() + def _sync_price_to_solr(self): solr_model = self.env['apache.solr'] for variant in self: + document = { + "id": variant.id, + "has_price_info_b": {"set": True} + } + price_excl_after_disc = price_excl = discount = tax = 0 flashsale_data = tier1 = tier2 = tier3 = {} @@ -83,32 +86,28 @@ class ProductProduct(models.Model): tier2 = variant._get_pricelist_tier2() tier3 = variant._get_pricelist_tier3() - document = solr_model.get_doc('variants', variant.id) document.update({ - 'id': variant.id, - 'flashsale_id_i': flashsale_data.get('flashsale_id', 0), - 'flashsale_tag_s': flashsale_data.get('flashsale_tag', ''), - 'flashsale_name_s': flashsale_data.get('flashsale_name', ''), - 'flashsale_base_price_f': flashsale_data.get('flashsale_base_price', 0), - 'flashsale_discount_f': flashsale_data.get('flashsale_discount', 0), - 'flashsale_price_f': flashsale_data.get('flashsale_price', 0), - 'price_f': price_excl, - 'discount_f': discount, - 'price_discount_f': price_excl_after_disc, - 'tax_f': tax, - 'discount_tier1_f': tier1.get('discount_tier1', 0), - 'price_tier1_f': tier1.get('price_tier1', 0), - 'discount_tier2_f': tier2.get('discount_tier2', 0), - '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 + "flashsale_id_i": {"set": flashsale_data.get('flashsale_id', 0)}, + "flashsale_tag_s": {"set": flashsale_data.get('flashsale_tag', '')}, + "flashsale_name_s": {"set": flashsale_data.get('flashsale_name', '')}, + "flashsale_base_price_f": {"set": flashsale_data.get('flashsale_base_price', 0)}, + "flashsale_discount_f": {"set": flashsale_data.get('flashsale_discount', 0)}, + "flashsale_price_f": {"set": flashsale_data.get('flashsale_price', 0)}, + "price_f": {"set": price_excl}, + "discount_f": {"set": discount}, + "price_discount_f": {"set": price_excl_after_disc}, + "tax_f": {"set": tax}, + "discount_tier1_f": {"set": tier1.get('discount_tier1', 0)}, + "price_tier1_f": {"set": tier1.get('price_tier1', 0)}, + "discount_tier2_f": {"set": tier2.get('discount_tier2', 0)}, + "price_tier2_f": {"set": tier2.get('price_tier2', 0)}, + "discount_tier3_f": {"set": tier3.get('discount_tier3', 0)}, + "price_tier3_f": {"set": tier3.get('price_tier3', 0)}, }) self.solr().add([document]) variant.change_solr_data('Ada perubahan pada harga product') - - if not document.get('has_product_info_b'): - variant._sync_variants_to_solr() + + self.solr().commit() def _sync_delete_solr(self): for rec in self: diff --git a/indoteknik_custom/models/solr/product_template.py b/indoteknik_custom/models/solr/product_template.py index e8bdc1af..b93d0a4f 100644 --- a/indoteknik_custom/models/solr/product_template.py +++ b/indoteknik_custom/models/solr/product_template.py @@ -64,39 +64,35 @@ class ProductTemplate(models.Model): category_id, category_name = category.id, category.name break - document = solr_model.get_doc('product', template.id) - document.update({ - 'id': template.id, - 'display_name_s': template.display_name, - 'name_s': template.name, - 'default_code_s': template.default_code or '', - 'product_rating_f': template.virtual_rating, - 'product_id_i': template.id, - 'image_s': self.env['ir.attachment'].api_image('product.template', 'image_512', template.id), - 'variant_total_i': template.product_variant_count, - 'stock_total_f': template.qty_stock_vendor, - 'weight_f': template.weight, - 'manufacture_id_i': template.x_manufacture.id or 0, - 'manufacture_name_s': template.x_manufacture.x_name or '', - 'manufacture_name': template.x_manufacture.x_name or '', - '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, - 'search_rank_i': template.search_rank, - 'search_rank_weekly_i': template.search_rank_weekly, - 'category_id_i': category_id, - 'category_name_s': category_name, - 'category_name': category_name, - 'has_product_info_b': True - }) - self.solr().add([document]) + partial_update_data = { + "id": template.id, + "display_name_s": {"set": template.display_name}, + "name_s": {"set": template.name}, + "default_code_s": {"set": template.default_code or ''}, + "product_rating_f": {"set": template.virtual_rating}, + "product_id_i": {"set": template.id}, + "image_s": {"set": self.env['ir.attachment'].api_image('product.template', 'image_512', template.id)}, + "variant_total_i": {"set": template.product_variant_count}, + "stock_total_f": {"set": template.qty_stock_vendor}, + "weight_f": {"set": template.weight}, + "manufacture_id_i": {"set": template.x_manufacture.id or 0}, + "manufacture_name_s": {"set": template.x_manufacture.x_name or ''}, + "manufacture_name": {"set": template.x_manufacture.x_name or ''}, + "image_promotion_1_s": {"set": self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_1', template.x_manufacture.id)}, + "image_promotion_2_s": {"set": self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_2', template.x_manufacture.id)}, + "variants_name_t": {"set": variant_names}, + "variants_code_t": {"set": variant_codes}, + "search_rank_i": {"set": template.search_rank}, + "search_rank_weekly_i": {"set": template.search_rank_weekly}, + "category_id_i": {"set": category_id}, + "category_name_s": {"set": category_name}, + "category_name": {"set": category_name} + } + + self.solr().add([partial_update_data]) template.product_variant_ids._sync_variants_to_solr() self.change_solr_data('Perubahan pada data product') - if not document.get('has_price_info_b'): - template._sync_price_to_solr() - self.solr().commit() def _sync_price_to_solr(self): @@ -104,9 +100,12 @@ class ProductTemplate(models.Model): solr = self.solr() for template in self: - document = solr_model.get_doc('product', template.id) + document = { + "id": template.id, + "has_price_info_b": {"set": True} + } flashsale_data = {} - + for variant in template.product_variant_ids: variant_flashsale = variant._get_flashsale_price() variant_flashsale_price = variant_flashsale.get('flashsale_price', 0) @@ -124,34 +123,31 @@ class ProductTemplate(models.Model): tier3 = variant._get_pricelist_tier3() document.update({ - 'id': template.id, - 'flashsale_id_i': flashsale_data.get('flashsale_id', 0), - 'flashsale_tag_s': flashsale_data.get('flashsale_tag', ''), - 'flashsale_name_s': flashsale_data.get('flashsale_name', ''), - 'flashsale_base_price_f': flashsale_data.get('flashsale_base_price', 0), - 'flashsale_discount_f': flashsale_data.get('flashsale_discount', 0), - 'flashsale_price_f': flashsale_data.get('flashsale_price', 0), - 'price_f': price_excl, - 'discount_f': discount, - 'price_discount_f': price_excl_after_disc, - 'tax_f': tax, - 'discount_tier1_f': tier1.get('discount_tier1', 0), - 'price_tier1_f': tier1.get('price_tier1', 0), - 'discount_tier2_f': tier2.get('discount_tier2', 0), - '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 + "flashsale_id_i": {"set": flashsale_data.get('flashsale_id', 0)}, + "flashsale_tag_s": {"set": flashsale_data.get('flashsale_tag', '')}, + "flashsale_name_s": {"set": flashsale_data.get('flashsale_name', '')}, + "flashsale_base_price_f": {"set": flashsale_data.get('flashsale_base_price', 0)}, + "flashsale_discount_f": {"set": flashsale_data.get('flashsale_discount', 0)}, + "flashsale_price_f": {"set": flashsale_data.get('flashsale_price', 0)}, + "price_f": {"set": price_excl}, + "discount_f": {"set": discount}, + "price_discount_f": {"set": price_excl_after_disc}, + "tax_f": {"set": tax}, + "discount_tier1_f": {"set": tier1.get('discount_tier1', 0)}, + "price_tier1_f": {"set": tier1.get('price_tier1', 0)}, + "discount_tier2_f": {"set": tier2.get('discount_tier2', 0)}, + "price_tier2_f": {"set": tier2.get('price_tier2', 0)}, + "discount_tier3_f": {"set": tier3.get('discount_tier3', 0)}, + "price_tier3_f": {"set": tier3.get('price_tier3', 0)}, }) + self.solr().add([document]) template.product_variant_ids._sync_price_to_solr() 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): for rec in self: self.solr().delete(rec.id) |
