diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-07-18 13:59:18 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-07-18 13:59:18 +0700 |
| commit | 45fd501a53c6997bf74d5927d7c0eecf387caa51 (patch) | |
| tree | fa20bdff3e95d943b0f4846b9746767f87f70f31 /indoteknik_api/controllers/api_v1/promotion.py | |
| parent | 999725ea036840d74c7fdeebbd3aefac772bd8d3 (diff) | |
| parent | d418bd8dd84b91b9dc031819cfa9a2446e77acd2 (diff) | |
Merge branch 'origin/feature/promotion-program' into feature/voucher-cart
Diffstat (limited to 'indoteknik_api/controllers/api_v1/promotion.py')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/promotion.py | 70 |
1 files changed, 65 insertions, 5 deletions
diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py index b137fe2e..68a23ef2 100644 --- a/indoteknik_api/controllers/api_v1/promotion.py +++ b/indoteknik_api/controllers/api_v1/promotion.py @@ -1,12 +1,13 @@ from .. import controller from odoo import http from odoo.http import request -import ast +from datetime import datetime class Promotion(controller.Controller): prefix = '/api/v1/' - + + @http.route(prefix + 'promotion/<id>', auth='public', methods=['GET']) @controller.Controller.must_authorized() def get_promotion_by_id(self, **kw): @@ -14,16 +15,75 @@ class Promotion(controller.Controller): id = kw.get('id') if not id: return self.response(code=400, description='id is required') - + data = {} id = int(id) - coupon_program = request.env['coupon.program'].search([('id', '=', id)]) + coupon_program = request.env['coupon.program'].search( + [('id', '=', id)]) if coupon_program: data = { 'banner': base_url + 'api/image/coupon.program/x_studio_banner_promo/' + str(coupon_program.id) if coupon_program.x_studio_banner_promo else '', 'image': base_url + 'api/image/coupon.program/x_studio_image_promo/' + str(coupon_program.id) if coupon_program.x_studio_image_promo else '', 'name': coupon_program.name, } + + return self.response(data) + + + @http.route(prefix + 'promotion/home', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def v1_get_promotion_home(self): + current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + programs = request.env['promotion.program'].search([ + ('start_time', '<=', current_time), + ('end_time', '>=', current_time), + ]) + if not programs: + return self.response(None) + + data = [] + for program in programs: + data_program = { + 'id': program.id, + 'name': program.name, + 'banner': request.env['ir.attachment'].api_image('promotion.program', 'banner', program.id), + 'icon': request.env['ir.attachment'].api_image('promotion.program', 'icon', program.id) + } + data.append(data_program) + + return self.response(data) + + + @http.route(prefix + 'promotion/home/<id>', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def v1_get_promotion_home_detail(self, id): + program_lines = request.env['promotion.program.line'].search([ + ('display_on_homepage', '=', True), + ('promotion_type', '=', 'special_price'), + ('program_id', '=', int(id)) + ]) + data = [] + for line in program_lines: + product = request.env['product.product'].v2_api_single_response(line.product_id) + product_template = line.product_id.product_tmpl_id + + product.update({ + 'id': product['parent']['id'], + 'image': product['parent']['image'], + 'name': product['parent']['name'], + 'variant_total': len(product_template.product_variant_ids), + 'lowest_price': line.calculate_price(product['price']), + 'stock_total': product['stock'], + 'icon': { + 'top': request.env['ir.attachment'].api_image('promotion.program', 'icon_top', line.program_id.id), + 'bottom': request.env['ir.attachment'].api_image('promotion.program', 'icon_bottom', line.program_id.id) + } + }) + + product.pop('parent', None) + product.pop('price', None) + product.pop('stock', None) + data.append(product) return self.response(data) -
\ No newline at end of file + |
