diff options
| author | Mqdd <ahmadmiqdad27@gmail.com> | 2025-12-31 13:29:51 +0700 |
|---|---|---|
| committer | Mqdd <ahmadmiqdad27@gmail.com> | 2025-12-31 13:29:51 +0700 |
| commit | a069b9d6b986e514c17b42df5e807b7076338cda (patch) | |
| tree | f13153c25303ae18e08d00fea28c26961c2fd773 | |
| parent | 7bd473f65b08d50152cc058385f681a60050e2d5 (diff) | |
<Miqdad> add brand & compute url
| -rw-r--r-- | indoteknik_custom/models/keywords.py | 92 |
1 files changed, 70 insertions, 22 deletions
diff --git a/indoteknik_custom/models/keywords.py b/indoteknik_custom/models/keywords.py index ef3715c9..53cf4225 100644 --- a/indoteknik_custom/models/keywords.py +++ b/indoteknik_custom/models/keywords.py @@ -41,16 +41,25 @@ class Keywords(models.Model): for record in self: record.sum = len(record.product_ids) - @api.depends('keywords') + + @api.depends('keywords', 'brand_id') def _compute_url(self): prefix = "https://indoteknik.com/searchkey/" for record in self: - if record.keywords: - slug = re.sub(r'[^a-zA-Z0-9]+', '-', record.keywords.strip().lower()) - slug = slug.strip('-') - record.url = prefix + slug - else: + if not record.keywords: record.url = False + continue + slug = re.sub(r'[^a-zA-Z0-9]+', '-', record.keywords.strip().lower()).strip('-') + + if record.brand_id: + brand_slug = re.sub( + r'[^a-zA-Z0-9]+', + '-', + record.brand_id.x_name.strip().lower() + ).strip('-') + record.url = f"{prefix}{slug}-{brand_slug}" + else: + record.url = f"{prefix}{slug}" def _compute_name(self): for record in self: @@ -75,14 +84,23 @@ class Keywords(models.Model): for record in self: record.product_ids = [(5, 0, 0)] + def generate_products(self): for record in self: - if not record.keywords or record.skip: + if record.skip: continue - keyword = f"%{record.keywords.strip()}%" + if not record.keywords: + raise UserError("Keyword wajib diisi") + + if not record.category_id: + raise UserError( + f"Category wajib diisi untuk keyword '{record.keywords}'" + ) + + keyword_raw = record.keywords.strip() + keyword = f"%{keyword_raw}%" - # AND (pt.unpublished IS FALSE OR pt.unpublished IS NULL) sql = """ SELECT DISTINCT pp.id FROM product_product pp @@ -98,26 +116,55 @@ class Keywords(models.Model): ) """ - params = [ - keyword, - keyword, - ] + params = [keyword, keyword] + + # ====================== + # CATEGORY (WAJIB) + # ====================== + child_categs = self.env['product.public.category'].search([ + ('id', 'child_of', record.category_id.id) + ]) + + sql += " AND rel.product_public_category_id = ANY(%s)" + params.append(child_categs.ids) - if record.category_id: - child_categs = self.env['product.public.category'].search([ - ('id', 'child_of', record.category_id.id) + # ====================== + # BRAND (OPTIONAL) + # ====================== + if record.brand_id: + brand = record.brand_id.x_name.strip() + + brand_kw_1 = f"%{brand} {keyword_raw}%" + brand_kw_2 = f"%{keyword_raw} {brand}%" + + sql += """ + AND ( + pt.name ILIKE %s + OR pt.name ILIKE %s + OR pt.website_description ILIKE %s + OR pt.website_description ILIKE %s + ) + """ + + params.extend([ + brand_kw_1, + brand_kw_2, + brand_kw_1, + brand_kw_2, ]) - sql += " AND rel.product_public_category_id = ANY(%s)" - params.append(child_categs.ids) + # ====================== + # EXECUTE + # ====================== self.env.cr.execute(sql, params) - rows = self.env.cr.fetchall() + product_ids = [r[0] for r in rows] if not product_ids: raise UserError( - f"Tidak berhasil menemukan barang untuk keyword '{record.keywords}'" + f"Tidak menemukan product untuk keyword " + f"'{record.keywords}' di category '{record.category_id.name}'" ) record.with_context(skip_generate=True).write({ @@ -125,9 +172,10 @@ class Keywords(models.Model): }) _logger.info( - "Product Found: Found %s products for keyword '%s'", + "Product Found: %s products | keyword='%s' | category='%s'", len(product_ids), - record.keywords + record.keywords, + record.category_id.name ) def sync_solr(self): |
