1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
from odoo import models, api, fields
from odoo.exceptions import UserError
from datetime import datetime
import logging
import pysolr
import time
_logger = logging.getLogger(__name__)
class ApacheSolr(models.Model):
_name = 'apache.solr'
_order = 'id desc'
def _sync_product_to_solr(self, limit=500):
# _solr = pysolr.Solr('http://10.148.0.5:8983/solr/product/', always_commit=True, timeout=30)
_solr = pysolr.Solr('http://192.168.23.5:8983/solr/product/', always_commit=True, timeout=30)
start_time = time.time()
_logger.info('run sync to solr...')
query = ["&","&",("type","=","product"),("active","=",True),"|",("solr_flag","=",0),("solr_flag","=",2)]
templates = self.env['product.template'].search(query, limit=limit)
documents = []
counter = 0
for template in templates:
template_time = time.time()
counter += 1
price_excl_after_disc = price_excl = discount = tax = 0
variants_name = variants_code = ''
flashsale_data = tier1 = tier2 = tier3 = {}
if template.product_variant_count > 1:
for variant in template.product_variant_ids:
if price_excl_after_disc == 0 or variant._get_website_price_after_disc_and_tax() < price_excl_after_disc:
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()
# add price tiering for base price, discount, and price after discount (tier 1 - 3)
tier1 = variant._get_pricelist_tier1()
tier2 = variant._get_pricelist_tier2()
tier3 = variant._get_pricelist_tier3()
else:
price_excl_after_disc = price_excl_after_disc
price_excl = price_excl
discount = discount
tax = tax
flashsale_data = flashsale_data
tier1 = tier1
tier2 = tier2
tier3 = tier3
variants_name += variant.display_name or ''+', '
variants_code += variant.default_code or ''+', '
else:
price_excl = template.product_variant_id._get_website_price_exclude_tax()
discount = template.product_variant_id._get_website_disc(0)
price_excl_after_disc = template.product_variant_id._get_website_price_after_disc_and_tax()
tax = template.product_variant_id._get_website_tax()
flashsale_data = template.product_variant_id._get_flashsale_price()
tier1 = template.product_variant_id._get_pricelist_tier1()
tier2 = template.product_variant_id._get_pricelist_tier2()
tier3 = template.product_variant_id._get_pricelist_tier3()
category_id = ''
category_name = ''
for category in template.public_categ_ids:
category_id = category.id
category_name = category.name
document = {
'id': template.id,
'display_name_s': template.display_name,
'name_s': template.name,
'default_code_s': template.default_code or '',
'product_rating_f': template.product_rating,
'product_id_i': template.id,
'image_s': self.env['ir.attachment'].api_image('product.template', 'image_512', template.id),
'price_f': price_excl,
'discount_f': discount,
'price_discount_f': price_excl_after_disc,
'tax_f': tax,
'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),
'category_id_i': category_id or 0,
'category_name_s': category_name or '',
'category_name': category_name or '',
'variants_name_t': variants_name,
'variants_code_t': variants_code,
'search_rank_i': template.search_rank,
'search_rank_weekly_i': template.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,
'discount_tier1_f': tier1['discount_tier1'] or 0,
'price_tier1_f': tier1['price_tier1'] or 0,
'discount_tier2_f': tier2['discount_tier2'] or 0,
'price_tier2_f': tier2['price_tier2'] or 0,
'discount_tier3_f': tier3['discount_tier3'] or 0,
'price_tier3_f': tier3['price_tier3'] or 0
}
documents.append(document)
template.solr_flag = 1
# add counter for monitoring
_logger.info('[SYNC_PRODUCT_TO_SOLR] {}/{} {:.6f}'.format(counter, limit, time.time() - template_time))
_logger.info('[SYNC_PRODUCT_TO_SOLR] Success add to solr product %s' % template.id)
_solr.add(documents)
end_time = time.time()
_logger.info("[SYNC_PRODUCT_TO_SOLR] Finish task add to solr. Time taken: {:.6f} seconds".format(end_time - start_time))
|