summaryrefslogtreecommitdiff
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
parente419a0e52ac9d01ac0b8900bfb2bebff1db522c9 (diff)
<Miqdad> sync to solr
-rw-r--r--indoteknik_custom/models/keywords.py66
-rw-r--r--indoteknik_custom/views/keywords.xml18
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 @@
<field name="keywords" />
<field name="url" />
<field name="sum" />
+ <field name="solr_flag" readonly="1"/>
<field name="product_ids" widget="many2many_tags" />
</tree>
</field>
@@ -36,6 +37,7 @@
<field name="sum" />
<field name="product_ids" widget="many2many_tags" />
<field name="skip" />
+ <field name="solr_flag" readonly="1"/>
</group>
</sheet>
</form>
@@ -51,6 +53,7 @@
<field name="category_id"/>
<field name="keywords"/>
<field name="sum"/>
+ <field name="solr_flag" readonly="1"/>
<field name="product_ids" widget="many2many_tags"/>
</search>
</field>
@@ -75,4 +78,19 @@
parent="website_sale.menu_orders"
action="action_keywords"
sequence="100"/>
+
+ <data noupdate="1">
+ <record id="cron_keywords_solr_flag_queue" model="ir.cron">
+ <field name="name">Sync Keywords To Solr: Queue Solr Flag 2</field>
+ <field name="interval_number">1</field>
+ <field name="interval_type">hours</field>
+ <field name="numbercall">-1</field>
+ <field name="doall" eval="False"/>
+ <field name="model_id" ref="model_keywords"/>
+ <field name="code">model.solr_flag_to_queue(limit=500)</field>
+ <field name="state">code</field>
+ <field name="priority">55</field>
+ <field name="active">False</field>
+ </record>
+ </data>
</odoo>