summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2023-06-22 11:32:36 +0700
committerstephanchrst <stephanchrst@gmail.com>2023-06-22 11:32:36 +0700
commit59d2b16d8b2820476a68449ace57703a2e725a39 (patch)
tree881a0e56ef91cf44ec5b7a2151276716b71b178c
parentdab37603ea378fb84c2bbc0562b901011629063b (diff)
variants sync to solr for generate xml google merchant
-rw-r--r--indoteknik_custom/models/apache_solr.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/indoteknik_custom/models/apache_solr.py b/indoteknik_custom/models/apache_solr.py
index 9f187d78..6211dfc4 100644
--- a/indoteknik_custom/models/apache_solr.py
+++ b/indoteknik_custom/models/apache_solr.py
@@ -7,6 +7,7 @@ import time
_logger = logging.getLogger(__name__)
_solr = pysolr.Solr('http://10.148.0.5:8983/solr/product/', always_commit=True, timeout=30)
+_variants_solr = pysolr.Solr('http://10.148.0.5:8983/solr/variants/', always_commit=True, timeout=30)
# _solr = pysolr.Solr('http://34.101.189.218:8983/solr/product/', always_commit=True, timeout=30) # for development only
@@ -177,3 +178,69 @@ class ApacheSolr(models.Model):
_logger.info('price after discount : %s' % product._get_website_price_after_disc())
_logger.info('price excl after discount : %s' % product._get_website_price_after_disc_and_tax())
_logger.info('tax : %s' % product._get_website_tax())
+
+ def _sync_variants_to_solr(self, limit=500):
+ start_time = time.time()
+ _logger.info('run sync to solr variants...')
+ query = ["&","&","&",("product_tmpl_id.active","=",True),("product_tmpl_id.type","=","product"),("active","=",True),"|",("solr_flag","=",0),("solr_flag","=",2)]
+ # query = [('id', 'in', [184622, 204903])]
+
+ variants = self.env['product.product'].search(query, limit=limit)
+ documents = []
+ counter = 0
+ for variant in variants:
+ template_time = time.time()
+ counter += 1
+
+ price_excl_after_disc = price_excl = discount = tax = 0
+ price_excl = variant._get_website_price_exclude_tax()
+ price_excl_after_disc = variant._get_website_price_after_disc_and_tax()
+ discount = variant._get_website_disc(0)
+ tax = variant._get_website_tax()
+ flashsale_data = variant._get_flashsale_price()
+
+ category_id = ''
+ category_name = ''
+ for category in variant.product_tmpl_id.public_categ_ids:
+ category_id = category.id
+ category_name = category.name
+
+ document = {
+ '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),
+ 'price_f': price_excl,
+ 'discount_f': discount,
+ 'price_discount_f': price_excl_after_disc,
+ 'tax_f': tax,
+ '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 or 0,
+ 'category_name_s': category_name or '',
+ 'category_name': category_name or '',
+ 'search_rank_i': variant.product_tmpl_id.search_rank,
+ 'search_rank_weekly_i': variant.product_tmpl_id.search_rank_weekly,
+ 'flashsale_id_i': flashsale_data['flashsale_id'] or 0,
+ 'flashsale_name_s': flashsale_data['flashsale_name'] or '',
+ 'flashsale_base_price_f': flashsale_data['flashsale_base_price'] or 0,
+ 'flashsale_discount_f': flashsale_data['flashsale_discount'] or 0,
+ 'flashsale_price_f': flashsale_data['flashsale_price'] or 0,
+ }
+ documents.append(document)
+ variant.solr_flag = 1
+ # add counter for monitoring
+ _logger.info('[SYNC_VARIANTS_TO_SOLR] {}/{} {:.6f}'.format(counter, limit, time.time() - template_time))
+ _logger.info('[SYNC_VARIANTS_TO_SOLR] Success add to solr variants %s' % variant.id)
+ _variants_solr.add(documents)
+ end_time = time.time()
+ _logger.info("[SYNC_VARIANTS_TO_SOLR] Finish task add to solr. Time taken: {:.6f} seconds".format(end_time - start_time))