diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2023-01-26 10:34:05 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2023-01-26 10:34:05 +0700 |
| commit | aed1817bac8a608bfd630acc8d5d406f13732387 (patch) | |
| tree | 7488b0f1352ca8b276871db446c6673cf0626ed3 | |
| parent | 9df53ac2b48ee5f6285715f642b341dff93ad295 (diff) | |
add rest coupon program
| -rw-r--r-- | indoteknik_api/controllers/api_v1/content.py | 27 | ||||
| -rw-r--r-- | indoteknik_api/models/__init__.py | 1 | ||||
| -rw-r--r-- | indoteknik_api/models/coupon_program.py | 36 |
3 files changed, 63 insertions, 1 deletions
diff --git a/indoteknik_api/controllers/api_v1/content.py b/indoteknik_api/controllers/api_v1/content.py index 3e175dc4..c5b6fd71 100644 --- a/indoteknik_api/controllers/api_v1/content.py +++ b/indoteknik_api/controllers/api_v1/content.py @@ -6,11 +6,36 @@ from odoo.http import request class WebsiteContent(controller.Controller): prefix = '/api/v1/' + @http.route(prefix + 'coupon_program', auth='public', methods=['GET', 'OPTIONS']) + def get_coupon_program(self, **kw): + if not self.authenticate(): + return self.response(code=401, description='Unauthorized') + + reward_type = str(kw.get('reward_type', '')) + limit = int(kw.get('limit', 0)) + offset = int(kw.get('offset', 0)) + + query = [ + ('x_studio_image_promo', '!=', ''), + ] + if reward_type: + query += [('reward_type', '=', reward_type)] + + coupons = request.env['coupon.program'].search(query, limit=limit, offset=offset) + data = { + 'coupon_total': request.env['coupon.program'].search_count(query), + 'coupons': [request.env['coupon.program'].api_single_response(x) for x in coupons] + } + # print (data) + return self.response(data) + + + @http.route(prefix + 'banner/category', auth='public', methods=['GET', 'OPTIONS']) def get_banner_by_category(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') + # base_url = request.env['ir.config_parameter'].get_param('web.base.url') category_id = int(kw.get('category_id'), 0) query = [ diff --git a/indoteknik_api/models/__init__.py b/indoteknik_api/models/__init__.py index 12a1f7fd..d484500a 100644 --- a/indoteknik_api/models/__init__.py +++ b/indoteknik_api/models/__init__.py @@ -6,3 +6,4 @@ from . import res_users from . import sale_order from . import x_manufactures from . import website_content +from . import coupon_program diff --git a/indoteknik_api/models/coupon_program.py b/indoteknik_api/models/coupon_program.py new file mode 100644 index 00000000..7ca003cd --- /dev/null +++ b/indoteknik_api/models/coupon_program.py @@ -0,0 +1,36 @@ +from odoo import models +import datetime +from pytz import timezone + + +class CouponProgram(models.Model): + _inherit = 'coupon.program' + + def api_single_response(self, coupon): + # print (coupon.display_name) + data = { + 'active': coupon.active, + 'discount_percentage': coupon.discount_percentage, + 'display_name': coupon.display_name, + 'id': coupon.id, + 'reward_type': coupon.reward_type, + 'reward_id': coupon.reward_id.id, + 'reward_product_id': coupon.reward_product_id.id, + 'reward_product_quantity': coupon.reward_product_quantity, + 'rule_date_from': self.time_to_str(coupon.rule_date_from, '%d/%m/%Y %H:%M:%S'), + 'rule_date_to': self.time_to_str(coupon.rule_date_to, '%d/%m/%Y %H:%M:%S'), + 'rule_id': coupon.rule_id.id, + 'rule_products_domain': coupon.rule_products_domain, + 'icon_program_promo': self.env['ir.attachment'].api_image('coupon.program', 'x_studio_field_Ifopn', coupon.id), + 'icon_program_promo2': self.env['ir.attachment'].api_image('coupon.program', 'x_studio_field_2Ul77', coupon.id), + 'discount_line_product_id': coupon.discount_line_product_id.id, + 'image_promo': self.env['ir.attachment'].api_image('coupon.program', 'x_studio_image_promo', coupon.id), + 'image_banner_promo': self.env['ir.attachment'].api_image('coupon.program', 'x_studio_banner_promo', coupon.id), + } + return data + + def time_to_str(self, object, format): + time = '' + if isinstance(object, datetime.datetime): + time = object.astimezone(timezone('Asia/Jakarta')).strftime(format) + return time |
