summaryrefslogtreecommitdiff
path: root/indoteknik_api
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2024-02-22 14:49:25 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2024-02-22 14:49:25 +0700
commitda0a75bc6b7166e297bcaf175c15089e95dbbf2e (patch)
tree156cfb319848cd2b2964f83b3cb185e8f920f7b2 /indoteknik_api
parent6850fd6f86a0fbba3156e59f9ac5836052f019ce (diff)
parent0face879a802bf1a15d1065d59db64f723b4e8d3 (diff)
Merge branch 'production' of bitbucket.org:altafixco/indoteknik-addons into production
Diffstat (limited to 'indoteknik_api')
-rw-r--r--indoteknik_api/controllers/api_v1/cart.py38
-rw-r--r--indoteknik_api/controllers/api_v1/voucher.py29
-rw-r--r--indoteknik_api/models/product_pricelist.py2
3 files changed, 41 insertions, 28 deletions
diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py
index 907c8288..5948a277 100644
--- a/indoteknik_api/controllers/api_v1/cart.py
+++ b/indoteknik_api/controllers/api_v1/cart.py
@@ -52,6 +52,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 +71,33 @@ 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 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/models/product_pricelist.py b/indoteknik_api/models/product_pricelist.py
index f05fa82d..0d4247c8 100644
--- a/indoteknik_api/models/product_pricelist.py
+++ b/indoteknik_api/models/product_pricelist.py
@@ -92,7 +92,7 @@ class ProductPricelist(models.Model):
('is_flash_sale', '=', True),
('start_date', '<=', current_time),
('end_date', '>=', current_time)
- ], limit=1)
+ ], limit=1, order='start_date asc')
return pricelist
def is_flash_sale_product(self, product_id: int):