summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/solr/promotion_program.py
blob: 014e8062e3d4a21e3e5207ea97909c1df6a44a00 (plain)
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
from odoo import models, api
from datetime import datetime
from pytz import timezone
from typing import Type
import pysolr


class PromotionProgram(models.Model):
    _inherit = 'promotion.program'
    _solr_schema = 'promotion_programs'

    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):
        ir_attachment = self.env['ir.attachment']
        solr_model = self.env['apache.solr']

        for rec in self:
            document = solr_model.get_doc(self._solr_schema, rec.id)
            document.update({
                'id': rec.id,
                'name_s': rec.name,
                'banner_s': ir_attachment.api_image(self._name, 'banner', rec.id) if rec.banner else '',
                'image_s': ir_attachment.api_image(self._name, 'image', rec.id) if rec.image else '',
                'keywords': [x.name for x in rec.keyword_ids],
                'line_ids': [x.id for x in rec.program_line],
                'start_time_s': self._time_format(rec.start_time),
                'end_time_s': self._time_format(rec.end_time),
                'applies_to_s': rec.applies_to,
                'icon_s': ir_attachment.api_image(self._name, 'icon', rec.id) if rec.icon else '',
                'icon_top_s': ir_attachment.api_image(self._name, 'icon_top', rec.id) if rec.icon_top else '',
                'icon_bottom_s': ir_attachment.api_image(self._name, 'icon_bottom', rec.id) if rec.icon_bottom else '',
            })

            self.solr().add([document])

        self.solr().commit()

    def _time_format(self, object) -> str:
        time = ''
        tz_jakarta = timezone('Asia/Jakarta')
        if isinstance(object, datetime):
            time = object.astimezone(tz_jakarta).strftime("%Y-%m-%d %H:%M:%S")
        return time

    @api.model
    def create(self, vals):
        self._create_solr_queue('_sync_to_solr')
        return super(PromotionProgram, self).create(vals)
    
    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')
                
    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')