From 35e308b24e8a9ae6c158d65c68d4a30e0be40cba Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Tue, 10 Oct 2023 09:27:49 +0700 Subject: Add sync to solr on model - promotion program - promotion program line --- .../models/solr/promotion_program_line.py | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 indoteknik_custom/models/solr/promotion_program_line.py (limited to 'indoteknik_custom/models/solr/promotion_program_line.py') diff --git a/indoteknik_custom/models/solr/promotion_program_line.py b/indoteknik_custom/models/solr/promotion_program_line.py new file mode 100644 index 00000000..581783d1 --- /dev/null +++ b/indoteknik_custom/models/solr/promotion_program_line.py @@ -0,0 +1,62 @@ +from odoo import models, api +from typing import Type +import pysolr, json + + +class PromotionProgramLine(models.Model): + _inherit = 'promotion.program.line' + _solr_schema = 'promotion_program_lines' + + def solr(self) -> Type[pysolr.Solr]: + return self.env['apache.solr'].connect(self._solr_schema) + + def _create_solr_queue(self, function_name: str): + for rec in self: + self.env['apache.solr.queue'].create_unique({ + 'res_model': self._name, + 'res_id': rec.id, + 'function_name': function_name + }) + + def _sync_to_solr(self): + solr_model = self.env['apache.solr'] + + for rec in self: + document = solr_model.get_doc(self._solr_schema, rec.id) + + products = [{ + 'product_id': x.product_id.id, + 'qty': x.qty + } for x in rec.product_ids] + + free_products = [{ + 'product_id': x.product_id.id, + 'qty': x.qty + } for x in rec.free_product_ids] + + document.update({ + 'id': rec.id, + 'program_id': rec.program_id.id, + 'name_s': rec.name, + 'image_s': self.env['ir.attachment'].api_image(self._name, 'image', rec.id) if rec.image else '', + 'type_s': json.dumps(rec._res_promotion_type()), + 'package_limit_i': rec.package_limit, + 'package_limit_user_i': rec.package_limit_user, + 'package_limit_trx_i': rec.package_limit_trx, + 'price_f': rec.price, + 'products_s': json.dumps(products), + 'free_products_s': json.dumps(free_products) + }) + + self.solr().add([document]) + + self.solr().commit() + + @api.model + def create(self, vals): + self._create_solr_queue('_sync_to_solr') + return super(PromotionProgramLine, self).create(vals) + + def write(self, vals): + self._create_solr_queue('_sync_to_solr') + return super(PromotionProgramLine, self).write(vals) \ No newline at end of file -- cgit v1.2.3 From ce3f0c1b8f9fee54caf2047190151b6caf80664c Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Tue, 10 Oct 2023 09:53:01 +0700 Subject: Update program line response --- indoteknik_custom/models/solr/promotion_program_line.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'indoteknik_custom/models/solr/promotion_program_line.py') diff --git a/indoteknik_custom/models/solr/promotion_program_line.py b/indoteknik_custom/models/solr/promotion_program_line.py index 581783d1..e5f22e74 100644 --- a/indoteknik_custom/models/solr/promotion_program_line.py +++ b/indoteknik_custom/models/solr/promotion_program_line.py @@ -36,7 +36,7 @@ class PromotionProgramLine(models.Model): document.update({ 'id': rec.id, - 'program_id': rec.program_id.id, + 'program_id_i': rec.program_id.id, 'name_s': rec.name, 'image_s': self.env['ir.attachment'].api_image(self._name, 'image', rec.id) if rec.image else '', 'type_s': json.dumps(rec._res_promotion_type()), @@ -44,7 +44,9 @@ class PromotionProgramLine(models.Model): 'package_limit_user_i': rec.package_limit_user, 'package_limit_trx_i': rec.package_limit_trx, 'price_f': rec.price, + 'product_ids': [x.product_id.id for x in rec.product_ids], 'products_s': json.dumps(products), + 'free_product_ids': [x.product_id.id for x in rec.free_product_ids], 'free_products_s': json.dumps(free_products) }) -- cgit v1.2.3 From 6a87e59e7220bdfa78e98b23003ccc4ef41bd0ce Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Tue, 10 Oct 2023 10:42:30 +0700 Subject: Add program and program line constrains --- .../models/solr/promotion_program_line.py | 24 ++++++++++++---------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'indoteknik_custom/models/solr/promotion_program_line.py') diff --git a/indoteknik_custom/models/solr/promotion_program_line.py b/indoteknik_custom/models/solr/promotion_program_line.py index e5f22e74..b0aeaa13 100644 --- a/indoteknik_custom/models/solr/promotion_program_line.py +++ b/indoteknik_custom/models/solr/promotion_program_line.py @@ -1,15 +1,16 @@ from odoo import models, api from typing import Type -import pysolr, json +import pysolr +import json class PromotionProgramLine(models.Model): _inherit = 'promotion.program.line' _solr_schema = 'promotion_program_lines' - + def solr(self) -> Type[pysolr.Solr]: return self.env['apache.solr'].connect(self._solr_schema) - + def _create_solr_queue(self, function_name: str): for rec in self: self.env['apache.solr.queue'].create_unique({ @@ -17,23 +18,23 @@ class PromotionProgramLine(models.Model): 'res_id': rec.id, 'function_name': function_name }) - + def _sync_to_solr(self): solr_model = self.env['apache.solr'] for rec in self: document = solr_model.get_doc(self._solr_schema, rec.id) - + products = [{ 'product_id': x.product_id.id, 'qty': x.qty } for x in rec.product_ids] - + free_products = [{ 'product_id': x.product_id.id, 'qty': x.qty } for x in rec.free_product_ids] - + document.update({ 'id': rec.id, 'program_id_i': rec.program_id.id, @@ -47,18 +48,19 @@ class PromotionProgramLine(models.Model): 'product_ids': [x.product_id.id for x in rec.product_ids], 'products_s': json.dumps(products), 'free_product_ids': [x.product_id.id for x in rec.free_product_ids], - 'free_products_s': json.dumps(free_products) + 'free_products_s': json.dumps(free_products), + 'total_qty_i': sum([x.qty for x in rec.product_ids] + [x.qty for x in rec.free_product_ids]), }) self.solr().add([document]) self.solr().commit() - + @api.model def create(self, vals): self._create_solr_queue('_sync_to_solr') return super(PromotionProgramLine, self).create(vals) - + def write(self, vals): self._create_solr_queue('_sync_to_solr') - return super(PromotionProgramLine, self).write(vals) \ No newline at end of file + return super(PromotionProgramLine, self).write(vals) -- cgit v1.2.3 From c1f7742c254898fa5981e8ca7d870ac42d3f8346 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 8 Dec 2023 14:58:52 +0700 Subject: Update document payload for promotion program lines SOLR --- indoteknik_custom/models/solr/promotion_program_line.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'indoteknik_custom/models/solr/promotion_program_line.py') diff --git a/indoteknik_custom/models/solr/promotion_program_line.py b/indoteknik_custom/models/solr/promotion_program_line.py index b0aeaa13..6e182324 100644 --- a/indoteknik_custom/models/solr/promotion_program_line.py +++ b/indoteknik_custom/models/solr/promotion_program_line.py @@ -35,12 +35,15 @@ class PromotionProgramLine(models.Model): 'qty': x.qty } for x in rec.free_product_ids] + promotion_type = rec._res_promotion_type() + document.update({ 'id': rec.id, 'program_id_i': rec.program_id.id, 'name_s': rec.name, 'image_s': self.env['ir.attachment'].api_image(self._name, 'image', rec.id) if rec.image else '', - 'type_s': json.dumps(rec._res_promotion_type()), + 'type_value_s': promotion_type['value'], + 'type_label_s': promotion_type['label'], 'package_limit_i': rec.package_limit, 'package_limit_user_i': rec.package_limit_user, 'package_limit_trx_i': rec.package_limit_trx, -- cgit v1.2.3