diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-10-24 17:07:43 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-10-24 17:07:43 +0700 |
| commit | da1ba5223456e4c39d11a5af67968dda6d569c6b (patch) | |
| tree | 310eaa29f5982767e6685c2ca263f24fba72645e /indoteknik_api/controllers/api_v1/blog.py | |
| parent | 646f9557c101bd6b8ed81fc46130e9761957b5c1 (diff) | |
| parent | 1934b5ca55840deb2a4369ef6ab0affd86e2ba8f (diff) | |
Merge commit '1934b5ca55840deb2a4369ef6ab0affd86e2ba8f'
Diffstat (limited to 'indoteknik_api/controllers/api_v1/blog.py')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/blog.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/indoteknik_api/controllers/api_v1/blog.py b/indoteknik_api/controllers/api_v1/blog.py new file mode 100644 index 00000000..af7c4245 --- /dev/null +++ b/indoteknik_api/controllers/api_v1/blog.py @@ -0,0 +1,44 @@ +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']) + def get_blog(self, **kw): + if not self.authenticate(): + return self.response(code=401, description='Unauthorized') + + 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']) + def get_blog_by_id(self, **kw): + if not self.authenticate(): + return self.response(code=401, description='Unauthorized') + + 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)
\ No newline at end of file |
