summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-08-24 09:17:29 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-08-24 09:17:29 +0700
commitb173abf78bd52ba0cd10829e1a2b31efc371f3cf (patch)
treedb2318154308ab8d65a7b8e403c3a457f4151599
parentb8356524ab2d381affe438a85e1662a08fcd3026 (diff)
Refactor update solr sync
-rw-r--r--indoteknik_custom/models/apache_solr.py4
-rw-r--r--indoteknik_custom/models/product_pricelist.py27
-rwxr-xr-xindoteknik_custom/models/product_template.py101
-rwxr-xr-xindoteknik_custom/models/stock_vendor.py6
-rwxr-xr-xindoteknik_custom/models/x_manufactures.py10
-rw-r--r--indoteknik_custom/views/product_pricelist.xml8
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 @@
</page>
</field>
</record>
-
- <record id="product_pricelist_sync_product_solr_ir_actions_server" model="ir.actions.server">
- <field name="name">Sync Product to SOLR</field>
- <field name="model_id" ref="product.model_product_pricelist"/>
- <field name="binding_model_id" ref="product.model_product_pricelist"/>
- <field name="state">code</field>
- <field name="code">action = records.update_product_solr_flag()</field>
- </record>
</odoo> \ No newline at end of file