summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2024-02-07 10:45:51 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2024-02-07 10:45:51 +0700
commitf6be42d37a363b86f4a9ec71ccb38c78cbe2d887 (patch)
tree9c297e776f67e35671012e94d3c64007bd5090d7 /indoteknik_api/controllers
parentedb3c1c80931078d40a8f56149aeca9efdcdc07d (diff)
parent29a9ec94f1ad131f398cf119a03a7b927a4c6cba (diff)
Merge branch 'production' into purchasing-job
# Conflicts: # indoteknik_custom/__manifest__.py # indoteknik_custom/models/__init__.py # indoteknik_custom/models/automatic_purchase.py # indoteknik_custom/models/purchase_order_line.py # indoteknik_custom/security/ir.model.access.csv # indoteknik_custom/views/automatic_purchase.xml # indoteknik_custom/views/purchase_order.xml
Diffstat (limited to 'indoteknik_api/controllers')
-rw-r--r--indoteknik_api/controllers/api_v1/cart.py21
-rw-r--r--indoteknik_api/controllers/api_v1/promotion.py22
-rw-r--r--indoteknik_api/controllers/controller.py79
3 files changed, 108 insertions, 14 deletions
diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py
index 8ef2c1c1..907c8288 100644
--- a/indoteknik_api/controllers/api_v1/cart.py
+++ b/indoteknik_api/controllers/api_v1/cart.py
@@ -36,18 +36,21 @@ class Cart(controller.Controller):
def create_or_update_cart(self, user_id, **kw):
# Convert input values to appropriate types
user_id = int(user_id)
- product_id = int(kw.get('product_id', 0))
- qty = int(kw.get('qty', 0))
- source = kw.get('source')
- is_selected = kw.get('selected', False)
+ product_id = kw.get('product_id', 0)
+ product_id = False if product_id == 'null' or not product_id else int(product_id)
+
program_line_id = kw.get('program_line_id', False)
program_line_id = False if program_line_id == 'null' or not program_line_id else int(program_line_id)
+ qty = int(kw.get('qty', 0))
+ source = kw.get('source')
+
+ is_selected = kw.get('selected', False)
is_selected = is_selected in ('true', True)
# Check required fields
- if not user_id or not product_id or not qty:
- return self.response(code=400, description='user_id, product_id and qty is required')
+ if not user_id:
+ return self.response(code=400, description='user_id is required')
website_user_cart = request.env['website.user.cart']
@@ -97,9 +100,15 @@ class Cart(controller.Controller):
def delete_cart_by_user_id(self, user_id, **kw):
user_id = int(user_id)
query = [('user_id', '=', user_id)]
+
+ ids = kw.get('ids')
+ if ids:
+ query += [('id', 'in', [int(x) for x in ids.split(',')])]
+
product_ids = kw.get('product_ids')
if product_ids:
query += [('product_id', 'in', [int(x) for x in product_ids.split(',')])]
+
cart = request.env['website.user.cart'].search(query).unlink()
return self.response(cart)
diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py
index f84b8c1c..221f6e10 100644
--- a/indoteknik_api/controllers/api_v1/promotion.py
+++ b/indoteknik_api/controllers/api_v1/promotion.py
@@ -6,6 +6,28 @@ from datetime import datetime
class Promotion(controller.Controller):
prefix = '/api/v1/'
+
+ @http.route(prefix + 'program-line/<id>/stock', auth='public', methods=['GET', 'OPTIONS'])
+ @controller.Controller.must_authorized()
+ def get_promotion_stock(self, id):
+ program_line = request.env['promotion.program.line'].browse(int(id))
+ if not program_line.id:
+ return self.response(code=400, description='program not found')
+
+ user_data = self.verify_user_token()
+
+ limit_qty = program_line._res_limit_qty()
+ remaining_qty = program_line._get_remaining_qty(user_data)
+
+ percent_remaining = 0
+ if limit_qty['all'] > 0:
+ percent_remaining = (limit_qty['all'] - remaining_qty['all']) / limit_qty['all'] * 100
+
+ return self.response({
+ 'limit_qty': limit_qty,
+ 'remaining_qty': remaining_qty,
+ 'used_percentage': percent_remaining,
+ })
@http.route(prefix + 'promotion/<id>', auth='public', methods=['GET'])
diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py
index 4d6716b2..50e86b68 100644
--- a/indoteknik_api/controllers/controller.py
+++ b/indoteknik_api/controllers/controller.py
@@ -1,14 +1,17 @@
-from array import array
-import datetime
import base64
+import datetime
+import functools
+import io
import json
+from array import array
+import jwt
from odoo import http
from odoo.http import request
+from odoo.modules import get_module_resource
from odoo.tools.config import config
+from PIL import Image
from pytz import timezone
-import jwt
-import functools
class Controller(http.Controller):
@@ -182,8 +185,68 @@ class Controller(http.Controller):
return self.response(address)
@http.route('/api/image/<model>/<field>/<id>', auth='public', methods=['GET'])
- def get_image(self, model, field, id):
+ def get_image(self, model, field, id, **kw):
+ model_name = model
model = request.env[model].sudo().search([('id', '=', id)], limit=1)
- image = model[field] if model[field] else ''
- request.env['user.activity.log'].record_activity()
- return request.make_response(base64.b64decode(image), [('Content-Type', 'image/jpg')])
+ image = model[field] if field in model else ''
+
+ if model_name in ['product.template']:
+ version = '1' if field in ['image_256', 'image_512', 'image_1024', 'image_1920'] else '2'
+ ratio = kw.get('ratio', '')
+ image = model['image_256'] or ''
+ image = self.add_watermark_to_image(image, ratio, version)
+
+ response_headers = [
+ ('Content-Type', 'image/jpg'),
+ ('Cache-Control', 'public, max-age=3600')
+ ]
+
+ return request.make_response(
+ base64.b64decode(image),
+ response_headers
+ )
+
+
+ def add_watermark_to_image(self, image, ratio, version = '1'):
+ if not image: return ''
+
+ logo_path = get_module_resource('indoteknik_api', 'static', 'src', 'images', 'logo-indoteknik-gray.png')
+ logo_img = Image.open(logo_path).convert('RGBA')
+
+ img_data = io.BytesIO(base64.b64decode(image))
+ img = Image.open(img_data).convert('RGBA')
+
+ img_width, img_height = img.size
+ longest_wh = max(img_height, img_width)
+
+ # Resize logo image
+ logo_img_w = img_width // 2.2
+ logo_img_h = img_height // 2.2
+
+ new_img = img
+
+ if ratio == 'square':
+ new_img = Image.new('RGBA', (longest_wh, longest_wh), (255, 255, 255, 255))
+
+ paste_x = (longest_wh - img_width) // 2
+ paste_y = (longest_wh - img_height) // 2
+
+ new_img.paste(img, (paste_x, paste_y), img)
+
+ if version == '2':
+ logo__footer_path = get_module_resource('indoteknik_api', 'static', 'src', 'images', 'logo-indoteknik-footer.png')
+ logo_footer_img = Image.open(logo__footer_path).convert('RGBA')
+ logo_footer_img.thumbnail((img_width, img_height // 1))
+ logo_footer_w, logo_footer_h = logo_footer_img.size
+ new_img.paste(logo_footer_img, (0, img_height - logo_footer_h - 20), logo_footer_img)
+
+ logo_img.thumbnail((logo_img_w, logo_img_h))
+
+ if version == '1':
+ # Add watermark
+ new_img.paste(logo_img, (12, 10), logo_img)
+
+ buffered = io.BytesIO()
+ new_img.save(buffered, format="PNG")
+ return base64.b64encode(buffered.getvalue()).decode('utf-8')
+