summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1/blog.py
blob: fcca2f32b22a883a2bdcfd3270af10482266ca04 (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
38
39
40
from .. import controller
from odoo import http
from odoo.http import request


class Blog(controller.Controller):
    prefix = '/api/v1/'
    
    @http.route(prefix + 'blog', auth='public', methods=['GET', 'OPTIONS'])
    @controller.Controller.must_authorized()
    def get_blog(self, **kw):
        limit = int(kw.get('limit', 0))
        offset = int(kw.get('offset', 0))
        query = [('active', '=', True)]
        
        title = kw.get('title')
        if title:
            query.append(('name', 'ilike', '%' + title.replace(' ', '%') + '%'))
        
        blogs = request.env['blog.post'].search(query, limit=limit, offset=offset, order='post_date desc')
        data = {
            'blog_total': request.env['blog.post'].search_count(query),
            'blogs': [request.env['blog.post'].api_single_response(x) for x in blogs]
        }
        return self.response(data)
    
    @http.route(prefix + 'blog/<id>', auth='public', methods=['GET', 'OPTIONS'])
    @controller.Controller.must_authorized()
    def get_blog_by_id(self, **kw):
        id = kw.get('id')
        if not id:
            return self.response(code=400, description='id is required')
        
        data = {}
        id = int(id)
        blog = request.env['blog.post'].search([('id', '=', id)], limit=1)
        if blog:
            data = request.env['blog.post'].api_single_response(blog, with_detail=True)
        
        return self.response(data)