summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/blog_post.py
blob: 18844f0fb4e3c487bf142a41e1d0c10d48f7be40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from odoo import models, fields
from odoo.exceptions import UserError
import logging
import re

_logger = logging.getLogger(__name__)


class BlogPost(models.Model):
    _inherit = 'blog.post'

    thumbnail = fields.Binary(string="Thumbnail")
    thumbnail_128 = fields.Image("Thumbnail 128", related="thumbnail", max_width=128, max_height=128, store=True)
    thumbnail_256 = fields.Image("Thumbnail 256", related="thumbnail", max_width=256, max_height=256, store=True)
    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('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 klik sekali')
        
        category_name = self.category_id.name
        category_name = category_name.replace(' ', '+')
        link_url = '<a href="https://indoteknik.com/shop/search?product_name=' + category_name + '">' + self.category_id.name + '</a>'

        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