summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/find_page.py
blob: c31b8334f65c677c6057ab2327c2e01dfd26b8a1 (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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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__)
# _cat_brand_solr = pysolr.Solr('http://10.148.0.5:8983/solr/url_category_brand/', always_commit=True, timeout=30)
_cat_brand_solr_dev = pysolr.Solr('http://127.0.0.1:8983/solr/url_category_brand/', always_commit=True, timeout=30)


class BrandProductCategory(models.Model):
    _name = 'v.brand.product.category'
    _auto = False
    _rec_name = 'brand_id'
    brand_id = fields.Many2one('x_manufactures', string='Brand')
    category_id = fields.Many2one('product.public.category', string='Category')

    def init(self):
        tools.drop_view_if_exists(self.env.cr, self._table)
        self.env.cr.execute("""
            CREATE OR REPLACE VIEW %s
            AS select row_number() over(order by pt.x_manufacture) as id,
            pt.x_manufacture as brand_id,
            ppcptr.product_public_category_id as category_id
            from product_template pt
            join product_public_category_product_template_rel ppcptr on ppcptr.product_template_id = pt.id
            join x_manufactures xm on xm.id = pt.x_manufacture
            group by x_manufacture, ppcptr.product_public_category_id
        """ % self._table)


class FindPage(models.Model):
    _name = 'web.find.page'
    _inherit = ['mail.thread']
    _rec_name = 'url'

    keywords = fields.Char('Keywords')
    brand_id = fields.Many2one('x_manufactures', string='Brand')
    category_id = fields.Many2one('product.public.category', string='Category', help='Bisa semua level Category')
    url = fields.Char(string='Url')

    def _sync_to_solr(self, limit=10000):
        urls = self.env['web.find.page'].search([])
        documents = []
        catch = {}
        for url in urls:
            try:
                document = {
                    'id': url.id,
                    'category_id_i': url.category_id.id,
                    'brand_id_i': url.brand_id.id,
                    'url_s': url.url
                }
                if url.keywords:
                    document['keywords_s']= url.keywords
                documents.append(document)
                catch = document
            except Exception as e:
                _logger.error('Failed to add document to Solr URL Category Brand: %s', e)
                _logger.error('Document Data: %s', catch)
        _cat_brand_solr_dev.add(documents)
        return True

    def _get_category_hierarchy(self, category):
        categories = []
        current_category = category
        while current_category:
            categories.insert(0, current_category)
            current_category = current_category.parent_id
        return categories

    def _generate_url(self):
        keyword = self.keywords

        list_url = []
        if not keyword:
            categories = self.env['v.brand.product.category'].search([])

            for category in categories:
                category_hierarchy = self._get_category_hierarchy(category.category_id)

                for level, cat in enumerate(reversed(category_hierarchy), start=1):
                    list_url.append(
                        self._generate_mod_url(cat, category.brand_id)  # keywords=None otomatis
                    )
        else:
            brands = self.env['x_manufactures'].search([('x_name', 'ilike', keyword)])
            categories = self.env['product.public.category'].search([('name', 'ilike', keyword)])

            for brand in brands:
                for category in categories:
                    list_url.append(
                        self._generate_mod_url(category, brand, keywords=keyword)
                    )
        unique_list = []
        for item in list_url:
            if item not in unique_list:
                unique_list.append(item)

        count = 0
        for item in unique_list:
            self._create_find_page(
                item['url'],
                item['category_id'],
                item['brand_id'],
                item.get('keywords')
            )
            count += 1
        print(f"Total categories processed: {count}")

    def _create_find_page(self, url, category_id, brand_id, keywords=None):
        param = {
            'url': url,
            'category_id': category_id,
            'brand_id': brand_id,
        }
        if keywords:
            param['keywords'] = keywords
        find_page = self.env['web.find.page'].create(param)
        _logger.info('Created Web Find Page %s' % find_page.id)

    def _generate_mod_url(self, category, brand, keywords=None):
        # generate_url = 'https://indoteknik.com/shop/find/category-brand' example
        cleaned_category = re.sub(r'[^\w\s]', '', category.name)
        cleaned_brand = re.sub(r'[^\w\s]', '', brand.x_name)
        cleaned_combined = cleaned_category+' '+cleaned_brand
        cleaned_combined = cleaned_combined.replace(' ', '-')
        cleaned_combined = cleaned_combined.replace('--', '-')
        if keywords:
            clean_keyword = re.sub(r'\s+', '-', keywords.strip().lower())
            # url = f"https://indoteknik.com/shop/find/key={clean_keyword}"
            url = f"http://localhost:2100/shop/find/key={clean_keyword}"
        else:
            url = 'http://localhost:2100/shop/find/'+cleaned_combined
            # url = 'https://indoteknik.com/shop/find/'+cleaned_combined
        url = url.lower()
        result = {
            'url': url,
            'category_id': category.id,
            'brand_id': brand.id
        }
        if keywords:
            result['keywords'] = keywords
        # print(url)
        # param = {
        #     'brand_id': brand.id,
        #     'category_id': category.id,
        #     'url':''
        # }
        # self.env['web.find.page'].create()
        # print(1)
        return result