summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2022-10-24 15:51:31 +0700
committerIT Fixcomart <it@fixcomart.co.id>2022-10-24 15:51:31 +0700
commit3270307c573ca7a8970899dcc7ee02aa7260a6cb (patch)
treedf092b367d3420bf63bf9b82450f88ee0587124a
parente0d9fdcfd618332d6324c244859e9cbbf3a6a225 (diff)
Get blog and blog detail Rest API
-rw-r--r--indoteknik_api/controllers/api_v1/__init__.py1
-rw-r--r--indoteknik_api/controllers/api_v1/blog.py45
-rw-r--r--indoteknik_api/models/__init__.py1
-rw-r--r--indoteknik_api/models/blog_post.py38
4 files changed, 85 insertions, 0 deletions
diff --git a/indoteknik_api/controllers/api_v1/__init__.py b/indoteknik_api/controllers/api_v1/__init__.py
index b569a012..275313ff 100644
--- a/indoteknik_api/controllers/api_v1/__init__.py
+++ b/indoteknik_api/controllers/api_v1/__init__.py
@@ -1,3 +1,4 @@
+from . import blog
from . import category
from . import flash_sale
from . import manufacture
diff --git a/indoteknik_api/controllers/api_v1/blog.py b/indoteknik_api/controllers/api_v1/blog.py
new file mode 100644
index 00000000..8d38e4f7
--- /dev/null
+++ b/indoteknik_api/controllers/api_v1/blog.py
@@ -0,0 +1,45 @@
+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')
+
+ base_url = request.env['ir.config_parameter'].get_param('web.base.url')
+ 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
diff --git a/indoteknik_api/models/__init__.py b/indoteknik_api/models/__init__.py
index 06f1e1da..de2a2804 100644
--- a/indoteknik_api/models/__init__.py
+++ b/indoteknik_api/models/__init__.py
@@ -1,2 +1,3 @@
+from . import blog_post
from . import product_pricelist
from . import product_template
diff --git a/indoteknik_api/models/blog_post.py b/indoteknik_api/models/blog_post.py
new file mode 100644
index 00000000..3f3444c3
--- /dev/null
+++ b/indoteknik_api/models/blog_post.py
@@ -0,0 +1,38 @@
+from odoo import models
+from pytz import timezone
+
+
+class BlogPost(models.Model):
+ _inherit = 'blog.post'
+
+ def api_single_response(self, blog, with_detail=False):
+ base_url = self.env['ir.config_parameter'].get_param('web.base.url')
+ data = {
+ 'id': blog.id,
+ 'thumbnail': base_url + 'api/image/blog.post/thumbnail/' + str(blog.id) if blog.thumbnail else '',
+ 'title': blog.name,
+ 'category': {},
+ 'author': {},
+ 'create_date': blog.create_date.astimezone(timezone('Asia/Jakarta')).strftime('%d/%m/%Y') or '',
+ }
+ if blog.blog_id:
+ data['category'] = {
+ 'id': blog.blog_id.id,
+ 'name': blog.blog_id.name,
+ }
+ if blog.author_id:
+ data['author'] = {
+ 'id': blog.author_id.id,
+ 'name': blog.author_id.name
+ }
+ if with_detail:
+ data_with_detail = {
+ 'meta': {
+ 'title': blog.website_meta_title,
+ 'description': blog.website_meta_description,
+ 'keywords': blog.website_meta_keywords
+ },
+ 'content': blog.content,
+ }
+ data.update(data_with_detail)
+ return data \ No newline at end of file