1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
from odoo import models, api
from typing import Type
import pysolr
import json
import logging
from odoo.exceptions import UserError
_logger = logging.getLogger(__name__)
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:
try:
document = solr_model.get_doc(self._solr_schema, rec.id)
products = [{
'product_id': x.product_id.id,
'qty': x.qty,
'qty_sold': x.product_id.qty_sold
} for x in rec.product_ids]
free_products = [{
'product_id': x.product_id.id,
'qty': x.qty
} for x in rec.free_product_ids]
promotion_type = rec._res_promotion_type()
category_names = [category.name for category in rec.product_ids.product_id.public_categ_ids]
sequence_value = None if rec.sequence == 0 else rec.sequence
document.update({
'id': rec.id,
'program_id_i': rec.program_id.id or 0,
'name_s': rec.name,
'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,
'price_f': rec.price,
'price_tier_1_f': rec.price_tier_1,
'price_tier_2_f': rec.price_tier_2,
'price_tier_3_f': rec.price_tier_3,
'price_tier_4_f': rec.price_tier_4,
'price_tier_5_f': rec.price_tier_5,
'sequence_i': sequence_value,
'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),
'total_qty_i': sum([x.qty for x in rec.product_ids] + [x.qty for x in rec.free_product_ids]),
'total_qty_sold_f': sum([x.product_id.qty_sold for x in rec.product_ids]),
'active_b': rec.active,
"manufacture_name_s": rec.product_ids[0].product_id.x_manufacture.x_name or '',
"category_name": category_names,
})
self.solr().add([document])
self.solr().commit()
except Exception as e:
_logger.error(
"Failed to sync record %s (ID: %s) to Solr. Error: %s",
rec._name, rec.id, str(e),
exc_info=True # biar stack trace keluar
)
# opsional -> kalau mau hard fail:
raise UserError(_("Sync to Solr failed for record %s: %s") % (rec.name, str(e)))
@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)
def solr_flag_to_queue(self, limit=500):
domain = [
('solr_flag', '=', 2),
('active', 'in', [True, False])
]
records = self.search(domain, limit=limit)
for record in records:
record._create_solr_queue('_sync_to_solr')
record.solr_flag = 1
def action_sync_to_solr(self):
rec_ids = self.env.context.get('active_ids', [])
recs = self.search([('id', 'in', rec_ids)])
recs._create_solr_queue('_sync_to_solr')
|