From f6876ff69e37169909a40059d2945e1d84be0db3 Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Tue, 22 Aug 2023 11:22:52 +0700 Subject: auto sync solr --- indoteknik_custom/models/apache_solr.py | 119 ++++++++++++++------- indoteknik_custom/models/product_pricelist.py | 128 +++++++++++++++++++++- indoteknik_custom/models/product_template.py | 148 +++++++++++++++++++++++++- indoteknik_custom/views/product_template.xml | 2 + 4 files changed, 351 insertions(+), 46 deletions(-) diff --git a/indoteknik_custom/models/apache_solr.py b/indoteknik_custom/models/apache_solr.py index 086fb6d0..8dc5dfbe 100644 --- a/indoteknik_custom/models/apache_solr.py +++ b/indoteknik_custom/models/apache_solr.py @@ -6,15 +6,30 @@ import pysolr 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://192.168.23.5:8983/solr/product/', always_commit=True, timeout=30) +_variants_solr = pysolr.Solr('http://192.168.23.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 class ApacheSolr(models.Model): - _name = 'apache.solr' + _name = 'apache.solr' _order = 'id desc' + def get_single_doc(self, solr, id): + try: + return solr.search(f'id:{id}').docs[0] + except: + return {} + def connect(self, env): + if env == 'production_template': + return pysolr.Solr('http://192.168.23.5:8983/solr/product/', always_commit=True, timeout=30) + elif env == 'production_variant': + return pysolr.Solr('http://192.168.23.5:8983/solr/variants/', always_commit=True, timeout=30) + elif env == 'local_template': + return pysolr.Solr('http://192.168.23.5:8983/solr/product/', always_commit=True, timeout=30) + elif env == 'local_variant': + return pysolr.Solr('http://192.168.23.5:8983/solr/variants/', always_commit=True, timeout=30) + def _update_stock_product_to_solr(self, limit=10000): current_time = datetime.now() delta_time = current_time - timedelta(days=3) @@ -67,11 +82,67 @@ class ApacheSolr(models.Model): _logger.info(documents) _solr.add(documents, fieldUpdates={'product_rating_f':'set'}) - def _sync_product_to_solr(self, limit=500): + def _sync_product_template_to_solr(self, limit=500): + 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 + variants_name = variants_code = '' + if template.product_variant_count > 1: + for variant in template.product_variant_ids: + variants_name += variant.display_name or ''+', ' + variants_code += variant.default_code or ''+', ' + + category_id = '' + category_name = '' + for category in template.public_categ_ids: + category_id = category.id + category_name = category.name + + document = { + # Start Product Template + 'id': template.id, + 'display_name_s': template.display_name, + 'name_s': template.name, + 'default_code_s': template.default_code or '', + 'product_rating_f': template.virtual_rating, + 'product_id_i': template.id, + 'image_s': self.env['ir.attachment'].api_image('product.template', 'image_512', template.id), + '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), + 'variants_name_t': variants_name, + 'variants_code_t': variants_code, + 'search_rank_i': template.search_rank, + 'search_rank_weekly_i': template.search_rank_weekly, + 'category_id_i': category_id or 0, + 'category_name_s': category_name or '', + 'category_name': category_name or '', + # End Product Template + } + documents.append(document) + template.solr_flag = 1 + _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)) + + def _sync_product_pricelist_to_solr(self, limit=500): start_time = time.time() _logger.info('run sync to solr...') query = ["&","&",("type","=","product"),("active","=",True),"|",("solr_flag","=",0),("solr_flag","=",2)] - # query = [('id', '=', 97942)] templates = self.env['product.template'].search(query, limit=limit) documents = [] @@ -116,51 +187,25 @@ class ApacheSolr(models.Model): 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.virtual_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, + # Start Product Pricelist 'flashsale_id_i': flashsale_data['flashsale_id'] or 0, 'flashsale_tag_s': flashsale_data['flashsale_tag'] or '', '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, + 'price_f': price_excl, + 'discount_f': discount, + 'price_discount_f': price_excl_after_disc, + 'tax_f': tax, '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 + # End Product Pricelist } documents.append(document) template.solr_flag = 1 diff --git a/indoteknik_custom/models/product_pricelist.py b/indoteknik_custom/models/product_pricelist.py index 2053accc..477f6645 100644 --- a/indoteknik_custom/models/product_pricelist.py +++ b/indoteknik_custom/models/product_pricelist.py @@ -1,8 +1,10 @@ from odoo import models, fields, api from odoo.exceptions import UserError from datetime import datetime +import logging +import time - +_logger = logging.getLogger(__name__) class ProductPricelist(models.Model): _inherit = 'product.pricelist' @@ -39,7 +41,123 @@ class ProductPricelistItem(models.Model): manufacture_id = fields.Many2one('x_manufactures', string='Manufacture') - @api.constrains('fixed_price','price_discount') - def update_product_solr_flag(self): - for item in self: - item.product_id.product_tmpl_id.solr_flag = 2 \ No newline at end of file + @api.constrains('applied_on','product_id','base','base_pricelist_id','price_discount') + def update_template_solr(self): + price_excl_after_disc = price_excl = discount = tax = 0 + variants_name = variants_code = '' + flashsale_data = tier1 = tier2 = tier3 = {} + + template = self.product_id.product_tmpl_id + 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: + variants_name = template.display_name + 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() + + solr_model = self.env['apache.solr'] + solr = solr_model.connect('local_template') + document = solr_model.get_single_doc(solr, template.id) + document.update({ + 'id': template.id, + 'flashsale_id_i': flashsale_data['flashsale_id'] or 0, + 'flashsale_tag_s': flashsale_data['flashsale_tag'] or '', + '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, + 'price_f': price_excl, + 'discount_f': discount, + 'price_discount_f': price_excl_after_disc, + 'tax_f': tax, + '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 + }) + solr.add([document]) + template.change_solr_data('Ada perubahan pada harga product') + + @api.constrains('applied_on','product_id','base','base_pricelist_id','price_discount') + def update_variant_solr(self): + price_excl_after_disc = price_excl = discount = tax = 0 + variants_name = variants_code = '' + flashsale_data = tier1 = tier2 = tier3 = {} + + product = self.product_id + for variant in product: + 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 ''+', ' + + solr_model = self.env['apache.solr'] + solr = solr_model.connect('local_variant') + document = solr_model.get_single_doc(solr, product.id) + document.update({ + 'id': product.id, + 'flashsale_id_i': flashsale_data['flashsale_id'] or 0, + 'flashsale_tag_s': flashsale_data['flashsale_tag'] or '', + '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, + 'price_f': price_excl, + 'discount_f': discount, + 'price_discount_f': price_excl_after_disc, + 'tax_f': tax, + '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 + }) + solr.add([document]) + product.change_solr_data('Ada perubahan pada harga product') \ No newline at end of file diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index bc54b703..128297f1 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -1,5 +1,5 @@ from odoo import fields, models, api -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from odoo.exceptions import AccessError, UserError, ValidationError import logging import requests @@ -50,7 +50,14 @@ class ProductTemplate(models.Model): is_new_product = fields.Boolean(string='Produk Baru', help='Centang jika ingin ditammpilkan di website sebagai segment Produk Baru') seq_new_product = fields.Integer(string='Seq New Product', help='Urutan Sequence New Product') - + last_update_solr = fields.Datetime(string='Last Update Solr') + desc_update_solr = fields.Char(string='Desc Update Solr') + + def change_solr_data(self, desc): + current_time = datetime.utcnow() + self.desc_update_solr = desc + self.last_update_solr = current_time + # def write(self, vals): # if 'solr_flag' not in vals and self.solr_flag == 1: # vals['solr_flag'] = 2 @@ -256,7 +263,66 @@ class ProductTemplate(models.Model): self.env['token.storage'].create([values]) return values - + @api.constrains( + 'name', + 'default_code', + 'virtual_rating', + 'product_variant_count', + 'weight', + 'x_manufacture', + 'public_categ_ids', + 'product_variant_ids.display_name', + 'product_variant_ids.default_code', + 'search_rank', + 'search_rank_weekly', + 'image_1920' + ) + def _sync_product_template_to_solr(self, limit=500): + template = self + if not template.active or template.type != 'product': + return + + variants_name = variants_code = '' + if template.product_variant_count > 1: + for variant in template.product_variant_ids: + variants_name += variant.display_name or ''+', ' + variants_code += variant.default_code or ''+', ' + + category_id = '' + category_name = '' + for category in template.public_categ_ids: + category_id = category.id + category_name = category.name + solr_model = self.env['apache.solr'] + solr = solr_model.connect('local_template') + document = solr_model.get_single_doc(solr, template.id) + document.update({ + 'id': template.id, + 'display_name_s': template.display_name, + 'name_s': template.name, + 'default_code_s': template.default_code or '', + 'product_rating_f': template.virtual_rating, + 'product_id_i': template.id, + 'image_s': self.env['ir.attachment'].api_image('product.template', 'image_512', template.id), + '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), + 'variants_name_t': variants_name, + 'variants_code_t': variants_code, + 'search_rank_i': template.search_rank, + 'search_rank_weekly_i': template.search_rank_weekly, + 'category_id_i': category_id or 0, + 'category_name_s': category_name or '', + 'category_name': category_name or '', + }) + solr.add([document]) + self.change_solr_data('Perubahan pada data product') + class ProductProduct(models.Model): _inherit = "product.product" web_price = fields.Float( @@ -278,6 +344,13 @@ class ProductProduct(models.Model): qty_onhand_bandengan = fields.Float(string='Qty Incoming Bandengan', compute='_get_qty_onhand_bandengan') qty_incoming_bandengan = fields.Float(string='Qty Incoming Bandengan', compute='_get_qty_incoming_bandengan') sla_version = fields.Integer(string="SLA Version", default=0) + last_update_solr = fields.Datetime(string='Last Update Solr') + desc_update_solr = fields.Char(string='Desc Update Solr') + + def change_solr_data(self, desc): + current_time = datetime.now() + self.desc_update_solr = desc + self.last_update_solr = current_time def _get_qty_incoming_bandengan(self): for product in self: @@ -329,4 +402,71 @@ class ProductProduct(models.Model): ('start_date', '<=', current_time), ('end_date', '>=', current_time) ], limit=1) - return pricelist \ No newline at end of file + return pricelist + + @api.constrains( + 'name', + 'default_code', + 'virtual_rating', + 'weight', + 'x_manufacture', + 'public_categ_ids', + 'product_tmpl_id.default_code', + 'product_tmpl_id.image_1920', + 'search_rank', + 'search_rank_weekly', + 'image_1920' + ) + def _sync_variants_to_solr(self): + variant = self + if not variant.product_tmpl_id.active and variant.product_tmpl_id.type != 'product': + return + + 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 + solr_model = self.env['apache.solr'] + solr = solr_model.connect('local_variant') + document = solr_model.get_single_doc(solr, variant.id) + document.update({ + '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, + }) + solr.add([document]) + self.change_solr_data('Perubahan pada data product') \ No newline at end of file diff --git a/indoteknik_custom/views/product_template.xml b/indoteknik_custom/views/product_template.xml index 558805b1..82b7ce4d 100755 --- a/indoteknik_custom/views/product_template.xml +++ b/indoteknik_custom/views/product_template.xml @@ -9,6 +9,8 @@ + + -- cgit v1.2.3 From b8356524ab2d381affe438a85e1662a08fcd3026 Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Tue, 22 Aug 2023 11:23:56 +0700 Subject: auto sync solr --- indoteknik_custom/models/apache_solr.py | 25 ++++++++++++++----------- indoteknik_custom/models/product_pricelist.py | 4 ++-- indoteknik_custom/models/product_template.py | 4 ++-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/indoteknik_custom/models/apache_solr.py b/indoteknik_custom/models/apache_solr.py index 8dc5dfbe..974718a5 100644 --- a/indoteknik_custom/models/apache_solr.py +++ b/indoteknik_custom/models/apache_solr.py @@ -4,10 +4,11 @@ from datetime import datetime, timedelta import logging import pysolr import time +from odoo.tools.config import config _logger = logging.getLogger(__name__) -_solr = pysolr.Solr('http://192.168.23.5:8983/solr/product/', always_commit=True, timeout=30) -_variants_solr = pysolr.Solr('http://192.168.23.5:8983/solr/variants/', always_commit=True, timeout=30) +_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 @@ -20,15 +21,17 @@ class ApacheSolr(models.Model): return solr.search(f'id:{id}').docs[0] except: return {} - def connect(self, env): - if env == 'production_template': - return pysolr.Solr('http://192.168.23.5:8983/solr/product/', always_commit=True, timeout=30) - elif env == 'production_variant': - return pysolr.Solr('http://192.168.23.5:8983/solr/variants/', always_commit=True, timeout=30) - elif env == 'local_template': - return pysolr.Solr('http://192.168.23.5:8983/solr/product/', always_commit=True, timeout=30) - elif env == 'local_variant': - return pysolr.Solr('http://192.168.23.5:8983/solr/variants/', always_commit=True, timeout=30) + + def connect(self, schema): + env = config.get('solr_env', 'development') + + url = '' + if env == 'development': + url = 'http://192.168.23.5:8983/solr/' + elif env == 'production': + url = 'http://10.148.0.5:8983/solr/' + + return pysolr.Solr(url + schema, always_commit=True, timeout=30) def _update_stock_product_to_solr(self, limit=10000): current_time = datetime.now() diff --git a/indoteknik_custom/models/product_pricelist.py b/indoteknik_custom/models/product_pricelist.py index 477f6645..9d2bdf82 100644 --- a/indoteknik_custom/models/product_pricelist.py +++ b/indoteknik_custom/models/product_pricelist.py @@ -83,7 +83,7 @@ class ProductPricelistItem(models.Model): tier3 = template.product_variant_id._get_pricelist_tier3() solr_model = self.env['apache.solr'] - solr = solr_model.connect('local_template') + solr = solr_model.connect('product') document = solr_model.get_single_doc(solr, template.id) document.update({ 'id': template.id, @@ -138,7 +138,7 @@ class ProductPricelistItem(models.Model): variants_code += variant.default_code or ''+', ' solr_model = self.env['apache.solr'] - solr = solr_model.connect('local_variant') + solr = solr_model.connect('variants') document = solr_model.get_single_doc(solr, product.id) document.update({ 'id': product.id, diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index 128297f1..1283a5cb 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -294,7 +294,7 @@ class ProductTemplate(models.Model): category_id = category.id category_name = category.name solr_model = self.env['apache.solr'] - solr = solr_model.connect('local_template') + solr = solr_model.connect('product') document = solr_model.get_single_doc(solr, template.id) document.update({ 'id': template.id, @@ -435,7 +435,7 @@ class ProductProduct(models.Model): category_id = category.id category_name = category.name solr_model = self.env['apache.solr'] - solr = solr_model.connect('local_variant') + solr = solr_model.connect('variants') document = solr_model.get_single_doc(solr, variant.id) document.update({ 'id': variant.id, -- cgit v1.2.3 From b173abf78bd52ba0cd10829e1a2b31efc371f3cf Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 24 Aug 2023 09:17:29 +0700 Subject: Refactor update solr sync --- indoteknik_custom/models/apache_solr.py | 4 +- indoteknik_custom/models/product_pricelist.py | 27 ++----- indoteknik_custom/models/product_template.py | 101 ++++++++------------------ indoteknik_custom/models/stock_vendor.py | 6 -- indoteknik_custom/models/x_manufactures.py | 10 --- indoteknik_custom/views/product_pricelist.xml | 8 -- 6 files changed, 41 insertions(+), 115 deletions(-) diff --git a/indoteknik_custom/models/apache_solr.py b/indoteknik_custom/models/apache_solr.py index 974718a5..bd4dd3dc 100644 --- a/indoteknik_custom/models/apache_solr.py +++ b/indoteknik_custom/models/apache_solr.py @@ -16,9 +16,9 @@ class ApacheSolr(models.Model): _name = 'apache.solr' _order = 'id desc' - def get_single_doc(self, solr, id): + def get_single_doc(self, schema, id): try: - return solr.search(f'id:{id}').docs[0] + return self.connect(schema).search(f'id:{id}').docs[0] except: return {} diff --git a/indoteknik_custom/models/product_pricelist.py b/indoteknik_custom/models/product_pricelist.py index 9d2bdf82..73eebdcf 100644 --- a/indoteknik_custom/models/product_pricelist.py +++ b/indoteknik_custom/models/product_pricelist.py @@ -26,25 +26,19 @@ class ProductPricelist(models.Model): remaining_time = (self.end_date - datetime.now()).total_seconds() remaining_time = round(remaining_time) return max(remaining_time, 0) - - def update_product_solr_flag(self): - for item in self.item_ids: - item.product_id.product_tmpl_id.solr_flag = 2 - return { - 'type': 'ir.actions.client', - 'tag': 'display_notification', - 'params': { 'title': 'Notification', 'message': f'{len(self.item_ids)} produk akan diupdate ke SOLR' } - } class ProductPricelistItem(models.Model): _inherit = 'product.pricelist.item' manufacture_id = fields.Many2one('x_manufactures', string='Manufacture') - @api.constrains('applied_on','product_id','base','base_pricelist_id','price_discount') + @api.constrains('applied_on', 'product_id', 'base', 'base_pricelist_id', 'price_discount') + def _constrains_related_solr_field(self): + self.update_template_solr() + self.update_variant_solr() + def update_template_solr(self): price_excl_after_disc = price_excl = discount = tax = 0 - variants_name = variants_code = '' flashsale_data = tier1 = tier2 = tier3 = {} template = self.product_id.product_tmpl_id @@ -69,10 +63,7 @@ class ProductPricelistItem(models.Model): tier1 = tier1 tier2 = tier2 tier3 = tier3 - variants_name += variant.display_name or ''+', ' - variants_code += variant.default_code or ''+', ' else: - variants_name = template.display_name 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() @@ -84,7 +75,7 @@ class ProductPricelistItem(models.Model): solr_model = self.env['apache.solr'] solr = solr_model.connect('product') - document = solr_model.get_single_doc(solr, template.id) + document = solr_model.get_single_doc('product', template.id) document.update({ 'id': template.id, 'flashsale_id_i': flashsale_data['flashsale_id'] or 0, @@ -107,10 +98,8 @@ class ProductPricelistItem(models.Model): solr.add([document]) template.change_solr_data('Ada perubahan pada harga product') - @api.constrains('applied_on','product_id','base','base_pricelist_id','price_discount') def update_variant_solr(self): price_excl_after_disc = price_excl = discount = tax = 0 - variants_name = variants_code = '' flashsale_data = tier1 = tier2 = tier3 = {} product = self.product_id @@ -134,12 +123,10 @@ class ProductPricelistItem(models.Model): tier1 = tier1 tier2 = tier2 tier3 = tier3 - variants_name += variant.display_name or ''+', ' - variants_code += variant.default_code or ''+', ' solr_model = self.env['apache.solr'] solr = solr_model.connect('variants') - document = solr_model.get_single_doc(solr, product.id) + document = solr_model.get_single_doc('variants', product.id) document.update({ 'id': product.id, 'flashsale_id_i': flashsale_data['flashsale_id'] or 0, diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index 1283a5cb..3caa3125 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -58,11 +58,6 @@ class ProductTemplate(models.Model): self.desc_update_solr = desc self.last_update_solr = current_time - # def write(self, vals): - # if 'solr_flag' not in vals and self.solr_flag == 1: - # vals['solr_flag'] = 2 - # return super().write(vals) - def _compute_virtual_rating(self): for product in self: rate = 0 @@ -126,12 +121,6 @@ class ProductTemplate(models.Model): product.default_code = 'ITV.'+str(product.id) _logger.info('Updated Variant %s' % product.name) - @api.onchange('name','default_code','x_manufacture','product_rating','website_description','image_1920','weight','public_categ_ids') - def update_solr_flag(self): - for tmpl in self: - if tmpl.solr_flag == 1: - tmpl.solr_flag = 2 - def _compute_qty_stock_vendor(self): for product_template in self: product_template.qty_stock_vendor = 0 @@ -264,38 +253,34 @@ class ProductTemplate(models.Model): return values @api.constrains( - 'name', - 'default_code', - 'virtual_rating', - 'product_variant_count', - 'weight', - 'x_manufacture', - 'public_categ_ids', - 'product_variant_ids.display_name', - 'product_variant_ids.default_code', - 'search_rank', - 'search_rank_weekly', - 'image_1920' - ) + 'name', + 'default_code', + 'weight', + 'x_manufacture', + 'public_categ_ids', + 'product_variant_ids.display_name', + 'product_variant_ids.default_code', + 'search_rank', + 'search_rank_weekly', + 'image_1920' + ) def _sync_product_template_to_solr(self, limit=500): template = self if not template.active or template.type != 'product': return - - variants_name = variants_code = '' - if template.product_variant_count > 1: - for variant in template.product_variant_ids: - variants_name += variant.display_name or ''+', ' - variants_code += variant.default_code or ''+', ' - category_id = '' + variant_names = ', '.join([x.display_name or '' for x in template.product_variant_ids]) + variant_codes = ', '.join([x.default_code or '' for x in template.product_variant_ids]) + + category_id = 0 category_name = '' for category in template.public_categ_ids: - category_id = category.id - category_name = category.name + category_id, category_name = category.id, category.name + break + solr_model = self.env['apache.solr'] solr = solr_model.connect('product') - document = solr_model.get_single_doc(solr, template.id) + document = solr_model.get_single_doc('product', template.id) document.update({ 'id': template.id, 'display_name_s': template.display_name, @@ -312,13 +297,13 @@ class ProductTemplate(models.Model): '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), - 'variants_name_t': variants_name, - 'variants_code_t': variants_code, + 'variants_name_t': variant_names, + 'variants_code_t': variant_codes, 'search_rank_i': template.search_rank, 'search_rank_weekly_i': template.search_rank_weekly, - 'category_id_i': category_id or 0, - 'category_name_s': category_name or '', - 'category_name': category_name or '', + 'category_id_i': category_id, + 'category_name_s': category_name, + 'category_name': category_name, }) solr.add([document]) self.change_solr_data('Perubahan pada data product') @@ -371,14 +356,6 @@ class ProductProduct(models.Model): qty = sum(qty_onhand.mapped('quantity')) product.qty_onhand_bandengan = qty - # def write(self, vals): - # if 'solr_flag' not in vals: - # for variant in self: - # if variant.solr_flag == 1: - # variant.product_tmpl_id.solr_flag = 2 - # vals['solr_flag'] = 2 - # return super().write(vals) - def _compute_web_price(self): for product in self: product_pricelist_item = self.env['product.pricelist.item'].search( @@ -416,27 +393,22 @@ class ProductProduct(models.Model): 'search_rank', 'search_rank_weekly', 'image_1920' - ) + ) def _sync_variants_to_solr(self): variant = self if not variant.product_tmpl_id.active and variant.product_tmpl_id.type != 'product': return - 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_id = 0 category_name = '' for category in variant.product_tmpl_id.public_categ_ids: category_id = category.id category_name = category.name + break + solr_model = self.env['apache.solr'] solr = solr_model.connect('variants') - document = solr_model.get_single_doc(solr, variant.id) + document = solr_model.get_single_doc('variants', variant.id) document.update({ 'id': variant.id, 'display_name_s': variant.display_name, @@ -446,10 +418,6 @@ class ProductProduct(models.Model): '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, @@ -457,16 +425,11 @@ class ProductProduct(models.Model): '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 '', + 'category_id_i': category_id, + 'category_name_s': category_name, + 'category_name': category_name, '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, }) solr.add([document]) self.change_solr_data('Perubahan pada data product') \ No newline at end of file diff --git a/indoteknik_custom/models/stock_vendor.py b/indoteknik_custom/models/stock_vendor.py index f214a5e1..075b63d2 100755 --- a/indoteknik_custom/models/stock_vendor.py +++ b/indoteknik_custom/models/stock_vendor.py @@ -50,9 +50,3 @@ class StockVendor(models.Model): for template in templates: template.virtual_qty = template.qty_stock_vendor or 0 _logger.info("Update Stock Product Template %s : %s" % (template.id, template.virtual_qty)) - - # @api.onchange('quantity') - # def update_solr_flag(self): - # for stock in self: - # if stock.product_variant_id.product_tmpl_id.solr_flag == 1: - # stock.product_variant_id.product_tmpl_id.solr_flag = 2 diff --git a/indoteknik_custom/models/x_manufactures.py b/indoteknik_custom/models/x_manufactures.py index e48a5367..1d0870a5 100755 --- a/indoteknik_custom/models/x_manufactures.py +++ b/indoteknik_custom/models/x_manufactures.py @@ -72,13 +72,3 @@ class XManufactures(models.Model): 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') - def update_solr_flag(self): - for manufacture in self: - templates = self.env['product.template'].search([ - ('x_manufacture', '=', manufacture.id) - ]) - for template in templates: - if template.solr_flag == 1: - template.solr_flag = 2 diff --git a/indoteknik_custom/views/product_pricelist.xml b/indoteknik_custom/views/product_pricelist.xml index 34876cc4..55139a24 100644 --- a/indoteknik_custom/views/product_pricelist.xml +++ b/indoteknik_custom/views/product_pricelist.xml @@ -25,12 +25,4 @@ - - - Sync Product to SOLR - - - code - action = records.update_product_solr_flag() - \ No newline at end of file -- cgit v1.2.3 From 8424fbdfd66c6eca58d546c256d57a61e258e930 Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Thu, 24 Aug 2023 14:47:44 +0700 Subject: solr categories homepage --- .../models/website_categories_homepage.py | 32 ++++++++++++++++++++-- .../views/website_categories_homepage.xml | 8 ++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/indoteknik_custom/models/website_categories_homepage.py b/indoteknik_custom/models/website_categories_homepage.py index a0fc1011..2f5b3d92 100644 --- a/indoteknik_custom/models/website_categories_homepage.py +++ b/indoteknik_custom/models/website_categories_homepage.py @@ -1,4 +1,5 @@ -from odoo import fields, models +from odoo import fields, models, api +import json class WebsiteCategoriesHomepage(models.Model): @@ -13,4 +14,31 @@ class WebsiteCategoriesHomepage(models.Model): ('tayang', 'Tayang'), ('tidak_tayang', 'Tidak Tayang') ], string='Status') - product_ids = fields.Many2many('product.template', string='Product Template') + product_ids = fields.Many2many('product.template', string='Product category') + + + @api.constrains('category_id', 'image', 'url', 'sequence', 'status', 'product_ids') + def _sync_category_homepage_to_solr(self): + solr_model = self.env['apache.solr'] + solr = solr_model.connect('product_category_homepage') + + for category in self: + document = solr_model.get_single_doc(solr, category.id) + result = [self.env['product.template'].api_single_response(x) for x in category.product_ids] + products = json.dumps(result, indent=None) + + document.update({ + 'id': category.id, + 'category_id_i': category.category_id.id, + 'name_s': category.category_id.name, + 'image_s': self.env['ir.attachment'].api_image('website.categories.homepage', 'image', category.id), + 'sequence_i': category.sequence or '', + 'url_s': category.url or '', + 'products_s': products, + }) + solr.add([document]) + + def sync_to_solr(self): + categories_ids = self.env.context.get('active_ids', []) + categories = self.env['website.categories.homepage'].search([('id', 'in', categories_ids)]) + categories._sync_category_homepage_to_solr() diff --git a/indoteknik_custom/views/website_categories_homepage.xml b/indoteknik_custom/views/website_categories_homepage.xml index 0a7ef2c2..0a3f684b 100644 --- a/indoteknik_custom/views/website_categories_homepage.xml +++ b/indoteknik_custom/views/website_categories_homepage.xml @@ -49,6 +49,14 @@ + + Sync To Solr + + + code + model.sync_to_solr() + +