summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2023-06-22 11:58:05 +0700
committerstephanchrst <stephanchrst@gmail.com>2023-06-22 11:58:05 +0700
commitb90e863ebb88d4e4519a1ce5b52481567d7c480f (patch)
tree5cb4e84417dabfece8d55cf4b154a4a61dd997b7
parentc1a0d0e48ef64241c92947a8800f93af4ff109d0 (diff)
parentc289fcaf07839dfd965e6113a853026833ff0743 (diff)
Merge branch 'release' into receipt-approval
-rw-r--r--indoteknik_custom/models/apache_solr.py67
-rwxr-xr-xindoteknik_custom/models/x_manufactures.py11
2 files changed, 74 insertions, 4 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))
diff --git a/indoteknik_custom/models/x_manufactures.py b/indoteknik_custom/models/x_manufactures.py
index 710bfe8a..e48a5367 100755
--- a/indoteknik_custom/models/x_manufactures.py
+++ b/indoteknik_custom/models/x_manufactures.py
@@ -61,13 +61,16 @@ class XManufactures(models.Model):
('cache_reset_status', '=', 'reset'),
])
for manufacture in manufactures:
- products = self.env['product.template'].search([
+ templates = self.env['product.template'].search([
('x_manufacture', '=', manufacture.id),
('solr_flag', '=', 1),
])
- for product in products:
- product.solr_flag = 2
- _logger.info("Reset Solr Flag to 2 %s" % product.id)
+ for template in templates:
+ template.solr_flag = 2
+ _logger.info("Reset Solr Flag template to 2 %s" % template.id)
+ for variant in template.product_variant_ids:
+ variant.solr_flag = 2
+ _logger.info("Reset Solr Flag variant to 2 %s" % variant.id)
manufacture.cache_reset_status = 'done'
@api.onchange('x_name','image_promotion_1','image_promotion_2')