diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-10-10 10:42:30 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-10-10 10:42:30 +0700 |
| commit | 6a87e59e7220bdfa78e98b23003ccc4ef41bd0ce (patch) | |
| tree | 543abc822be6355f04dd306d9b4bd6ffdc63a4fd | |
| parent | a4a1461c3b6603c1a4943935ed45f1a3ea9b80ed (diff) | |
Add program and program line constrains
| -rw-r--r-- | indoteknik_custom/models/solr/promotion_program.py | 7 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/promotion_program_line.py | 24 |
2 files changed, 20 insertions, 11 deletions
diff --git a/indoteknik_custom/models/solr/promotion_program.py b/indoteknik_custom/models/solr/promotion_program.py index c288c165..0d417b3e 100644 --- a/indoteknik_custom/models/solr/promotion_program.py +++ b/indoteknik_custom/models/solr/promotion_program.py @@ -59,3 +59,10 @@ class PromotionProgram(models.Model): def write(self, vals): self._create_solr_queue('_sync_to_solr') return super(PromotionProgram, self).write(vals) + + @api.constrains('program_line') + def constrains_program_line(self): + for rec in self: + for line in rec.program_line: + line._create_solr_queue('_sync_to_solr') + 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) |
