diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2023-07-24 03:31:59 +0000 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2023-07-24 03:31:59 +0000 |
| commit | c1ac22304b557b9982b5fb79d23d29b6faf48090 (patch) | |
| tree | a1e6db531a5bf19994374e895d5d8a8c9abf1ffa /indoteknik_api/controllers/api_v1/promotion.py | |
| parent | 6966c00bf2ef2bd9c2261d9363ac6b463a7766dd (diff) | |
| parent | 30909e82d7ff1f3cac4700e284f80552a0d38523 (diff) | |
Merged in feature/voucher-cart (pull request #65)
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, 66 insertions, 4 deletions
diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py index b137fe2e..f84b8c1c 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,7 +15,7 @@ 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)]) @@ -24,6 +25,67 @@ class Promotion(controller.Controller): '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), + ('program_line.display_on_homepage', '=', True) + ]) + + 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 + |
