from odoo import http
from odoo.http import request


class CustomSnippets(http.Controller):
    @http.route(['/prime_latest_news_snippet/cart_content'], type='json', auth="public", website=True)
    def cart(self):
        #products = request.website.sale_get_order().order_line.product_id
        #data = []
        #for product in products:
        #    fields = product.read(['display_name', 'description_sale', 'list_price', 'website_url'])[0];
        #    fields['image'] = request.env['website'].image_url(product, 'image_512')
        #    data.append(fields)

        #posts = http.request.env['blog.post'].search([], limit=4)

        sql_query = """
            select 
            bp.name,
            bp.subtitle,
            concat('/blog/', lower(REPLACE(bb.name,' ', '-')) , '-' , bb.id , '/' , lower(REPLACE(bp.name,' ', '-')) , '-' , bp.id ) as post_url,
            left(split_part(bp.cover_properties , 'url(''', 2),  strpos(split_part(bp.cover_properties , 'url(''', 2), ''')"') - 1) as post_image
            from blog_post bp 
            left join blog_blog bb on (bb.id = bp.blog_id)
            order by bp.id desc
            limit 3
            ;
        """
        request.env.cr.execute(sql_query)
        posts = request.env.cr.dictfetchall()

        return request.env['ir.ui.view']._render_template('prime_latest_news_snippet.s_latest_news_area',
                                                          {'posts': posts})
