main.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. # -*- coding: utf-8 -*-
  2. # Part of AppJetty. See LICENSE file for full copyright and licensing details.
  3. import re
  4. import math
  5. from odoo import http, SUPERUSER_ID, fields
  6. from odoo.http import request
  7. from odoo.addons.http_routing.models.ir_http import slug
  8. from odoo.addons.website.controllers.main import QueryURL
  9. from odoo.addons.website_sale.controllers import main
  10. from odoo.addons.website_sale.controllers import main as main_shop
  11. from odoo.addons.website_sale.controllers.main import WebsiteSale
  12. from odoo.addons.website_sale.controllers.main import TableCompute
  13. class KingfisherProSliderSettings(http.Controller):
  14. @http.route(['/kingfisher_pro/pro_get_options'], type='json', auth="public", website=True)
  15. def get_slider_options(self):
  16. slider_options = []
  17. option = request.env['product.category.slider.config'].sudo().search(
  18. [('active', '=', True)], order="name asc")
  19. for record in option:
  20. slider_options.append({'id': record.id,
  21. 'name': record.name})
  22. return slider_options
  23. @http.route(['/kingfisher_pro/pro_get_dynamic_slider'], type='http', auth='public', website=True)
  24. def get_dynamic_slider(self, **post):
  25. uid, context, pool = request.uid, dict(request.context), request.env
  26. if post.get('slider-type'):
  27. slider_header = request.env['product.category.slider.config'].sudo().search(
  28. [('id', '=', int(post.get('slider-type')))])
  29. if not context.get('pricelist'):
  30. pricelist = request.website.get_current_pricelist()
  31. context = dict(request.context, pricelist=int(pricelist))
  32. else:
  33. pricelist = pool.get('product.pricelist').browse(
  34. context['pricelist'])
  35. context.update({'pricelist': pricelist.id})
  36. values = {
  37. 'slider_header': slider_header
  38. }
  39. if slider_header.prod_cat_type == 'product':
  40. values.update(
  41. {'slider_details': slider_header.collections_product})
  42. if slider_header.prod_cat_type == 'category':
  43. values.update(
  44. {'slider_details': slider_header.collections_category})
  45. from_currency = pool['res.users'].sudo().browse(
  46. uid).company_id.currency_id
  47. to_currency = pricelist.currency_id
  48. def compute_currency(price): return pool['res.currency']._convert(
  49. price, from_currency, to_currency, fields.Date.today())
  50. values.update({'slider_type': slider_header.prod_cat_type,
  51. 'compute_currency': compute_currency, })
  52. return request.render("kingfisher_pro.kingfisher_pro_pro_cat_slider_view", values)
  53. @http.route(['/kingfisher_pro/pro_image_effect_config'], type='json', auth='public', website=True)
  54. def product_image_dynamic_slider(self, **post):
  55. slider_data = request.env['product.category.slider.config'].sudo().search(
  56. [('id', '=', int(post.get('slider_type')))])
  57. values = {
  58. 's_id': slider_data.prod_cat_type + str(slider_data.id),
  59. 'counts': slider_data.no_of_counts,
  60. 'auto_rotate': slider_data.auto_rotate,
  61. 'auto_play_time': slider_data.sliding_speed,
  62. }
  63. return values
  64. @http.route(['/kingfisher_pro/blog_get_options'], type='json', auth="public", website=True)
  65. def king_blog_get_slider_options(self):
  66. slider_options = []
  67. option = request.env['blog.slider.config'].sudo().search(
  68. [('active', '=', True)], order="name asc")
  69. for record in option:
  70. slider_options.append({'id': record.id,
  71. 'name': record.name})
  72. return slider_options
  73. @http.route(['/kingfisher_pro/blog_get_dynamic_slider'], type='http', auth='public', website=True)
  74. def king_blog_get_dynamic_slider(self, **post):
  75. if post.get('slider-type'):
  76. slider_header = request.env['blog.slider.config'].sudo().search(
  77. [('id', '=', int(post.get('slider-type')))])
  78. values = {
  79. 'slider_header': slider_header,
  80. 'blog_slider_details': slider_header.collections_blog_post,
  81. }
  82. return request.render("kingfisher_pro.kingfisher_pro_blog_slider_view", values)
  83. @http.route(['/kingfisher_pro/blog_image_effect_config'], type='json', auth='public', website=True)
  84. def king_blog_product_image_dynamic_slider(self, **post):
  85. slider_data = request.env['blog.slider.config'].sudo().search(
  86. [('id', '=', int(post.get('slider_type')))])
  87. values = {
  88. 's_id': slider_data.no_of_counts + '-' + str(slider_data.id),
  89. 'counts': slider_data.no_of_counts,
  90. 'auto_rotate': slider_data.auto_rotate,
  91. 'auto_play_time': slider_data.sliding_speed,
  92. }
  93. return values
  94. # Multi image gallery
  95. @http.route(['/kingfisher_pro/multi_image_effect_config'], type='json', auth="public", website=True)
  96. def get_multi_image_effect_config(self):
  97. cur_website = request.website
  98. values = {
  99. 'no_extra_options': cur_website.no_extra_options,
  100. 'theme_panel_position': cur_website.thumbnail_panel_position,
  101. 'interval_play': cur_website.interval_play,
  102. 'enable_disable_text': cur_website.enable_disable_text,
  103. 'color_opt_thumbnail': cur_website.color_opt_thumbnail,
  104. 'change_thumbnail_size': cur_website.change_thumbnail_size,
  105. 'thumb_height': cur_website.thumb_height,
  106. 'thumb_width': cur_website.thumb_width,
  107. }
  108. return values
  109. # For multi product slider
  110. @http.route(['/kingfisher_pro/product_multi_get_options'], type='json', auth="public", website=True)
  111. def product_multi_get_slider_options(self):
  112. slider_options = []
  113. option = request.env['multi.slider.config'].sudo().search(
  114. [('active', '=', True)], order="name asc")
  115. for record in option:
  116. slider_options.append({'id': record.id,
  117. 'name': record.name})
  118. return slider_options
  119. @http.route(['/kingfisher_pro/product_multi_get_dynamic_slider'], type='http', auth='public', website=True)
  120. def product_multi_get_dynamic_slider(self, **post):
  121. context, pool = dict(request.context), request.env
  122. if post.get('slider-type'):
  123. slider_header = request.env['multi.slider.config'].sudo().search(
  124. [('id', '=', int(post.get('slider-type')))])
  125. if not context.get('pricelist'):
  126. pricelist = request.website.get_current_pricelist()
  127. context = dict(request.context, pricelist=int(pricelist))
  128. else:
  129. pricelist = pool.get('product.pricelist').browse(
  130. context['pricelist'])
  131. context.update({'pricelist': pricelist.id})
  132. from_currency = pool['res.users'].sudo().browse(
  133. SUPERUSER_ID).company_id.currency_id
  134. to_currency = pricelist.currency_id
  135. def compute_currency(price): return pool['res.currency']._convert(
  136. price, from_currency, to_currency, fields.Date.today())
  137. values = {
  138. 'slider_details': slider_header,
  139. 'slider_header': slider_header,
  140. 'compute_currency': compute_currency,
  141. }
  142. return request.render("kingfisher_pro.kingfisher_pro_multi_cat_slider_view", values)
  143. @http.route(['/kingfisher_pro/product_multi_image_effect_config'], type='json', auth='public', website=True)
  144. def product_multi_product_image_dynamic_slider(self, **post):
  145. slider_data = request.env['multi.slider.config'].sudo().search(
  146. [('id', '=', int(post.get('slider_type')))])
  147. values = {
  148. 's_id': slider_data.no_of_collection + '-' + str(slider_data.id),
  149. 'counts': slider_data.no_of_collection,
  150. 'auto_rotate': slider_data.auto_rotate,
  151. 'auto_play_time': slider_data.sliding_speed,
  152. }
  153. return values
  154. class KingfisherProBrandSlider(WebsiteSale):
  155. @http.route(['/shop/pager_selection/<model("product.per.page.no"):pl_id>'], type='http', auth="public", website=True)
  156. def product_page_change(self, pl_id, **post):
  157. request.session['default_paging_no'] = pl_id.name
  158. main.PPG = pl_id.name
  159. return request.redirect('/shop' or request.httprequest.referrer)
  160. @http.route(['/shop',
  161. '/shop/page/<int:page>',
  162. '/shop/category/<model("product.public.category"):category>',
  163. '/shop/category/<model("product.public.category"):category>/page/<int:page>',
  164. '/shop/brands'],
  165. type='http',
  166. auth='public',
  167. website=True)
  168. def shop(self, page=0, category=None, brand=None, search='', ppg=False, **post):
  169. add_qty = int(post.get('add_qty', 1))
  170. if brand:
  171. req_ctx = request.context.copy()
  172. req_ctx.setdefault('brand_id', int(brand))
  173. request.context = req_ctx
  174. result = super(KingfisherProBrandSlider, self).shop(
  175. page=page, category=category, brand=brand, search=search, **post)
  176. # odoo11
  177. # for displaying after whishlist or add to cart button n product_detail page
  178. if request.env.get('product.attribute.category') != None:
  179. compare_tmpl_obj = request.env.ref(
  180. 'website_sale_comparison.product_add_to_compare')
  181. if compare_tmpl_obj and compare_tmpl_obj.priority != 20:
  182. compare_tmpl_obj.sudo().write({'priority': 20})
  183. sort_order = ""
  184. cat_id = []
  185. page_no = request.env['product.per.page.no'].sudo().search(
  186. [('set_default_check', '=', True)])
  187. if page_no:
  188. ppg = page_no.name
  189. else:
  190. ppg = main_shop.PPG
  191. product_tmp = []
  192. newproduct = []
  193. product_price = []
  194. # product template object
  195. product_obj = request.env['product.template']
  196. attrib_list = request.httprequest.args.getlist('attrib')
  197. attrib_values = [list(map(int, v.split("-")))
  198. for v in attrib_list if v]
  199. attributes_ids = set([v[0] for v in attrib_values])
  200. attrib_set = set([v[1] for v in attrib_values])
  201. domain = request.website.sale_product_domain()
  202. domain += self._get_search_domain(search, category, attrib_values)
  203. url = "/shop"
  204. keep = QueryURL('/shop', category=category and int(category), search=search,
  205. attrib=attrib_list, order=post.get('order'))
  206. pricelist_context, pricelist = self._get_pricelist_context()
  207. if post:
  208. request.session.update(post)
  209. if search:
  210. post["search"] = search
  211. if attrib_list:
  212. post['attrib'] = attrib_list
  213. prevurl = request.httprequest.referrer
  214. if prevurl:
  215. if not re.search('/shop', prevurl, re.IGNORECASE):
  216. request.session['tag'] = ""
  217. request.session['sort_id'] = ""
  218. request.session['sortid'] = ""
  219. request.session['pricerange'] = ""
  220. request.session['min1'] = ""
  221. request.session['max1'] = ""
  222. request.session['curr_category'] = ""
  223. session = request.session
  224. cate_for_price = None
  225. # for category filter
  226. if category:
  227. cate_for_price = int(category)
  228. category = request.env['product.public.category'].sudo().browse(
  229. int(category))
  230. url = "/shop/category/%s" % slug(category)
  231. if not category or not category.can_access_from_current_website():
  232. raise NotFound()
  233. if category != None:
  234. for ids in category:
  235. cat_id.append(ids.id)
  236. domain += ['|', ('public_categ_ids.id', 'in', cat_id),
  237. ('public_categ_ids.parent_id', 'in', cat_id)]
  238. # for tag filter
  239. if session.get('tag'):
  240. session_tag = session.get('tag')[0]
  241. tag = session_tag
  242. if tag:
  243. tag = request.env['biztech.product.tags'].sudo().browse(
  244. int(tag))
  245. domain += [('biztech_tag_ids', '=', int(tag))]
  246. request.session["tag"] = [tag.id, tag.name]
  247. # For Product Sorting
  248. if session.get('sort_id'):
  249. session_sort = session.get('sort_id')
  250. sort = session_sort
  251. sort_field = request.env['biztech.product.sortby'].sudo().browse(
  252. int(sort))
  253. request.session['product_sort_name'] = sort_field.name
  254. order_field = sort_field.sort_on.name
  255. order_type = sort_field.sort_type
  256. sort_order = '%s %s' % (order_field, order_type)
  257. if post.get("sort_id"):
  258. request.session["sortid"] = [
  259. sort, sort_order, sort_field.name, order_type]
  260. # For Price slider
  261. is_price_slider = request.env.ref(
  262. 'kingfisher_pro.kingfisher_pro_slider_layout')
  263. if is_price_slider:
  264. is_discount_hide = True if request.website.get_current_pricelist(
  265. ).discount_policy == 'with_discount' else False
  266. product_slider_ids = []
  267. asc_product_slider_ids = product_obj.search(
  268. domain, limit=1, order='list_price')
  269. desc_product_slider_ids = product_obj.search(
  270. domain, limit=1, order='list_price desc')
  271. if asc_product_slider_ids:
  272. # product_slider_ids.append(asc_product_slider_ids.website_price)
  273. product_slider_ids.append(
  274. asc_product_slider_ids.website_price if is_discount_hide else asc_product_slider_ids.list_price)
  275. if desc_product_slider_ids:
  276. # product_slider_ids.append(desc_product_slider_ids.website_price)
  277. product_slider_ids.append(
  278. desc_product_slider_ids.website_price if is_discount_hide else desc_product_slider_ids.list_price)
  279. if product_slider_ids:
  280. if post.get("range1") or post.get("range2") or not post.get("range1") or not post.get("range2"):
  281. range1 = min(product_slider_ids)
  282. range2 = max(product_slider_ids)
  283. result.qcontext['range1'] = math.floor(range1)
  284. result.qcontext['range2'] = math.ceil(range2)
  285. if request.session.get('pricerange'):
  286. if cate_for_price and request.session.get('curr_category') and request.session.get('curr_category') != int(cate_for_price):
  287. request.session["min1"] = math.floor(range1)
  288. request.session["max1"] = math.ceil(range2)
  289. if session.get("min1") and session["min1"]:
  290. post["min1"] = session["min1"]
  291. if session.get("max1") and session["max1"]:
  292. post["max1"] = session["max1"]
  293. if range1:
  294. post["range1"] = range1
  295. if range2:
  296. post["range2"] = range2
  297. if range1 == range2:
  298. post['range1'] = 0.0
  299. if request.session.get('min1') or request.session.get('max1'):
  300. if request.session.get('min1'):
  301. if request.session['min1'] != None:
  302. # domain += [('list_price', '>=', request.session.get('min1')), ('list_price', '<=', request.session.get('max1'))]
  303. # ========== for hide list-website price diffrence ====================
  304. if is_discount_hide:
  305. price_product_list = []
  306. product_withprice = product_obj.search(domain)
  307. for prod_id in product_withprice:
  308. if prod_id.website_price >= float(request.session['min1']) and prod_id.website_price <= float(request.session['max1']):
  309. price_product_list.append(prod_id.id)
  310. if price_product_list:
  311. domain += [('id', 'in',
  312. price_product_list)]
  313. else:
  314. domain += [('id', 'in', [])]
  315. else:
  316. domain += [('list_price', '>=', request.session.get('min1')),
  317. ('list_price', '<=', request.session.get('max1'))]
  318. # ==============================
  319. request.session["pricerange"] = str(
  320. request.session['min1'])+"-To-"+str(request.session['max1'])
  321. if session.get('min1') and session['min1']:
  322. result.qcontext['min1'] = session["min1"]
  323. result.qcontext['max1'] = session["max1"]
  324. if cate_for_price:
  325. request.session['curr_category'] = int(cate_for_price)
  326. if request.session.get('default_paging_no'):
  327. ppg = int(request.session.get('default_paging_no'))
  328. product_count = product_obj.search_count(domain)
  329. pager = request.website.pager(
  330. url=url, total=product_count, page=page, step=ppg, scope=7, url_args=post)
  331. products = product_obj.search(
  332. domain, limit=ppg, offset=pager['offset'], order=sort_order)
  333. compute_currency = self._get_compute_currency(pricelist, products[:1])
  334. result.qcontext.update({'product_count': product_count,
  335. 'products': products,
  336. 'category': category,
  337. 'pager': pager,
  338. 'add_qty': add_qty,
  339. 'compute_currency': compute_currency,
  340. 'keep': keep,
  341. 'search': search,
  342. 'bins': TableCompute().process(products, ppg)})
  343. result.qcontext['brand'] = brand
  344. result.qcontext['domain'] = domain
  345. result.qcontext['brand_obj'] = request.env['product.brands'].sudo().search([
  346. ('id', '=', brand)])
  347. return result
  348. @http.route()
  349. def cart_update_json(self, product_id, line_id=None, add_qty=None, set_qty=None, display=True):
  350. result = super(KingfisherProBrandSlider, self).cart_update_json(
  351. product_id, line_id, add_qty, set_qty, display)
  352. order = request.website.sale_get_order()
  353. result.update({'kingfisher_pro.hover_total': request.env['ir.ui.view'].render_template("kingfisher_pro.hover_total", {
  354. 'website_sale_order': order})
  355. })
  356. return result
  357. @http.route(['/king_pro/get_brand_slider'], type='http', auth='public', website=True)
  358. def get_brand_slider(self, **post):
  359. keep = QueryURL('/king_pro/get_brand_slider', brand_id=[])
  360. value = {
  361. 'website_brands': False,
  362. 'brand_header': False,
  363. 'keep': keep
  364. }
  365. if post.get('product_count'):
  366. brand_data = request.env['product.brands'].sudo().search(
  367. [], limit=int(post.get('product_count')))
  368. if brand_data:
  369. value['website_brands'] = brand_data
  370. if post.get('product_label'):
  371. value['brand_header'] = post.get('product_label')
  372. return request.render("kingfisher_pro.kingfisher_pro_brand_slider_view", value)
  373. @http.route(['/kingfisher_pro/removeattribute'], type='json', auth='public', website=True)
  374. def remove_selected_attribute(self, **post):
  375. if post.get("attr_remove"):
  376. remove = post.get("attr_remove")
  377. if remove == "pricerange":
  378. del request.session['min1']
  379. del request.session['max1']
  380. request.session[remove] = ''
  381. return True
  382. elif remove == "sortid":
  383. request.session[remove] = ''
  384. request.session["sort_id"] = ''
  385. return True
  386. elif remove == "tag":
  387. request.session[remove] = ''
  388. return True