summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2023-04-06 13:27:48 +0700
committerstephanchrst <stephanchrst@gmail.com>2023-04-06 13:27:48 +0700
commitd1b86303b0934c9f6348e7016822d449349540d3 (patch)
tree3136519871a4f10611714915ca7ecc23930ff2a4
parentf7acb7af352274f149907f924958ca1276c66877 (diff)
integrate flashsale to solr for new website
-rw-r--r--indoteknik_api/controllers/api_v1/flash_sale.py51
-rw-r--r--indoteknik_api/models/product_pricelist.py26
-rw-r--r--indoteknik_api/models/product_product.py41
-rw-r--r--indoteknik_custom/models/apache_solr.py14
4 files changed, 128 insertions, 4 deletions
diff --git a/indoteknik_api/controllers/api_v1/flash_sale.py b/indoteknik_api/controllers/api_v1/flash_sale.py
index ea73ab1b..cad2cb61 100644
--- a/indoteknik_api/controllers/api_v1/flash_sale.py
+++ b/indoteknik_api/controllers/api_v1/flash_sale.py
@@ -10,6 +10,55 @@ _logger = logging.getLogger(__name__)
class FlashSale(controller.Controller):
prefix = '/api/v1/'
+ @http.route(prefix + 'flashsale/header', auth='public', methods=['GET'])
+ @controller.Controller.must_authorized()
+ def _get_flash_sale_header(self, **kw):
+ try:
+ # base_url = request.env['ir.config_parameter'].get_param('web.base.url')
+ active_flash_sale = request.env['product.pricelist'].get_active_flash_sale()
+ data = []
+ for pricelist in active_flash_sale:
+ query = [
+ ('pricelist_id', '=', pricelist.id)
+ ]
+ data.append({
+ 'pricelist_id': pricelist.id,
+ 'option': pricelist.flashsale_option,
+ 'name': pricelist.name,
+ 'banner': request.env['ir.attachment'].api_image('product.pricelist', 'banner', pricelist.id),
+ 'banner_mobile': request.env['ir.attachment'].api_image('product.pricelist', 'banner_mobile', pricelist.id),
+ 'duration': round((pricelist.end_date - datetime.now()).total_seconds()),
+ 'product_total': request.env['product.pricelist.item'].search_count(query),
+ })
+ return self.response(data)
+ except Exception as e:
+ _logger.info(self.prefix + '/flashsale/header: ' + str(e))
+ return self.response(code=500, description='Internal server error')
+
+ @http.route(prefix + 'flashsale/line', auth='public', methods=['GET'])
+ @controller.Controller.must_authorized()
+ def _get_flash_sale_line(self, **kw):
+ try:
+ # base_url = request.env['ir.config_parameter'].get_param('web.base.url')
+ pricelist_id = int(kw.get('pricelist_id'),0)
+ limit = int(kw.get('limit'),0)
+ offset = int(kw.get('offset'),0)
+
+ if pricelist_id == 0 or limit == 0:
+ return self.response(code=500, description='Internal Server error')
+
+ pricelist_items = request.env['product.pricelist.item'].search([('pricelist_id', '=', pricelist_id)], limit=limit, offset=offset)
+ data = {
+ 'total': request.env['product.pricelist.item'].search_count([('pricelist_id', '=', pricelist_id)]),
+ 'products': [request.env['product.pricelist.item'].api_single_response(x) for x in pricelist_items]
+ }
+ return self.response(data)
+
+ except Exception as e:
+ _logger.info(self.prefix + '/flashsale/line: ' + str(e))
+ return self.response(code=500, description='Internal Server error')
+
+
@http.route(prefix + 'flash_sale', auth='public', methods=['GET'])
@controller.Controller.must_authorized()
def get_flash_sale(self, **kw):
@@ -54,6 +103,6 @@ class FlashSale(controller.Controller):
else:
return self.response(code=404, description='Data not found')
except Exception as e:
- _logger.info(self.prefix_url + '/flash_sale: ' + str(e))
+ _logger.info(self.prefix + '/flash_sale: ' + str(e))
return self.response(code=500, description='Internal server error')
\ No newline at end of file
diff --git a/indoteknik_api/models/product_pricelist.py b/indoteknik_api/models/product_pricelist.py
index 4e5c9501..f05fa82d 100644
--- a/indoteknik_api/models/product_pricelist.py
+++ b/indoteknik_api/models/product_pricelist.py
@@ -107,3 +107,29 @@ class ProductPricelist(models.Model):
if product_id in pricelist_product_ids:
return pricelist.id
return False
+
+
+class ProductPricelistItem(models.Model):
+ _inherit = 'product.pricelist.item'
+
+ def api_single_response(self, item):
+ data = {
+ 'id': item.product_id.id,
+ 'parent': {
+ 'id': item.product_id.product_tmpl_id.id,
+ 'name': item.product_id.product_tmpl_id.name,
+ 'image': self.env['ir.attachment'].api_image('product.template', 'image_256', item.product_id.product_tmpl_id.id)
+ },
+ 'code': item.product_id.default_code or item.product_tmpl_id.default_code or '',
+ 'name': item.product_id.display_name,
+ 'price':{
+ 'price': item.product_id._get_website_price_exclude_tax(),
+ 'discount': item.product_id._get_website_disc(0),
+ 'price_discount': item.product_id._get_website_price_after_disc_and_tax()
+ },
+ 'stock': item.product_id.qty_stock_vendor,
+ 'weight': item.product_id.weight,
+ 'attributes': [x.name for x in item.product_id.product_template_attribute_value_ids],
+ 'manufacture': item.product_id.api_manufacture(item.product_id)
+ }
+ return data \ No newline at end of file
diff --git a/indoteknik_api/models/product_product.py b/indoteknik_api/models/product_product.py
index 284900de..8c60ce43 100644
--- a/indoteknik_api/models/product_product.py
+++ b/indoteknik_api/models/product_product.py
@@ -42,6 +42,14 @@ class ProductProduct(models.Model):
}
return {}
+ def _is_have_flashsale(self):
+ active_flashsale = self.env['product.pricelist'].get_active_flash_sale()
+ for pricelist in active_flashsale:
+ found_product = self.env['product.pricelist.item'].search([('pricelist_id', '=', pricelist.id), ('product_id', '=', self.id)])
+ if found_product:
+ return True
+ return False
+
def _get_website_price_include_tax(self):
default_pricelist_id = int(1)
@@ -104,3 +112,36 @@ class ProductProduct(models.Model):
price_after_disc = self._get_website_price_after_disc_and_tax()
res = price_after_disc * default_percent_tax / 100
return math.floor(res)
+
+ def _get_flashsale_price(self):
+ # must get active pricelist
+ active_flash_sale = self.env['product.pricelist'].get_active_flash_sale()
+ flashsale_id = 0
+ flashsale_name = ''
+ # loop pricelist items
+ base_price = discount = price_flashsale = 0
+ for pricelist in active_flash_sale:
+ flashsale_id = pricelist.id
+ flashsale_name = pricelist.name
+ query = [
+ ('pricelist_id', '=', pricelist.id),
+ ('product_id', '=', self.id),
+ ]
+ pricelist_items = self.env['product.pricelist.item'].search(query, limit=1)
+ for item in pricelist_items:
+ base_price = self._get_website_price_exclude_tax()
+ if item.price_discount > 0:
+ discount = item.price_discount
+ # base_item = self.env['product.pricelist.item'].search([('pricelist_id', '=', item.base_pricelist_id.id), ('product_id', '=', self.id)], limit=1)
+ price_flashsale = base_price - (base_price * discount // 100)
+ elif item.fixed_price > 0:
+ price_flashsale = item.fixed_price # ask darren for include or exclude
+ discount = (base_price - price_flashsale) // base_price * 100
+ data = {
+ 'flashsale_id': flashsale_id,
+ 'flashsale_name': flashsale_name,
+ 'flashsale_base_price': base_price,
+ 'flashsale_discount': discount,
+ 'flashsale_price': price_flashsale
+ }
+ return data
diff --git a/indoteknik_custom/models/apache_solr.py b/indoteknik_custom/models/apache_solr.py
index 3ed431ef..0a403b6b 100644
--- a/indoteknik_custom/models/apache_solr.py
+++ b/indoteknik_custom/models/apache_solr.py
@@ -13,9 +13,10 @@ class ApacheSolr(models.Model):
_order = 'id desc'
def _sync_product_to_solr(self, limit = 500):
+ # _solr = pysolr.Solr('http://10.148.0.5:8983/solr/product/', always_commit=True, timeout=30)
+ _solr = pysolr.Solr('http://192.168.23.5:8983/solr/product/', always_commit=True, timeout=30)
start_time = time.time()
_logger.info('run sync to solr...')
- solr = pysolr.Solr('http://10.148.0.5:8983/solr/product/', always_commit=True, timeout=30)
query = ["&","&",("type","=","product"),("active","=",True),"|",("solr_flag","=",0),("solr_flag","=",2)]
templates = self.env['product.template'].search(query, limit=limit)
@@ -27,6 +28,7 @@ class ApacheSolr(models.Model):
variants_name = variants_code = ''
if template.product_variant_count > 1:
for variant in template.product_variant_ids:
+ flashsale_data = variant._get_flashsale_price()
if price_excl_after_disc == 0:
price_excl = variant._get_website_price_exclude_tax()
price_excl_after_disc = variant._get_website_price_after_disc_and_tax()
@@ -49,6 +51,7 @@ class ApacheSolr(models.Model):
discount = template.product_variant_id._get_website_disc(0)
price_excl_after_disc = template.product_variant_id._get_website_price_after_disc_and_tax()
tax = template.product_variant_id._get_website_tax()
+ flashsale_data = template.product_variant_id._get_flashsale_price()
category_id = ''
category_name = ''
@@ -82,13 +85,18 @@ class ApacheSolr(models.Model):
'variants_name_t': variants_name,
'variants_code_t': variants_code,
'search_rank_i': template.search_rank,
- 'search_rank_weekly_i': template.search_rank_weekly
+ 'search_rank_weekly_i': template.search_rank_weekly,
+ 'flashsale_id_i': flashsale_data['flashsale_id'] or 0,
+ 'flashsale_name_s': flashsale_data['flashsale_name'] or '',
+ 'flashsale_base_price_f': flashsale_data['flashsale_base_price'] or 0,
+ 'flashsale_discount_f': flashsale_data['flashsale_discount'] or 0,
+ 'flashsale_price_f': flashsale_data['flashsale_price'] or 0,
}
documents.append(document)
template.solr_flag = 1
# add counter for monitoring
_logger.info('[SYNC_PRODUCT_TO_SOLR] %s/%i' % (counter, limit))
_logger.info('[SYNC_PRODUCT_TO_SOLR] Success add to solr product %s' % template.id)
- solr.add(documents)
+ _solr.add(documents)
end_time = time.time()
_logger.info("[SYNC_PRODUCT_TO_SOLR] Finish task add to solr. Time taken: {:.6f} seconds".format(end_time - start_time))