summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2024-02-28 14:07:47 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2024-02-28 14:07:47 +0700
commit00b6739e4f4228c1cc66de0ef63312bc633ae21f (patch)
tree85f8fc83d4030878893599abb2a949e5d478e3e7 /indoteknik_api/controllers
parent0738a192409687790c16c757f85fe440cb1f377d (diff)
parent46a7cc5601ceab2a7a6cdf4d74e0fa26ce13ab8a (diff)
Merge branch 'production' into purchasing-job
Diffstat (limited to 'indoteknik_api/controllers')
-rw-r--r--indoteknik_api/controllers/__init__.py1
-rw-r--r--indoteknik_api/controllers/api_v1/cart.py42
-rw-r--r--indoteknik_api/controllers/api_v1/voucher.py29
-rw-r--r--indoteknik_api/controllers/controller.py22
-rw-r--r--indoteknik_api/controllers/export.py46
5 files changed, 107 insertions, 33 deletions
diff --git a/indoteknik_api/controllers/__init__.py b/indoteknik_api/controllers/__init__.py
index 237f4135..34bba89f 100644
--- a/indoteknik_api/controllers/__init__.py
+++ b/indoteknik_api/controllers/__init__.py
@@ -1,4 +1,5 @@
from . import controller
+from . import export
from . import api_v1
from . import api_v2
from . import api_v3 \ No newline at end of file
diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py
index 907c8288..f472a9b0 100644
--- a/indoteknik_api/controllers/api_v1/cart.py
+++ b/indoteknik_api/controllers/api_v1/cart.py
@@ -44,6 +44,7 @@ class Cart(controller.Controller):
qty = int(kw.get('qty', 0))
source = kw.get('source')
+ qty_append = kw.get('qty_append', False)
is_selected = kw.get('selected', False)
is_selected = is_selected in ('true', True)
@@ -52,6 +53,9 @@ class Cart(controller.Controller):
if not user_id:
return self.response(code=400, description='user_id is required')
+ if not product_id and not program_line_id:
+ return self.response(code=400, description='product_id or program_line_id is required')
+
website_user_cart = request.env['website.user.cart']
# Remove previous 'buy' entries for the user
@@ -68,32 +72,36 @@ class Cart(controller.Controller):
cart = website_user_cart.search(query, limit=1)
data_to_update = {
+ 'user_id': user_id,
'qty': qty,
'is_selected': is_selected,
'program_line_id': program_line_id,
- 'product_id': product_id
+ 'product_id': product_id,
+ 'source': source or False
}
+
+ if isinstance(qty_append, str) and qty_append.lower() == "true" and cart:
+ data_to_update['qty'] += cart.qty
- if program_line_id:
- data_to_update['product_id'] = False
-
- if source:
- data_to_update['source'] = source
-
- result = {}
if cart:
- # Update existing cart entry
cart.write(data_to_update)
- result['id'] = cart.id
else:
- # Create a new cart entry if it doesn't exist
- create = website_user_cart.create({
- 'user_id': user_id,
- **data_to_update
- })
- result['id'] = create.id
+ cart = website_user_cart.create(data_to_update)
- return self.response(result)
+ return self.response({
+ 'id': cart.id,
+ 'product': {
+ 'id': cart.product_id.id,
+ 'name': cart.product_id.name
+ } if cart.product_id else None,
+ 'program_line': {
+ 'id': cart.program_line_id.id,
+ 'name': cart.program_line_id.name
+ } if cart.program_line_id else None,
+ 'qty': cart.qty,
+ 'is_selected': cart.is_selected,
+ 'source': cart.source
+ })
@http.route(PREFIX_USER + 'cart', auth='public', methods=['DELETE', 'OPTIONS'], csrf=False)
@controller.Controller.must_authorized()
diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py
index 3c056ecd..53f118ec 100644
--- a/indoteknik_api/controllers/api_v1/voucher.py
+++ b/indoteknik_api/controllers/api_v1/voucher.py
@@ -1,31 +1,39 @@
-from .. import controller
+from bs4 import BeautifulSoup
from odoo import http
from odoo.http import request
-from bs4 import BeautifulSoup
+
+from .. import controller
class Voucher(controller.Controller):
- prefix = '/api/v1/'
+ PREFIX_API = '/api/v1/'
+
+ @http.route(PREFIX_API + 'voucher', auth='public', methods=['GET', 'OPTIONS'])
+ @controller.Controller.must_authorized()
+ def get_vouchers(self, **kw):
+ vouchers = request.env['voucher'].get_active_voucher([('visibility', 'in', ['public'])])
+ vouchers = vouchers.res_format()
+ return self.response(vouchers)
- @http.route(prefix + 'user/<user_id>/voucher', auth='public', methods=['GET', 'OPTIONS'])
+ @http.route(PREFIX_API + 'user/<user_id>/voucher', auth='public', methods=['GET', 'OPTIONS'])
@controller.Controller.must_authorized(private=True, private_key='user_id')
- def get_vouchers(self, **kw):
+ def get_vouchers_by_user_id(self, **kw):
cart = request.env['website.user.cart']
code = kw.get('code')
user_id = int(kw.get('user_id', 0))
source = kw.get('source')
visibility = ['public']
- parameter = []
+ domain = []
if code:
visibility.append('private')
- parameter += [('code', '=', code)]
+ domain += [('code', '=', code)]
user_pricelist = request.env.context.get('user_pricelist')
if user_pricelist:
- parameter += [('excl_pricelist_ids', 'not in', [user_pricelist.id])]
+ domain += [('excl_pricelist_ids', 'not in', [user_pricelist.id])]
- parameter += [('visibility', 'in', visibility)]
- vouchers = request.env['voucher'].get_active_voucher(parameter)
+ domain += [('visibility', 'in', visibility)]
+ vouchers = request.env['voucher'].get_active_voucher(domain)
checkout = cart.get_user_checkout(user_id, source=source)
products = checkout['products']
@@ -35,6 +43,7 @@ class Voucher(controller.Controller):
order_line = []
for product in products:
+ if product['cart_type'] == 'promotion': continue
order_line.append({
'product_id': request.env['product.product'].browse(product['id']),
'price': product['price']['price'],
diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py
index 50e86b68..c4f323fe 100644
--- a/indoteknik_api/controllers/controller.py
+++ b/indoteknik_api/controllers/controller.py
@@ -193,8 +193,11 @@ class Controller(http.Controller):
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', '')
+ variant = kw.get('variant', False)
+
image = model['image_256'] or ''
- image = self.add_watermark_to_image(image, ratio, version)
+ if not variant:
+ image = self.add_watermark_to_image(image, ratio, version)
response_headers = [
('Content-Type', 'image/jpg'),
@@ -210,7 +213,12 @@ class Controller(http.Controller):
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_FILENAME = {
+ '1': 'logo-indoteknik-gray.png',
+ '2': 'logo-indoteknik.png'
+ }
+
+ logo_path = get_module_resource('indoteknik_api', 'static', 'src', 'images', LOGO_FILENAME.get(version))
logo_img = Image.open(logo_path).convert('RGBA')
img_data = io.BytesIO(base64.b64decode(image))
@@ -238,13 +246,15 @@ class Controller(http.Controller):
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)
+ new_img.paste(logo_footer_img, (0, img_height - logo_footer_h), logo_footer_img)
+
+ logo_img_w = img_width // 1.8
+ logo_img_h = img_height // 1.8
logo_img.thumbnail((logo_img_w, logo_img_h))
- if version == '1':
- # Add watermark
- new_img.paste(logo_img, (12, 10), logo_img)
+ # Add watermark
+ new_img.paste(logo_img, (12, 10), logo_img)
buffered = io.BytesIO()
new_img.save(buffered, format="PNG")
diff --git a/indoteknik_api/controllers/export.py b/indoteknik_api/controllers/export.py
new file mode 100644
index 00000000..c29c82c7
--- /dev/null
+++ b/indoteknik_api/controllers/export.py
@@ -0,0 +1,46 @@
+import json
+
+from odoo.tools import pycompat
+from odoo.exceptions import Warning
+from odoo import http
+from odoo.http import request
+from odoo.addons.web.controllers.main import ExportFormat, GroupExportXlsxWriter, ExportXlsxWriter, serialize_exception, clean_action
+
+class Export(ExportFormat, http.Controller):
+ @http.route('/web/export/xlsx', type='http', auth="public", csrf=False)
+ @serialize_exception
+ def export_xlsx(self, data, token, **kw):
+ data_obj = json.loads(data)
+ model = data_obj['model']
+ can_export = request.env.user.check_access(model, 'export')
+
+ if not can_export:
+ raise Warning('You are not allowed to export')
+
+ return self.base(data, token)
+
+ @property
+ def content_type(self):
+ return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
+
+ def filename(self, base):
+ return base + '.xlsx'
+
+ def from_group_data(self, fields, groups):
+ with GroupExportXlsxWriter(fields, groups.count) as xlsx_writer:
+ x, y = 1, 0
+ for group_name, group in groups.children.items():
+ x, y = xlsx_writer.write_group(x, y, group_name, group)
+
+ return xlsx_writer.value
+
+ def from_data(self, fields, rows):
+ with ExportXlsxWriter(fields, len(rows)) as xlsx_writer:
+ for row_index, row in enumerate(rows):
+ for cell_index, cell_value in enumerate(row):
+ if isinstance(cell_value, (list, tuple)):
+ cell_value = pycompat.to_text(cell_value)
+ xlsx_writer.write_cell(row_index + 1, cell_index, cell_value)
+
+ return xlsx_writer.value
+ \ No newline at end of file