From ea39d3aaf90aa99f4e845420cf11223dc993aa7f Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Thu, 23 Feb 2023 11:31:59 +0700 Subject: change method replace keyword blog --- indoteknik_custom/models/blog_post.py | 37 +++++++++++++---------------------- indoteknik_custom/views/blog_post.xml | 2 +- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/indoteknik_custom/models/blog_post.py b/indoteknik_custom/models/blog_post.py index 6e96a311..18844f0f 100644 --- a/indoteknik_custom/models/blog_post.py +++ b/indoteknik_custom/models/blog_post.py @@ -15,32 +15,23 @@ class BlogPost(models.Model): thumbnail_512 = fields.Image("Thumbnail 512", related="thumbnail", max_width=512, max_height=512, store=True) thumbnail_1024 = fields.Image("Thumbnail 1024", related="thumbnail", max_width=1024, max_height=1024, store=True) replace_click = fields.Boolean(string='Replace Clicked', help='Penanda bahwa Replace Keyword sudah di klik atau belum') - category_id = fields.Many2one(string='Product Category', help='Untuk replace keyword sesuai product category yang dipilih') + category_id = fields.Many2one('product.public.category', string='Product Category', help='Untuk replace keyword sesuai product category yang dipilih') def replace_keyword_to_link(self): + if not self.category_id: + return if self.replace_click: - raise UserError('Sudah pernah di click') - query = [ - ('level', '=', 'level4') - # ('id', '=', 1057) for debug only - ] - categories = self.env['product.public.category'].search(query, order='id') - for category in categories: - category_name = category.name - category_name = category_name.replace(' ', '+') - link_url = '' + category.name + '' + raise UserError('Sudah pernah di klik sekali') + + category_name = self.category_id.name + category_name = category_name.replace(' ', '+') + link_url = '' + self.category_id.name + '' - query = [ - ('content', 'ilike', category.name) - ] - # blogs = self.env['blog.post'].search(query, order='id') - - # for blog in blogs: - content = self.content - compiled = re.compile(re.escape(category.name), re.IGNORECASE) - res = compiled.sub(link_url, content) - # content = content.replace(category.name, link_url) - self.content = res - _logger.info('Blog: Success Replace text to URL %s' % self.id) + content = self.content + compiled = re.compile(re.escape(self.category_id.name), re.IGNORECASE) + res = compiled.sub(link_url, content) + # content = content.replace(category.name, link_url) + self.content = res + _logger.info('Blog: Success Replace text to URL %s' % self.id) self.replace_click = True diff --git a/indoteknik_custom/views/blog_post.xml b/indoteknik_custom/views/blog_post.xml index d34a82d4..de346d1f 100644 --- a/indoteknik_custom/views/blog_post.xml +++ b/indoteknik_custom/views/blog_post.xml @@ -7,12 +7,12 @@ +