summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMqdd <ahmadmiqdad27@gmail.com>2025-12-01 16:27:14 +0700
committerMqdd <ahmadmiqdad27@gmail.com>2025-12-01 16:27:14 +0700
commit6ba1731e740f7ad1e7aea38f723a2f431fec2c4b (patch)
tree78e592f98c0c6d7f7f9ca5054f05aa102de24b3f
parentfcd64cd1e041c161a74ebe8f853c3e80000e6480 (diff)
<Miqdad> Initial Commit
-rw-r--r--indoteknik_custom/models/find_page.py69
-rw-r--r--indoteknik_custom/views/find_page.xml15
2 files changed, 67 insertions, 17 deletions
diff --git a/indoteknik_custom/models/find_page.py b/indoteknik_custom/models/find_page.py
index 467e30d1..461e647d 100644
--- a/indoteknik_custom/models/find_page.py
+++ b/indoteknik_custom/models/find_page.py
@@ -4,8 +4,8 @@ import re
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://127.0.0.1:8983/solr/url_category_brand/', always_commit=True, timeout=30)
+# _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)
class BrandProductCategory(models.Model):
@@ -36,7 +36,20 @@ class FindPage(models.Model):
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')
+ 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
def _sync_to_solr(self, limit=10000):
urls = self.env['web.find.page'].search([])
@@ -44,18 +57,27 @@ class FindPage(models.Model):
catch = {}
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
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.add(documents)
+
+ try:
+ _cat_brand_solr_dev.add(documents)
+ except Exception as e:
+ _logger.error('Gagal sinkron ke Solr: %s', e)
+ return True
+
return True
def _get_category_hierarchy(self, category):
@@ -66,26 +88,38 @@ class FindPage(models.Model):
current_category = current_category.parent_id
return categories
- def _generate_url(self):
- categories = self.env['v.brand.product.category'].search([])
+ def _generate_url(self):
list_url = []
- for category in categories:
- category_hierarchy = self._get_category_hierarchy(category.category_id)
+ keyword = self.keywords
- 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}")
+ if keyword:
+ 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, 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))
+
+ # 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'])
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):
param = {
@@ -96,20 +130,25 @@ class FindPage(models.Model):
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):
+ def _generate_mod_url(self, category, brand, keyword):
# 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('--', '-')
- url = 'https://indoteknik.com/shop/find/'+cleaned_combined
+ if self.keywords:
+ url = 'https://indoteknik.com/shop/find/key='+keyword
+ else:
+ url = 'https://indoteknik.com/shop/find/'+cleaned_combined
url = url.lower()
result = {
'url': url,
'category_id': category.id,
'brand_id': brand.id
}
+ if self.keywords:
+ result['keywords_s'] = keyword
# print(url)
# param = {
# 'brand_id': brand.id,
diff --git a/indoteknik_custom/views/find_page.xml b/indoteknik_custom/views/find_page.xml
index c752aa98..65b0a570 100644
--- a/indoteknik_custom/views/find_page.xml
+++ b/indoteknik_custom/views/find_page.xml
@@ -7,6 +7,7 @@
<tree>
<field name="category_id"/>
<field name="brand_id"/>
+ <field name="keywords"/>
<field name="url"/>
<field name="create_uid"/>
<field name="write_uid"/>
@@ -25,7 +26,8 @@
<group>
<field name="category_id"/>
<field name="brand_id"/>
- <field name="url"/>
+ <field name="keywords"/>
+ <field name="url" readonly="1"/>
</group>
<group>
<field name="create_uid"/>
@@ -62,9 +64,18 @@
<field name="view_mode">tree,form</field>
</record>
+
+ <record id="ir_actions_server_find_page_sync_to_solr" model="ir.actions.server">
+ <field name="name">Sync to solr</field>
+ <field name="model_id" ref="indoteknik_custom.model_web_find_page"/>
+ <field name="binding_model_id" ref="indoteknik_custom.model_web_find_page"/>
+ <field name="state">code</field>
+ <field name="code">model._sync_to_solr()</field>
+ </record>
+
<menuitem id="menu_web_find_page"
name="Web Find Page"
action="web_find_page_action"
parent="website_sale.menu_orders"
sequence="8"/>
-</odoo> \ No newline at end of file
+</odoo>