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): start_time = time.time() _logger.info('run sync to solr...') solr = pysolr.Solr('http://10.148.0.5:8983/solr/product/', always_commit=True, timeout=30) 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: counter += 1 price_excl_after_disc = price_excl = discount = tax = 0 variants_name = variants_code = '' if template.product_variant_count > 1: for variant in template.product_variant_ids: if price_excl_after_disc == 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() elif variant._get_website_price_after_disc_and_tax() < price_excl_after_disc: price_excl_after_disc = variant._get_website_price_after_disc_and_tax() price_excl = variant._get_website_price_exclude_tax() discount = variant._get_website_disc(0) tax = variant._get_website_tax() else: price_excl_after_disc = price_excl_after_disc price_excl = price_excl discount = discount tax = tax 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() 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 } documents.append(document) template.solr_flag = 1 # add counter for monitoring _logger.info('[SYNC_PRODUCT_TO_SOLR] %s/%i' % (counter, limit)) _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))