From d098ff60225cd6d7770a08bdd1c06aafdd74f1d9 Mon Sep 17 00:00:00 2001 From: Mqdd Date: Thu, 19 Feb 2026 15:44:17 +0700 Subject: sync to solr --- indoteknik_custom/models/keywords.py | 66 ++++++++++++++++++++++++++++++++++-- indoteknik_custom/views/keywords.xml | 18 ++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/indoteknik_custom/models/keywords.py b/indoteknik_custom/models/keywords.py index 20a18726..ca5fc8d0 100644 --- a/indoteknik_custom/models/keywords.py +++ b/indoteknik_custom/models/keywords.py @@ -31,6 +31,7 @@ class Keywords(models.Model): skip = fields.Boolean('Skip Generate Product', default=False, help="If checked, the system will not generate products for this keyword") url = fields.Char('Website URL', compute="_compute_url", help="Generated website url based on keywords") sum = fields.Integer('Total Product', compute="_compute_total_product", readonly=True, help="Total products found for this keyword including variants") + solr_flag = fields.Integer(string='Solr Flag', default=0, help="0=no sync needed, 2=needs sync, 1=queued") @api.depends('product_ids') def _compute_total_product(self): @@ -128,7 +129,62 @@ class Keywords(models.Model): record.keywords ) + @api.onchange('keywords', 'category_id', 'product_ids') + def _onchange_solr_flag(self): + """Set solr_flag=2 when tracked fields change to trigger queue sync""" + for record in self: + if not record.skip: + record.solr_flag = 2 + + def solr_flag_to_queue(self, limit=500): + """Find keywords with solr_flag=2 and create apache.solr.queue entries""" + keywords = self.search([('solr_flag', '=', 2)], limit=limit) + + for keyword in keywords: + # Create unique queue entry + queue_obj = self.env['apache.solr.queue'] + queue_obj.create_unique({ + 'res_model': 'keywords', + 'res_id': keyword.id, + 'function_name': '_sync_keywords_queue_callback' + }) + + # Set flag to indicate queued + keyword.solr_flag = 1 + + if keywords: + _logger.info( + 'Queued %s keywords for Solr synchronization', + len(keywords) + ) + + return True + + def _sync_keywords_queue_callback(self): + """Callback method executed by apache.solr.queue - syncs keyword data to Solr""" + documents = [] + for keyword in self: + searchkey = (keyword.keywords or '').strip().lower().replace(' ', '-') + try: + doc = { + 'id': keyword.id, + 'category_id_i': keyword.category_id.id, + 'keywords_s': searchkey, + 'url_s': keyword.url, + 'product_ids_is': [p.product_tmpl_id.id for p in keyword.product_ids], + } + documents.append(doc) + except Exception as e: + _logger.error('failed %s', e) + _logger.error('doc data: %s', doc) + + if documents: + solr.add(documents) + + return True + def sync_solr(self): + """Manual sync method for active_ids context (backward compatibility)""" active_ids = self.env.context.get('active_ids', []) if not active_ids: _logger.warning("No active_ids found, nothing to sync") @@ -166,7 +222,11 @@ class Keywords(models.Model): def write(self, vals): result = super().write(vals) - # self.check_already_exist() - # if not self.env.context.get("skip_generate") and not self.skip: - # self.generate_products() + + tracked_fields = ['keywords', 'category_id', 'product_ids'] + neded_sync = any(field in vals for field in tracked_fields) + if neded_sync and self.skip == False: + for record in self: + record.solr_flag = 2 + return result diff --git a/indoteknik_custom/views/keywords.xml b/indoteknik_custom/views/keywords.xml index 9faa7112..a33edae0 100644 --- a/indoteknik_custom/views/keywords.xml +++ b/indoteknik_custom/views/keywords.xml @@ -9,6 +9,7 @@ + @@ -36,6 +37,7 @@ + @@ -51,6 +53,7 @@ + @@ -75,4 +78,19 @@ parent="website_sale.menu_orders" action="action_keywords" sequence="100"/> + + + + Sync Keywords To Solr: Queue Solr Flag 2 + 1 + hours + -1 + + + model.solr_flag_to_queue(limit=500) + code + 55 + False + + -- cgit v1.2.3