diff --git a/addons/website/models/website.py b/addons/website/models/website.py index dbb09d504d3..fb47298463b 100644 --- a/addons/website/models/website.py +++ b/addons/website/models/website.py @@ -129,6 +129,10 @@ class website(osv.osv): }) } + _defaults = { + 'company_id': lambda self,cr,uid,c: self.pool['ir.model.data'].xmlid_to_res_id(cr, openerp.SUPERUSER_ID, 'base.public_user'), + } + # cf. Wizard hack in website_views.xml def noop(self, *args, **kwargs): pass @@ -339,8 +343,7 @@ class website(osv.osv): """ router = request.httprequest.app.get_db_router(request.db) # Force enumeration to be performed as public user - # TODO: use website.user_id instead - uid = self.pool['ir.model.data'].xmlid_to_res_id(request.cr, openerp.SUPERUSER_ID, 'base.public_user') + uid = request.website.user_id.id url_list = [] for rule in router.iter_rules(): if not self.rule_is_enumerable(rule): diff --git a/addons/website/views/website_templates.xml b/addons/website/views/website_templates.xml index 6025e09ee5b..42c0ad46dbb 100644 --- a/addons/website/views/website_templates.xml +++ b/addons/website/views/website_templates.xml @@ -257,7 +257,7 @@ - + diff --git a/addons/website_event_sale/models/website.py b/addons/website_event_sale/models/website.py index 84315b52a77..83c653a3999 100644 --- a/addons/website_event_sale/models/website.py +++ b/addons/website_event_sale/models/website.py @@ -5,6 +5,6 @@ from openerp.osv import orm class Website(orm.Model): _inherit = 'website' - def sale_product_domain(self): + def sale_product_domain(self, cr, uid, ids, context=None): # remove product event from the website content grid and list view (not removed in detail view) - return ['&'] + super(Website, self).sale_product_domain() + [('event_ok', '=', False)] + return ['&'] + super(Website, self).sale_product_domain(cr, uid, ids, context=context) + [('event_ok', '=', False)] diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index 504789ead1b..6e5520f08a4 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -104,8 +104,7 @@ class website_sale(http.Controller): pricelist = partner.property_product_pricelist return pricelist - @http.route([ - '/shop/', + @http.route(['/shop/', '/shop/page//', '/shop/category//', '/shop/category//page//' @@ -125,6 +124,8 @@ class website_sale(http.Controller): attrib_set = set(attrib_values) keep = QueryURL('/shop', category=category and category.id, search=search, attrib=attrib_set) + if not context.get('pricelist'): + context['pricelist'] = int(self.get_pricelist()) product_obj = pool.get('product.template') product_count = product_obj.search_count(cr, uid, domain, context=context) pager = request.website.pager(url="/shop/", total=product_count, page=page, step=PPG, scope=7, url_args=post) @@ -166,6 +167,7 @@ class website_sale(http.Controller): @http.route(['/shop/product//'], type='http', auth="public", website=True, multilang=True) def product(self, product, category='', search='', **kwargs): cr, uid, context, pool = request.cr, request.uid, request.context, request.registry + category_obj = pool['product.public.category'] if category: category = category_obj.browse(request.cr, request.uid, int(category), context=request.context) @@ -175,11 +177,14 @@ class website_sale(http.Controller): keep = QueryURL('/shop', category=category and category.id, search=search, attrib=attrib_set) - category_obj = pool['product.public.category'] category_ids = category_obj.search(cr, uid, [], context=context) category_list = category_obj.name_get(cr, uid, category_ids, context=context) category_list = sorted(category_list, key=lambda category: category[1]) + if not context.get('pricelist'): + context['pricelist'] = int(self.get_pricelist()) + product = request.registry.get('product.template').browse(request.cr, request.uid, int(product), context=context) + values = { 'search': search, 'category': category, @@ -216,37 +221,6 @@ class website_sale(http.Controller): return request.redirect("/shop/product/%s/?enable_editor=1" % product.product_tmpl_id.id) - @http.route(['/shop/mycart/'], type='http', auth="public", website=True, multilang=True) - def mycart(self, **post): - cr, uid, context = request.cr, request.uid, request.context - prod_obj = request.registry.get('product.product') - - # must have a draft sale order with lines at this point, otherwise reset - order = self.get_order() - if order and order.state != 'draft': - request.registry['website'].sale_reset_order(cr, uid, context=context) - return request.redirect('/shop/') - - self.get_pricelist() - - suggested_ids = [] - product_ids = [] - if order: - for line in order.order_line: - suggested_ids += [p.id for p in line.product_id and line.product_id.accessory_product_ids or []] - product_ids.append(line.product_id.id) - suggested_ids = list(set(suggested_ids) - set(product_ids)) - if suggested_ids: - suggested_ids = prod_obj.search(cr, uid, [('id', 'in', suggested_ids)], context=context) - - # select 3 random products - suggested_products = [] - while len(suggested_products) < 3 and suggested_ids: - index = random.randrange(0, len(suggested_ids)) - suggested_products.append(suggested_ids.pop(index)) - - context = dict(context or {}, pricelist=request.registry['website'].ecommerce_get_pricelist_id(cr, uid, None, context=context)) - @http.route(['/shop/cart'], type='http', auth="public", website=True, multilang=True) def cart(self, **post): order = request.website.sale_get_order() @@ -255,20 +229,26 @@ class website_sale(http.Controller): 'suggested_products': [], } if order: - values['suggested_products'] = order._cart_accessories() + if not request.context.get('pricelist'): + request.context['pricelist'] = order.pricelist_id.id + values['suggested_products'] = order._cart_accessories(context=request.context) return request.website.render("website_sale.cart", values) @http.route(['/shop/cart/update'], type='http', auth="public", methods=['POST'], website=True, multilang=True) - def cart_update(self, product_id, add_qty=None, set_qty=None, **kw): + def cart_update(self, product_id, add_qty=0, set_qty=0, **kw): cr, uid, context = request.cr, request.uid, request.context - request.website.sale_get_order(force_create=1)._cart_update(product_id=product_id, add_qty=add_qty, set_qty=set_qty) + request.website.sale_get_order(force_create=1)._cart_update(product_id=int(product_id), add_qty=add_qty, set_qty=set_qty) return request.redirect("/shop/cart") - @http.route(['/shop/cart/update_json'], type='json', auth="public", website=True, multilang=True) + @http.route(['/shop/cart/update_json'], type='json', auth="public", methods=['POST'], website=True, multilang=True) def cart_update_json(self, product_id, add_qty=None, set_qty=None): order = request.website.sale_get_order(force_create=1) quantity = order._cart_update(product_id=product_id, add_qty=add_qty, set_qty=set_qty) - return request.website._render("website_sale.total", {'website_sale_order': order}) # FIXME good template + return { + 'quantity': quantity, + 'cart_quantity': order.cart_quantity, + 'website_sale.total': request.website._render("website_sale.total", {'website_sale_order': order}) # FIXME good template + } #------------------------------------------------------ # Checkout @@ -384,13 +364,12 @@ class website_sale(http.Controller): billing_info['parent_id'] = company_id partner_id = None - public_id = request.registry['website'].get_public_user(cr, uid, context) - if request.uid != public_id: + if request.uid != request.website.user_id.id: partner_id = orm_user.browse(cr, SUPERUSER_ID, uid, context=context).partner_id.id elif order.partner_id: domain = [("active", "=", False), ("partner_id", "=", order.partner_id.id)] user_ids = request.registry['res.users'].search(cr, SUPERUSER_ID, domain, context=context) - if not user_ids or public_id not in user_ids: + if not user_ids or request.website.user_id.id not in user_ids: partner_id = order.partner_id.id if partner_id: diff --git a/addons/website_sale/models/sale_order.py b/addons/website_sale/models/sale_order.py index 68425fb9dfe..5e2df03e5cf 100644 --- a/addons/website_sale/models/sale_order.py +++ b/addons/website_sale/models/sale_order.py @@ -16,12 +16,19 @@ class payment_transaction(orm.Model): class sale_order(osv.Model): _inherit = "sale.order" + def _cart_qty(self, cr, uid, ids, field_name, arg, context=None): + res = dict(); + for order in self.browse(cr, uid, ids, context=context): + res[order.id] = int(sum(l.product_uom_qty for l in (order.website_order_line or []))) + return res + _columns = { 'website_order_line': fields.one2many( 'sale.order.line', 'order_id', string='Order Lines displayed on Website', readonly=True, help='Order Lines to be displayed on the website. They should not be used for computation purpose.', ), + 'cart_quantity': fields.function(_cart_qty, type='integer', string='Main Menu'), } def _get_website_data(self, cr, uid, order, context): @@ -30,11 +37,6 @@ class sale_order(osv.Model): 'order': order } - # TODO make a function field instead - def _cart_qty(self, cr, uid, ids, context=None): - for order in self.browse(cr, uid, ids, context=context): - return int(sum(l.product_uom_qty for l in (order.website_order_line or []))) - def _cart_find_product_line(self, cr, uid, ids, product_id=None, context=None): for so in self.browse(cr, uid, ids, context=context): line_id = None @@ -43,7 +45,7 @@ class sale_order(osv.Model): line_id = line_ids[0] return line_id - def _cart_update(self, cr, uid, ids, product_id=None, add_qty=None, set_qty=None, context=None): + def _cart_update(self, cr, uid, ids, product_id=None, add_qty=0, set_qty=0, context=None): """ Add or set product quantity, add_qty can be negative """ for so in self.browse(cr, uid, ids, context=context): sol = self.pool.get('sale.order.line') @@ -60,17 +62,15 @@ class sale_order(osv.Model): context=context )['value'] values['name'] = "%s: %s" % (product.name, product.variants) if product.variants else product.name - # Maybe it's better to do this ? create and then link ? - #order_line_id = sol.create(cr, SUPERUSER_ID, values, context=context) - #self.write(cr, SUPERUSER_ID, [order.id], {'order_line': [(4, order_line_id)]}, context=context) - so.write({'order_line': (0, 0, values)}) - line_id = so._cart_find_product_line(product_id) + values['product_id'] = product_id + values['order_id'] = so.id + line_id = sol.create(cr, SUPERUSER_ID, values, context=context) # compute new quantity if set_qty: quantity = set_qty else: - quantity = line_id.product_uom_qty + add_qty + quantity = sol.browse(cr, SUPERUSER_ID, line_id, context=context).product_uom_qty + add_qty # Remove zero of negative lines if quantity <= 0: @@ -91,9 +91,9 @@ class sale_order(osv.Model): def _cart_accessories(self, cr, uid, ids, context=None): for order in self.browse(cr, uid, ids, context=context): - s = set(j for l in (order.website_order_line or []) for j in (l.product_id.accessory_product_ids or [])) + s = set(j.id for l in (order.website_order_line or []) for j in (l.product_id.accessory_product_ids or [])) product_ids = random.sample(s, min(len(s),3)) - return self.pool['product.product'].browse(cr, uid, product_ids, context=context) + return self.pool['product.template'].browse(cr, uid, product_ids, context=context) class website(orm.Model): _inherit = 'website' @@ -135,10 +135,10 @@ class website(orm.Model): if pricelist_ids: pricelist_id = pricelist_ids[0] values = {'pricelist_id': pricelist_id} - values.update(order.onchange_pricelist_id(pricelist_id, None)['value']) - order.write(values) - for line in order.order_line: - sale_order._cart_update(cr, uid, order.product_id, add_qty=0) + values.update(sale_order.onchange_pricelist_id(pricelist_id, None)['value']) + sale_order.write(values) + for line in sale_order.order_line: + sale_order._cart_update(cr, uid, sale_order.product_id, add_qty=0) # check for change of partner_id ie after signup if sale_order.partner_id.id != partner.id: @@ -163,11 +163,4 @@ class website(orm.Model): 'sale_transaction_id': False, }) - def preprocess_request(self, cr, uid, ids, request, context=None): - request.context.update({ - 'sale_order': self.sale_get_order(cr, uid, ids, context=context), - #'website_sale_transaction': self.ecommerce_get_current_transaction(cr, uid, context=context) - }) - return super(website, self).preprocess_request(cr, uid, ids, request, context=None) - # vim:et: diff --git a/addons/website_sale/static/src/js/website_sale.js b/addons/website_sale/static/src/js/website_sale.js index 8de601c1b35..67b38bce1f9 100644 --- a/addons/website_sale/static/src/js/website_sale.js +++ b/addons/website_sale/static/src/js/website_sale.js @@ -24,14 +24,16 @@ $(document).ready(function () { var $input = $(this); var value = parseInt($input.val(), 10); if (isNaN(value)) value = 0; - openerp.jsonRpc("/shop/set_cart_json/", 'call', {'order_line_id': $input.data('id'), 'set_number': value}) + openerp.jsonRpc("/shop/cart/update_json", 'call', { + 'product_id': parseInt($input.data('id'),10), + 'set_qty': value}) .then(function (data) { - if (!data) { + if (!data.quantity) { location.reload(); return; } - set_my_cart_quantity(data[1]); - $input.val(data[0]); + set_my_cart_quantity(data.cart_quantity); + $input.val(data.quantity); }); }); @@ -39,25 +41,9 @@ $(document).ready(function () { $('.oe_website_sale a.js_add_cart_json').on('click', function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); - var href = $link.attr("href"); - - var add_cart = href.match(/add_cart\/([0-9]+)/); - var product_id = add_cart && +add_cart[1] || false; - - var change_cart = href.match(/change_cart\/([0-9]+)/); - var order_line_id = change_cart && +change_cart[1] || false; - openerp.jsonRpc("/shop/add_cart_json/", 'call', { - 'product_id': product_id, - 'order_line_id': order_line_id, - 'remove': $link.is('[href*="remove"]')}) - .then(function (data) { - if (!data[0]) { - location.reload(); - } - set_my_cart_quantity(data[1]); - $link.parents(".input-group:first").find(".js_quantity").val(data[0]); - $('#cart_total').replaceWith(data[3]); - }); + var $input = $link.parent().parent().find("input"); + $input.val(($link.has(".fa-minus").length ? -1 : 1) + parseFloat($input.val(),10)); + $input.change(); return false; }); diff --git a/addons/website_sale/views/templates.xml b/addons/website_sale/views/templates.xml index 1b0e5c84654..979b29c9e93 100644 --- a/addons/website_sale/views/templates.xml +++ b/addons/website_sale/views/templates.xml @@ -19,17 +19,18 @@