diff options
| author | Mqdd <ahmadmiqdad27@gmail.com> | 2025-12-02 10:31:41 +0700 |
|---|---|---|
| committer | Mqdd <ahmadmiqdad27@gmail.com> | 2025-12-02 10:31:41 +0700 |
| commit | 0db7816b9208f9ef72bb4bb02e1d550cffb884ee (patch) | |
| tree | 88ae96a2c09a6d6e06684230dcb57d2966b60330 | |
| parent | c03a690a935508c2c669a5e7ff6da3c50f42fb53 (diff) | |
<Miqdad> set url not readonly and fix typo
| -rw-r--r-- | indoteknik_custom/models/find_page.py | 102 | ||||
| -rw-r--r-- | indoteknik_custom/views/find_page.xml | 2 |
2 files changed, 43 insertions, 61 deletions
diff --git a/indoteknik_custom/models/find_page.py b/indoteknik_custom/models/find_page.py index 5e530c64..e50ccd5e 100644 --- a/indoteknik_custom/models/find_page.py +++ b/indoteknik_custom/models/find_page.py @@ -5,7 +5,7 @@ import pysolr _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://localhost: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): @@ -34,50 +34,32 @@ class FindPage(models.Model): _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', readonly=True) - keywords = fields.Char(string='Keywords') - - - @api.model - def create(self, vals): - record = super(FindPage, self).create(vals) - record._sync_to_solr() - return record - - def write(self, vals): - res = super(FindPage, self).write(vals) - self._sync_to_solr() - return res + url = fields.Char(string='Url') def _sync_to_solr(self, limit=10000): urls = self.env['web.find.page'].search([]) documents = [] catch = {} + keywords = self.keywords for url in urls: try: - keyword = self.keywords document = { 'id': url.id, 'category_id_i': url.category_id.id, 'brand_id_i': url.brand_id.id, - 'url_s': url.url, + 'url_s': url.url } - if keyword: - document['keyword_s'] = keyword + if url.keywords: + document['keywords_s']= 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) - - try: - _cat_brand_solr_dev.add(documents) - except Exception as e: - _logger.error('Gagal sinkron ke Solr: %s', e) - return True - + _cat_brand_solr_dev.add(documents) return True def _get_category_hierarchy(self, category): @@ -88,66 +70,66 @@ class FindPage(models.Model): current_category = current_category.parent_id return categories - def _generate_url(self): - list_url = [] - keyword = self.keywords + categories = self.env['v.brand.product.category'].search([]) - if keyword: - brands = self.env['x_manufactures'].search([('x_name', 'ilike', keyword)]) - categories = self.env['product.public.category'].search([('name', 'ilike', keyword)]) + list_url = [] + for category in categories: + category_hierarchy = self._get_category_hierarchy(category.category_id) - for brand in brands: - for category in categories: - list_url.append(self._generate_mod_url(category, brand, keyword)) - else: - # Default: ambil semua kombinasi brand-category dari view - 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)) + for level, cat in enumerate(reversed(category_hierarchy), start=1): + list_url.append(self._generate_mod_url(cat, category.brand_id)) + # print(f"Level {level}: {cat.name} {category.brand_id.x_name}") - # Hapus duplikat URL 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']) + self._create_find_page(item['url'], item['category_id'], item['brand_id'], item['keywords']) count += 1 + print(f"Total categories processed: {count}") - _logger.info(f"Total URL generated: {count}") - - def _create_find_page(self, url, category_id, brand_id): + 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, keyword=None): + 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 = f"{cleaned_category} {cleaned_brand}" - cleaned_combined = re.sub(r'\s+', '-', cleaned_combined.strip()) - - if keyword: - slugified_keyword = re.sub(r'\s+', '-', keyword.strip().lower()) - url = f"https://indoteknik.com/shop/find/key={slugified_keyword}" + 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 = f"https://indoteknik.com/shop/find/{cleaned_combined.lower()}" - + 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 keyword: - result['keyword_s'] = keyword - + if keywords: + result['keywords_s'] = keywords + # print(url) + # param = { + # 'brand_id': brand.id, + # 'category_id': category.id, + # 'url':'' + # } + # self.env['web.find.page'].create() + # print(1) return result diff --git a/indoteknik_custom/views/find_page.xml b/indoteknik_custom/views/find_page.xml index 65b0a570..e333559e 100644 --- a/indoteknik_custom/views/find_page.xml +++ b/indoteknik_custom/views/find_page.xml @@ -27,7 +27,7 @@ <field name="category_id"/> <field name="brand_id"/> <field name="keywords"/> - <field name="url" readonly="1"/> + <field name="url" /> </group> <group> <field name="create_uid"/> |
