summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2023-09-12 17:06:33 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2023-09-12 17:06:33 +0700
commit9715888978cb93586f62ddb7ea153d0504bd290d (patch)
treefab22fd05a3bcf9d4d082aabfa18c19fb43ca117
parent038ebd6474ddf370fb5e0e6708d61e394a55fb44 (diff)
update code solr
-rw-r--r--indoteknik_custom/models/solr/apache_solr.py2
-rw-r--r--indoteknik_custom/models/solr/product_product.py97
-rw-r--r--indoteknik_custom/models/solr/product_template.py96
3 files changed, 100 insertions, 95 deletions
diff --git a/indoteknik_custom/models/solr/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py
index f05357bf..98004f00 100644
--- a/indoteknik_custom/models/solr/apache_solr.py
+++ b/indoteknik_custom/models/solr/apache_solr.py
@@ -25,7 +25,7 @@ class ApacheSolr(models.Model):
elif env == 'production':
url = 'http://34.101.189.218:8983/solr/'
- return pysolr.Solr(url + schema, always_commit=True, timeout=30)
+ return pysolr.Solr(url + schema, always_commit=False, timeout=10)
def get_doc(self, schema, id):
try:
diff --git a/indoteknik_custom/models/solr/product_product.py b/indoteknik_custom/models/solr/product_product.py
index 23ab51fd..452e9b94 100644
--- a/indoteknik_custom/models/solr/product_product.py
+++ b/indoteknik_custom/models/solr/product_product.py
@@ -42,43 +42,41 @@ class ProductProduct(models.Model):
category_name = category.name
break
- partial_update_data = {
+ document = solr_model.get_doc('variants', variant.id)
+ document.update({
'id': variant.id,
- '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}
- }
+ '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,
+ 'has_product_info_b': True,
+ 'search_rank_weekly_i': variant.product_tmpl_id.search_rank_weekly
+ })
- self.solr().add([partial_update_data])
+ self.solr().add(docs=[document], softCommit=True)
variant.change_solr_data('Perubahan pada data product')
-
- self.solr().commit()
+
+ if not document.get('has_price_info_b'):
+ variant._sync_price_to_solr()
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 = {}
@@ -93,28 +91,31 @@ class ProductProduct(models.Model):
tier2 = variant._get_pricelist_tier2()
tier3 = variant._get_pricelist_tier3()
+ document = solr_model.get_doc('variants', variant.id)
document.update({
- "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)},
+ "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,
+ "has_price_info_b": True,
+ "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)
})
- self.solr().add([document])
+ self.solr().add(docs=[document], softCommit=True)
variant.change_solr_data('Ada perubahan pada harga product')
-
- self.solr().commit()
+
+ if not document.get('has_product_info_b'):
+ variant._sync_variants_to_solr()
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 9d714713..ec42746c 100644
--- a/indoteknik_custom/models/solr/product_template.py
+++ b/indoteknik_custom/models/solr/product_template.py
@@ -70,37 +70,40 @@ class ProductTemplate(models.Model):
for category in template.public_categ_ids:
category_id, category_name = category.id, category.name
break
-
- partial_update_data = {
+
+ document = solr_model.get_doc('product', template.id)
+ document.update({
"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}
- }
+ "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,
+ 'has_product_info_b': True,
+ "category_id_i": category_id,
+ "category_name_s": category_name,
+ "category_name": category_name
+ })
- self.solr().add([partial_update_data])
+ self.solr().add(docs=[document], softCommit=True)
template.product_variant_ids._sync_variants_to_solr()
self.change_solr_data('Perubahan pada data product')
- self.solr().commit()
+ if not document.get('has_price_info_b'):
+ template._sync_price_to_solr()
def _sync_price_to_solr(self):
solr_model = self.env['apache.solr']
@@ -109,7 +112,7 @@ class ProductTemplate(models.Model):
for template in self:
document = {
"id": template.id,
- "has_price_info_b": {"set": True}
+ "has_price_info_b": True
}
flashsale_data = {}
@@ -129,31 +132,32 @@ class ProductTemplate(models.Model):
tier2 = variant._get_pricelist_tier2()
tier3 = variant._get_pricelist_tier3()
+ document = solr_model.get_doc('product', template.id)
document.update({
- "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)},
+ "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),
})
- self.solr().add([document])
+ self.solr().add(docs=[document], softCommit=True)
template.product_variant_ids._sync_price_to_solr()
template.change_solr_data('Ada perubahan pada harga product')
- self.solr().commit()
-
+ if not document.get('has_product_info_b'):
+ template._sync_product_template_to_solr()
def _sync_delete_solr(self):
for rec in self: