summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMqdd <ahmadmiqdad27@gmail.com>2025-12-02 11:53:49 +0700
committerMqdd <ahmadmiqdad27@gmail.com>2025-12-02 11:53:49 +0700
commitfc226081de0bcf8e573307f94c08dfe4c3769b4d (patch)
treeb6f51bbf73d2f3ffc12e379a0fc0667cf2440859
parent9a16b32bb2fdd58739c410e7c4ea376ead340a32 (diff)
<Miqdad> add import from excel
-rw-r--r--indoteknik_custom/models/find_page.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/indoteknik_custom/models/find_page.py b/indoteknik_custom/models/find_page.py
index 46b69ff2..3968caed 100644
--- a/indoteknik_custom/models/find_page.py
+++ b/indoteknik_custom/models/find_page.py
@@ -2,6 +2,11 @@ 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)
@@ -39,6 +44,78 @@ class FindPage(models.Model):
category_id = fields.Many2one('product.public.category', string='Category', help='Bisa semua level Category')
url = fields.Char(string='Url')
+ def action_import_excel(self):
+ if not self.excel_file:
+ raise UserError(_("⚠️ Harap upload file Excel terlebih dahulu."))
+
+ try:
+ data = base64.b64decode(self.excel_file)
+ book = xlrd.open_workbook(file_contents=data)
+ sheet = book.sheet_by_index(0)
+ except:
+ raise UserError(_("❌ Format Excel tidak valid atau rusak."))
+
+ header = [str(sheet.cell(0, col).value).strip() for col in range(sheet.ncols)]
+ required_headers = [
+ 'category',
+ 'brand',
+ 'keyword',
+ 'url',
+ ]
+
+ for req in required_headers:
+ if req not in header:
+ raise UserError(_("❌ Kolom '%s' tidak ditemukan di file Excel.") % req)
+
+ header_map = {h: idx for idx, h in enumerate(header)}
+ lines_created = 0
+ PublicCategory = self.env['product.public.category']
+ url = self.url
+ keywords = self.keywords
+ brand = self.env['x_manufactures']
+
+ for row_idx in range(1, sheet.nrows):
+ row = sheet.row(row_idx)
+ def val(field):
+ return str(sheet.cell(row_idx, header_map[field]).value).strip()
+
+ if not val('category'):
+ continue # skip kosong
+
+ # Relations
+ brand = brand.search([('name', 'ilike', val('brand'))], limit=1)
+
+ # Many2many: Categories
+ class_names = val('Categories').split(';')
+ class_ids = []
+ for name in class_names:
+ name = name.strip()
+ if name:
+ pc = PublicCategory.search([('name', 'ilike', name)], limit=1)
+ if pc:
+ class_ids.append(pc.id)
+
+ # Build values
+ vals = {
+ 'brand_id': val('brand'),
+ 'keywords': val('keywords'),
+ 'url': val('url'),
+ 'category_id': val('category')
+ }
+
+ lines_created += 1
+
+ return {
+ 'type': 'ir.actions.client',
+ 'tag': 'display_notification',
+ 'params': {
+ 'title': _('✅ Import Selesai'),
+ 'message': _('%s baris berhasil diimport.') % lines_created,
+ 'type': 'success',
+ 'sticky': False,
+ }
+ }
+
def _sync_to_solr(self, limit=10000):
urls = self.env['web.find.page'].search([])
documents = []