From 3270307c573ca7a8970899dcc7ee02aa7260a6cb Mon Sep 17 00:00:00 2001 From: IT Fixcomart Date: Mon, 24 Oct 2022 15:51:31 +0700 Subject: Get blog and blog detail Rest API --- indoteknik_api/controllers/api_v1/__init__.py | 1 + indoteknik_api/controllers/api_v1/blog.py | 45 +++++++++++++++++++++++++++ indoteknik_api/models/__init__.py | 1 + indoteknik_api/models/blog_post.py | 38 ++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 indoteknik_api/controllers/api_v1/blog.py create mode 100644 indoteknik_api/models/blog_post.py 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/', 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 -- cgit v1.2.3 From a069927b8f9b2b5942dffc241ed8b79f2d57ecf8 Mon Sep 17 00:00:00 2001 From: IT Fixcomart Date: Mon, 24 Oct 2022 15:54:27 +0700 Subject: Filter price_from and price_to di product Rest API --- indoteknik_api/controllers/api_v1/blog.py | 1 - indoteknik_api/controllers/api_v1/product.py | 8 ++++++++ indoteknik_api/models/product_template.py | 8 ++++---- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/indoteknik_api/controllers/api_v1/blog.py b/indoteknik_api/controllers/api_v1/blog.py index 8d38e4f7..af7c4245 100644 --- a/indoteknik_api/controllers/api_v1/blog.py +++ b/indoteknik_api/controllers/api_v1/blog.py @@ -11,7 +11,6 @@ class Blog(controller.Controller): 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)] diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 0d5d89b6..d667c1b6 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -40,6 +40,14 @@ class Product(controller.Controller): if categories: query.append(('public_categ_ids', 'child_of', [int(x) for x in categories.split(',')])) + + price_from = kw.get('price_from') + if price_from and int(price_from): + query.append(('web_price_sorting', '>=', int(price_from))) + + price_to = kw.get('price_to') + if price_to and int(price_to): + query.append(('web_price_sorting', '<=', int(price_to))) product_variants = request.env['product.product'].search(query) product_variant_ids = [x.id for x in product_variants] diff --git a/indoteknik_api/models/product_template.py b/indoteknik_api/models/product_template.py index 477be2f3..e99b2c2d 100644 --- a/indoteknik_api/models/product_template.py +++ b/indoteknik_api/models/product_template.py @@ -1,4 +1,4 @@ -from odoo import models, fields +from odoo import models class ProductTemplate(models.Model): @@ -21,13 +21,13 @@ class ProductTemplate(models.Model): 'categories': self.api_categories(product_template), } if with_detail: - detail_data = { + data_with_detail = { 'image': base_url + 'api/image/product.template/image_512/' + str(product_template.id) if product_template.image_512 else '', 'variants': [], 'description': product_template.website_description or '', } for variant in product_template.product_variant_ids: - detail_data['variants'].append({ + data_with_detail['variants'].append({ 'id': variant.id, 'code': variant.default_code or '', 'name': variant.display_name, @@ -36,7 +36,7 @@ class ProductTemplate(models.Model): 'weight': variant.weight, 'attributes': [x.name for x in variant.product_template_attribute_value_ids] }) - data.update(detail_data) + data.update(data_with_detail) return data def api_manufacture(self, product_template): -- cgit v1.2.3 From 1934b5ca55840deb2a4369ef6ab0affd86e2ba8f Mon Sep 17 00:00:00 2001 From: IT Fixcomart Date: Mon, 24 Oct 2022 16:05:18 +0700 Subject: (create_date -> post_date) in blog rest api add "tags" in blog rest api --- indoteknik_api/models/blog_post.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/indoteknik_api/models/blog_post.py b/indoteknik_api/models/blog_post.py index 3f3444c3..2a82c23c 100644 --- a/indoteknik_api/models/blog_post.py +++ b/indoteknik_api/models/blog_post.py @@ -13,7 +13,7 @@ class BlogPost(models.Model): 'title': blog.name, 'category': {}, 'author': {}, - 'create_date': blog.create_date.astimezone(timezone('Asia/Jakarta')).strftime('%d/%m/%Y') or '', + 'post_date': blog.post_date.astimezone(timezone('Asia/Jakarta')).strftime('%d/%m/%Y') or '', } if blog.blog_id: data['category'] = { @@ -32,7 +32,13 @@ class BlogPost(models.Model): 'description': blog.website_meta_description, 'keywords': blog.website_meta_keywords }, + 'tags': [], 'content': blog.content, } + for tag in blog.tag_ids: + data_with_detail['tags'].append({ + 'id': tag.id, + 'name': tag.name, + }) data.update(data_with_detail) return data \ No newline at end of file -- cgit v1.2.3