from odoo import models, api, fields from odoo.exceptions import UserError from datetime import datetime import logging import pysolr _logger = logging.getLogger(__name__) class ApacheSolr(models.Model): _name = 'apache.solr' _order = 'id desc' def _sync_product_to_solr(self): _logger.info('run sync to solr...') solr = pysolr.Solr('http://10.148.0.5:8983/solr/product/', timeout=100) templates = self.env['product.template'].search([ ('solr_flag', '=', 0), # ('id', '=', 21560) ], limit=500) document = [] counter = 0 for template in templates: counter += 1 price_excl_after_disc = price_excl = 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() 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() else: price_excl_after_disc = price_excl_after_disc price_excl = price_excl variants_name += variant.display_name+', ' variants_code += variant.default_code+', ' document=[({ 'id': template.id, 'display_name_s': template.display_name, 'name_s': template.name, 'default_code_s': template.default_code, 'product_rating_i': 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 or template.product_variant_id._get_website_price_exclude_tax(), 'price_discount_f': price_excl_after_disc or template.product_variant_id._get_website_price_after_disc_and_tax(), 'tax_f': template.product_variant_id._get_website_tax(), 'variant_total_i': template.product_variant_count, 'stock_total_i': template.qty_stock_vendor, 'weight_f': template.weight, 'manufacture_id_i': template.x_manufacture.id or 0, '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': template.categ_id.id or 0, 'category_name_s': template.categ_id.name, 'variants_name_t': variants_name, 'variants_code_t': variants_code, 'search_rank_i': template.search_rank, 'search_rank_weekly_i': template.search_rank_weekly, 'active_b': template.active })] solr.add(document) # add counter for monitoring _logger.info('%s / 500' % counter) _logger.info('Success add to solr product %s' % template.id)