summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models
diff options
context:
space:
mode:
authorMqdd <ahmadmiqdad27@gmail.com>2026-02-19 15:44:17 +0700
committerMqdd <ahmadmiqdad27@gmail.com>2026-02-19 15:44:17 +0700
commitd098ff60225cd6d7770a08bdd1c06aafdd74f1d9 (patch)
treed42cd00d7e2347ea51487797e5eaf361f966e603 /indoteknik_custom/models
parente419a0e52ac9d01ac0b8900bfb2bebff1db522c9 (diff)
<Miqdad> sync to solr
Diffstat (limited to 'indoteknik_custom/models')
-rw-r--r--indoteknik_custom/models/keywords.py66
1 files changed, 63 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