summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-08-26 12:13:28 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-08-26 12:13:28 +0700
commit315b832420eb8314e809b1c0f549304d423b45f3 (patch)
treebf31b1f24ffa511308bec829b7e57ff09eabd294 /indoteknik_custom/models
parent52f4b12c0b211f958c2704346d7da24d1d4767c6 (diff)
Create apache solr queue and implement to product.template and website.categories.homepage
Diffstat (limited to 'indoteknik_custom/models')
-rw-r--r--indoteknik_custom/models/solr/__init__.py1
-rw-r--r--indoteknik_custom/models/solr/apache_solr_queue.py69
-rw-r--r--indoteknik_custom/models/solr/product_product.py26
-rw-r--r--indoteknik_custom/models/solr/product_template.py48
-rw-r--r--indoteknik_custom/models/solr/website_categories_homepage.py52
5 files changed, 147 insertions, 49 deletions
diff --git a/indoteknik_custom/models/solr/__init__.py b/indoteknik_custom/models/solr/__init__.py
index 450e1dae..45ef4299 100644
--- a/indoteknik_custom/models/solr/__init__.py
+++ b/indoteknik_custom/models/solr/__init__.py
@@ -1,4 +1,5 @@
from . import apache_solr
+from . import apache_solr_queue
from . import product_pricelist_item
from . import product_product
from . import product_template
diff --git a/indoteknik_custom/models/solr/apache_solr_queue.py b/indoteknik_custom/models/solr/apache_solr_queue.py
new file mode 100644
index 00000000..eb5a99a8
--- /dev/null
+++ b/indoteknik_custom/models/solr/apache_solr_queue.py
@@ -0,0 +1,69 @@
+from odoo import models, fields
+from datetime import datetime
+import logging
+
+
+_logger = logging.getLogger(__name__)
+
+class ApacheSolrQueue(models.Model):
+ _name = 'apache.solr.queue'
+
+ display_name = fields.Char('Display Name', compute="_compute_display_name")
+ res_model = fields.Char('Resource Model')
+ res_id = fields.Integer('Resource ID')
+ function_name = fields.Char('Function Name')
+ execute_status = fields.Selection([
+ ('success', 'Success'),
+ ('failed', 'Failed'),
+ ('not_found', 'Record not found')
+ ], 'Execute Status')
+ execute_date = fields.Datetime('Execute Date')
+
+ def _compute_display_name(self):
+ for rec in self:
+ try:
+ res_model = rec.res_model
+ res_id = int(rec.res_id)
+ model_instance = self.env[res_model].browse(res_id)
+ rec.display_name = model_instance.display_name or ''
+ except:
+ rec.display_name = ''
+
+ def process_queue_item(self, limit=100):
+ records = self.search([('execute_status', '=', False)], order='create_date asc', limit=limit)
+ for rec in records:
+ rec.execute_queue()
+
+ def execute_queue(self):
+ for rec in self:
+ try:
+ res_model = rec.res_model
+ res_id = int(rec.res_id)
+ function_name = rec.function_name
+ _logger.info(f'Execute Queue: {res_model}.{function_name}() -> {res_id}')
+
+ domain = [('id', '=', res_id)]
+ if res_model in ['product.template']:
+ domain.append(('active', 'in', [True, False]))
+
+ model_instance = self.env[res_model].search(domain)
+ if model_instance:
+ getattr(model_instance, function_name)()
+ rec.execute_status = 'success'
+ else:
+ rec.execute_status = 'not_found'
+ except:
+ rec.execute_status = 'failed'
+ rec.execute_date = datetime.utcnow()
+ self.env.cr.commit()
+
+ def create_unique(self, payload={}):
+ count = self.search_count([
+ ('res_model', '=', payload['res_model']),
+ ('res_id', '=', payload['res_id']),
+ ('function_name', '=', payload['function_name']),
+ ('execute_status', '=', False)
+ ])
+ if count == 0:
+ self.create(payload)
+
diff --git a/indoteknik_custom/models/solr/product_product.py b/indoteknik_custom/models/solr/product_product.py
index 5090b8d5..f3107afa 100644
--- a/indoteknik_custom/models/solr/product_product.py
+++ b/indoteknik_custom/models/solr/product_product.py
@@ -14,6 +14,12 @@ class ProductProduct(models.Model):
def solr(self):
return self.env['apache.solr'].connect('variants')
+
+ def action_sync_to_solr(self):
+ product_ids = self.env.context.get('active_ids', [])
+ products = self.search([('id', 'in', product_ids)])
+ for product in products:
+ product.product_tmpl_id._create_solr_queue('_sync_product_template_to_solr')
def _sync_variants_to_solr(self):
solr_model = self.env['apache.solr']
@@ -59,8 +65,6 @@ class ProductProduct(models.Model):
if not document.get('has_price_info_b'):
variant._sync_price_to_solr()
- self.solr().commit()
-
def _sync_price_to_solr(self):
solr_model = self.env['apache.solr']
@@ -105,17 +109,7 @@ class ProductProduct(models.Model):
if not document.get('has_product_info_b'):
variant._sync_variants_to_solr()
-
- self.solr().commit()
-
- def sync_to_solr(self):
- product_ids = self.env.context.get('active_ids', [])
- products = self.search([('id', 'in', product_ids)])
-
- updated_template_ids = []
- for product in products:
- template = product.product_tmpl_id
- if template.id in updated_template_ids:
- continue
- template._sync_product_template_to_solr()
- updated_template_ids.append(template.id) \ No newline at end of file
+
+ def _sync_delete_solr(self):
+ for rec in self:
+ self.solr().delete(rec.id) \ No newline at end of file
diff --git a/indoteknik_custom/models/solr/product_template.py b/indoteknik_custom/models/solr/product_template.py
index 6089adda..ba670a81 100644
--- a/indoteknik_custom/models/solr/product_template.py
+++ b/indoteknik_custom/models/solr/product_template.py
@@ -15,16 +15,31 @@ class ProductTemplate(models.Model):
def solr(self):
return self.env['apache.solr'].connect('product')
+ def _create_solr_queue(self, function_name):
+ for rec in self:
+ self.env['apache.solr.queue'].create_unique({
+ 'res_model': self._name,
+ 'res_id': rec.id,
+ 'function_name': function_name
+ })
+
@api.constrains('active')
+ def _create_solr_queue_sync_active(self):
+ self._create_solr_queue('_sync_active_template_solr')
+
+ @api.constrains('name', 'default_code', 'weight', 'x_manufacture', 'public_categ_ids', 'search_rank', 'search_rank_weekly', 'image_1920')
+ def _create_solr_queue_sync_product_template(self):
+ self._create_solr_queue('_sync_product_template_to_solr')
+
+ def action_sync_to_solr(self):
+ template_ids = self.env.context.get('active_ids', [])
+ templates = self.search([('id', 'in', template_ids)])
+ templates._create_solr_queue('_sync_product_template_to_solr')
+
def _sync_active_template_solr(self):
for template in self:
if not template.active or template.type != 'product':
- self.solr().delete(template.id)
-
- variant_ids = [x.id for x in template.product_variant_ids]
- template.product_variant_ids.solr().delete(variant_ids)
-
- self.solr().commit()
+ template._sync_delete_solr()
else:
template._sync_product_template_to_solr()
template._sync_price_to_solr()
@@ -32,16 +47,6 @@ class ProductTemplate(models.Model):
products = self.env['product.product'].search([('product_tmpl_id', '=', template.id), ('active', 'in', [True, False])])
products._sync_variants_to_solr()
- @api.constrains(
- 'name',
- 'default_code',
- 'weight',
- 'x_manufacture',
- 'public_categ_ids',
- 'search_rank',
- 'search_rank_weekly',
- 'image_1920'
- )
def _sync_product_template_to_solr(self):
solr_model = self.env['apache.solr']
@@ -152,7 +157,10 @@ class ProductTemplate(models.Model):
self.solr().commit()
- def sync_to_solr(self):
- template_ids = self.env.context.get('active_ids', [])
- templates = self.search([('id', 'in', template_ids)])
- templates._sync_product_template_to_solr() \ No newline at end of file
+ def _sync_delete_solr(self):
+ for rec in self:
+ self.solr().delete(rec.id)
+ for variant in rec.product_variant_ids:
+ variant._sync_delete_solr()
+ self.solr().optimize()
+ self.solr().commit() \ No newline at end of file
diff --git a/indoteknik_custom/models/solr/website_categories_homepage.py b/indoteknik_custom/models/solr/website_categories_homepage.py
index 59e7f32f..21812acf 100644
--- a/indoteknik_custom/models/solr/website_categories_homepage.py
+++ b/indoteknik_custom/models/solr/website_categories_homepage.py
@@ -14,20 +14,51 @@ class WebsiteCategoriesHomepage(models.Model):
def update_last_update_solr(self):
self.last_update_solr = datetime.utcnow()
+ def _create_solr_queue(self, function_name):
+ for rec in self:
+ self.env['apache.solr.queue'].create_unique({
+ 'res_model': self._name,
+ 'res_id': rec.id,
+ 'function_name': function_name
+ })
+
@api.constrains('status')
+ def _create_solr_queue_sync_status(self):
+ self._create_solr_queue('_sync_status_category_homepage_solr')
+
+ @api.constrains('category_id', 'image', 'url', 'sequence', 'product_ids')
+ def _create_solr_queue_sync_category_homepage(self):
+ self._create_solr_queue('_sync_category_homepage_to_solr')
+
+ def action_sync_to_solr(self):
+ category_ids = self.env.context.get('active_ids', [])
+ categories = self.search([('id', 'in', category_ids)])
+ categories._create_solr_queue('_sync_category_homepage_to_solr')
+
+ def unlink(self):
+ res = super(WebsiteCategoriesHomepage, self).unlink()
+ for rec in self:
+ self.env['apache.solr.queue'].create_unique({
+ 'res_model': self._name,
+ 'res_id': rec.id,
+ 'function_name': '_sync_delete_solr'
+ })
+ return res
+
def _sync_status_category_homepage_solr(self):
for rec in self:
if rec.status == 'tayang':
rec._sync_category_homepage_to_solr()
else:
- self.solr().delete(rec.id)
- self.solr().commit()
+ rec._sync_delete_solr()
- @api.constrains('category_id', 'image', 'url', 'sequence', 'product_ids')
def _sync_category_homepage_to_solr(self):
solr_model = self.env['apache.solr']
for category in self:
+ if category.status == 'tidak_tayang':
+ continue
+
document = solr_model.get_single_doc('product_category_homepage', category.id)
products = [self.env['product.template'].api_single_response(x) for x in category.product_ids]
@@ -45,13 +76,8 @@ class WebsiteCategoriesHomepage(models.Model):
self.solr().commit()
- def sync_to_solr(self):
- category_ids = self.env.context.get('active_ids', [])
- categories = self.search([('id', 'in', category_ids)])
- categories._sync_category_homepage_to_solr()
-
- def unlink(self):
- res = super(WebsiteCategoriesHomepage, self).unlink()
- self.solr().delete([x.id for x in self])
- self.solr().commit()
- return res \ No newline at end of file
+ def _sync_delete_solr(self):
+ for rec in self:
+ self.solr().delete(rec.id)
+ self.solr().optimize()
+ self.solr().commit() \ No newline at end of file