summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/keywords.py
blob: 38b6e2fe9d7c6c9bcee81927d99fdbcc616bb3ff (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
from itertools import product
from odoo import fields, models, api, tools, _
import logging
import re
import pysolr
from odoo.exceptions import UserError
import base64
import xlrd, xlwt
import io


_logger = logging.getLogger(__name__)

class Keywords(models.Model):
    _name = 'keywords'
    _order= 'id desc'

    category_id = fields.Many2one('product.public.category', string='Category')
    keywords = fields.Char('Keywords')
    product_ids = fields.One2many('product.product', 'keyword_id', string='Products')

    @api.constrains('product_ids', 'keywords', 'category_id')
    def action_generate_products(self):
        for record in self:
            if not record.keywords:
                continue

            domain = [
                      ('name', 'ilike', record.keywords),
                      ('product_rating', '>=', 8),
                      ('unpublish', '=', False)
                      ]

            # if record.category_id:
            #     domain += [(record.product_ids.id, 'in', record.category_id.id)]

            matched_products = self.env['product.product'].search(domain)

            record.product_ids = [(6, 0, matched_products.ids)]

            _logger.info('Generated %s products for keyword "%s"', len(matched_products), record.keywords)

    @api.model
    def create(self, vals):
        record = super().create(vals)
        record.action_generate_products()
        return record

    def write(self, vals):
        result = super().write(vals)
        self.action_generate_products()
        return result