diff options
| author | Mqdd <ahmadmiqdad27@gmail.com> | 2025-12-12 08:55:07 +0700 |
|---|---|---|
| committer | Mqdd <ahmadmiqdad27@gmail.com> | 2025-12-12 08:55:07 +0700 |
| commit | 097f53b8cb0b2c39fb14f5f3a665410df2c7ebf3 (patch) | |
| tree | 0bc6e9a5c4e0341472edf06474d413eab4673534 | |
| parent | ed88e91aa769e07ba048fd8b3067442754c06065 (diff) | |
<MIqdad> done temp add product to keywords
| -rwxr-xr-x | indoteknik_custom/models/product_template.py | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index 400d31e0..2cbef168 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -1,3 +1,4 @@ +from this import s from odoo import fields, models, api, tools, _ from datetime import datetime, timedelta, date from odoo.exceptions import UserError @@ -84,6 +85,7 @@ class ProductTemplate(models.Model): if self.env.user.id not in users_in_group.mapped('id') and active_model == None: raise UserError('Hanya MD yang bisa membuat Product') result = super(ProductTemplate, self).create(vals) + self.env['product.product']._add_product_to_keywords(result) return result # def write(self, values): @@ -883,6 +885,12 @@ class ProductTemplate(models.Model): result = super().write(vals) # Log changes self._log_field_changes_product(vals, old_values) + + # Add product to keywords + keyword_trigger = ['name', 'website_description', 'unpublished'] + if any(field in vals for field in keyword_trigger): + for product in self: + self.env['product.product']._add_product_to_keywords(product) return result # def write(self, vals): @@ -931,6 +939,24 @@ class ProductProduct(models.Model): barcode_box = fields.Char("Barcode Box") keyword_id = fields.Many2one('keywords', string='Keyword') + def _add_product_to_keywords(self,product): + keywords_model = self.env['keywords'] + if not product: + return False + + for product in self: + match_keywords = keywords_model.search([ + '|', + ('name', 'ilike', product.name), + ('keywords', 'ilike', product.website_description) + ]) + + for kw in match_keywords.filtered(lambda k: not k.skip): + if not self.unpublished and product.id not in kw.product_ids.ids: + kw.write({'product_ids': [(4, product.id)]}) + + return True + def generate_product_sla(self): product_variant_ids = self.env.context.get('active_ids', []) product_variant = self.search([('id', 'in', product_variant_ids)]) @@ -951,6 +977,7 @@ class ProductProduct(models.Model): if self.env.user.id not in users_in_group.mapped('id') and active_model == None: raise UserError('Hanya MD yang bisa membuat Product') result = super(ProductProduct, self).create(vals) + self._add_product_to_keywords(result) return result # def write(self, values): @@ -1315,7 +1342,11 @@ class ProductProduct(models.Model): 'public_categ_ids', 'search_rank', 'search_rank_weekly', 'image_1920', 'unpublished', 'image_carousel_lines' ] - + # pake ini kalau mau Cek semua field + # if vals: + trigger_fields = ['name', 'website_description', 'unpublished'] + if any(f in vals for f in trigger_fields): + self._add_product_to_keywords(vals) if any(field in vals for field in tracked_fields): old_values = self._collect_old_values(vals) result = super().write(vals) |
