main.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. import werkzeug
  3. from openerp import SUPERUSER_ID
  4. from openerp import http
  5. from openerp.http import request
  6. from openerp.tools.translate import _
  7. from openerp.addons.website.models.website import slug
  8. PPG = 20 # Products Per Page
  9. PPR = 4 # Products Per Row
  10. class web_site_sale_extend(http.Controller):
  11. def get_pricelist(self):
  12. return get_pricelist()
  13. def get_attribute_value_ids_ext(self, product):
  14. sale_order_id = request.session.get('sale_last_order_id')
  15. cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
  16. currency_obj = pool['res.currency']
  17. attribute_value_ids = []
  18. if request.website.pricelist_id.id != context['pricelist']:
  19. website_currency_id = request.website.currency_id.id
  20. currency_id = self.get_pricelist().currency_id.id
  21. for p in product.product_variant_ids:
  22. price = currency_obj.compute(cr, uid, website_currency_id, currency_id, p.lst_price)
  23. attribute_value_ids.append([p.id, map(int, p.attribute_value_ids), p.price, price, p.qty_available])
  24. else:
  25. attribute_value_ids = [[p.id, map(int, p.attribute_value_ids), p.price, p.lst_price, p.qty_available] for p in product.product_variant_ids]
  26. return attribute_value_ids
  27. @http.route(['/shop/product/<model("product.template"):product>'], type='http', auth="public", website=True)
  28. def product(self, product, category='', search='', **kwargs):
  29. cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
  30. category_obj = pool['product.public.category']
  31. template_obj = pool['product.template']
  32. context.update(active_id=product.id)
  33. if category:
  34. category = category_obj.browse(cr, uid, int(category), context=context)
  35. attrib_list = request.httprequest.args.getlist('attrib')
  36. attrib_values = [map(int,v.split("-")) for v in attrib_list if v]
  37. attrib_set = set([v[1] for v in attrib_values])
  38. keep = QueryURL('/shop', category=category and category.id, search=search, attrib=attrib_list)
  39. category_ids = category_obj.search(cr, uid, [], context=context)
  40. category_list = category_obj.name_get(cr, uid, category_ids, context=context)
  41. category_list = sorted(category_list, key=lambda category: category[1])
  42. pricelist = self.get_pricelist()
  43. from_currency = pool.get('product.price.type')._get_field_currency(cr, uid, 'list_price', context)
  44. to_currency = pricelist.currency_id
  45. compute_currency = lambda price: pool['res.currency']._compute(cr, uid, from_currency, to_currency, price, context=context)
  46. if not context.get('pricelist'):
  47. context['pricelist'] = int(self.get_pricelist())
  48. product = template_obj.browse(cr, uid, int(product), context=context)
  49. values = {
  50. 'search': search,
  51. 'category': category,
  52. 'pricelist': pricelist,
  53. 'attrib_values': attrib_values,
  54. 'compute_currency': compute_currency,
  55. 'attrib_set': attrib_set,
  56. 'keep': keep,
  57. 'category_list': category_list,
  58. 'main_object': product,
  59. 'product': product,
  60. 'get_attribute_value_ids': self.get_attribute_value_ids_ext
  61. }
  62. return request.website.render("website_sale.product", values)
  63. @http.route(['/shop/cart/get_added_qty_product_json'], type='json', auth="public", methods=['POST'], website=True)
  64. def cart_stock_for_product_json(self, product_id):
  65. cr, uid, context = request.cr, request.uid, request.context
  66. qty = request.website.added_to_cart_product_qty_variant(product_id)
  67. return qty;
  68. class QueryURL(object):
  69. def __init__(self, path='', **args):
  70. self.path = path
  71. self.args = args
  72. def __call__(self, path=None, **kw):
  73. if not path:
  74. path = self.path
  75. for k,v in self.args.items():
  76. kw.setdefault(k,v)
  77. l = []
  78. for k,v in kw.items():
  79. if v:
  80. if isinstance(v, list) or isinstance(v, set):
  81. l.append(werkzeug.url_encode([(k,i) for i in v]))
  82. else:
  83. l.append(werkzeug.url_encode([(k,v)]))
  84. if l:
  85. path += '?' + '&'.join(l)
  86. return path
  87. def get_pricelist():
  88. cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
  89. sale_order = context.get('sale_order')
  90. if sale_order:
  91. pricelist = sale_order.pricelist_id
  92. else:
  93. partner = pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context).partner_id
  94. pricelist = partner.property_product_pricelist
  95. return pricelist
  96. # vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4: