From 204a62499a4337cecc8050801806e6b8ebda1665 Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Tue, 24 Jun 2014 15:11:53 +0200 Subject: [PATCH] [IMP] website_sale_options --- addons/website/models/ir_http.py | 2 +- addons/website_sale/controllers/main.py | 45 ++++-------- addons/website_sale/data/demo.xml | 52 -------------- addons/website_sale/models/sale_order.py | 49 ++----------- addons/website_sale/views/templates.xml | 68 ++---------------- addons/website_sale/views/views.xml | 1 - addons/website_sale_options/__init__.py | 2 + addons/website_sale_options/__openerp__.py | 22 ++++++ .../controllers/__init__.py | 1 + .../website_sale_options/controllers/main.py | 46 ++++++++++++ addons/website_sale_options/data/demo.xml | 58 +++++++++++++++ .../website_sale_options/models/__init__.py | 1 + .../website_sale_options/models/sale_order.py | 72 +++++++++++++++++++ .../static/src/js/website_sale.js | 1 + .../website_sale_options/views/templates.xml | 56 +++++++++++++++ addons/website_sale_options/views/views.xml | 17 +++++ 16 files changed, 300 insertions(+), 193 deletions(-) create mode 100644 addons/website_sale_options/__init__.py create mode 100644 addons/website_sale_options/__openerp__.py create mode 100644 addons/website_sale_options/controllers/__init__.py create mode 100644 addons/website_sale_options/controllers/main.py create mode 100644 addons/website_sale_options/data/demo.xml create mode 100644 addons/website_sale_options/models/__init__.py create mode 100644 addons/website_sale_options/models/sale_order.py create mode 100644 addons/website_sale_options/static/src/js/website_sale.js create mode 100644 addons/website_sale_options/views/templates.xml create mode 100644 addons/website_sale_options/views/views.xml diff --git a/addons/website/models/ir_http.py b/addons/website/models/ir_http.py index 6ffb267c214..f3e90da7df4 100644 --- a/addons/website/models/ir_http.py +++ b/addons/website/models/ir_http.py @@ -62,7 +62,7 @@ class ir_http(orm.AbstractModel): self.geo_ip_resolver = GeoIP.open('/usr/share/GeoIP/GeoIP.dat', GeoIP.GEOIP_STANDARD) except ImportError: self.geo_ip_resolver = False - if self.geo_ip_resolver: + if self.geo_ip_resolver and request.httprequest.remote_addr: record = self.geo_ip_resolver.record_by_addr(request.httprequest.remote_addr) or {} request.session['geoip'] = record diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index d0f021e170f..52d4c15df38 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -98,17 +98,17 @@ class QueryURL(object): return path -class website_sale(http.Controller): +def get_pricelist(): + cr, uid, context, pool = request.cr, request.uid, request.context, request.registry + sale_order = context.get('sale_order') + if sale_order: + pricelist = sale_order.pricelist_id + else: + partner = pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context).partner_id + pricelist = partner.property_product_pricelist + return pricelist - def get_pricelist(self): - cr, uid, context, pool = request.cr, request.uid, request.context, request.registry - sale_order = context.get('sale_order') - if sale_order: - pricelist = sale_order.pricelist_id - else: - partner = pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context).partner_id - pricelist = partner.property_product_pricelist - return pricelist +class website_sale(http.Controller): @http.route(['/shop', '/shop/page/', @@ -215,11 +215,6 @@ class website_sale(http.Controller): context['pricelist'] = int(self.get_pricelist()) product = template_obj.browse(cr, uid, int(product), context=context) - optional_product_ids = [] - for p in product.optional_product_ids: - ctx = dict(context, active_id=p.id) - optional_product_ids.append(template_obj.browse(cr, uid, p.id, context=ctx)) - attribute_value_ids = [] if request.website.company_pricelist_id.id != context['pricelist']: company_currency_id = request.website.company_currency_id.id @@ -240,7 +235,6 @@ class website_sale(http.Controller): 'category_list': category_list, 'main_object': product, 'product': product, - 'optional_product_ids': optional_product_ids, 'attribute_value_ids': attribute_value_ids } return request.website.render("website_sale.product", values) @@ -282,24 +276,13 @@ class website_sale(http.Controller): @http.route(['/shop/cart/update'], type='http', auth="public", methods=['POST'], website=True) def cart_update(self, product_id, add_qty=1, set_qty=0, goto_shop=None, **kw): cr, uid, context, pool = request.cr, request.uid, request.context, request.registry - order = request.website.sale_get_order(force_create=1) + prod_obj = pool['product.product'] - optional_product_ids = [] - for k, v in kw.items(): - if "optional-product-" in k and int(kw.get(k.replace("product", "quantity"))): - optional_product_ids.append(int(v)) - if add_qty or set_qty: - line_id, quantity, option_ids = order._cart_update(product_id=int(product_id), - add_qty=int(add_qty), set_qty=int(set_qty), - optional_product_ids=optional_product_ids) - # options have all time the same quantity - for option_id in optional_product_ids: - order._cart_update(product_id=option_id, set_qty=quantity, linked_line_id=line_id) + order = request.website.sale_get_order(force_create=1) + line_id, quantity = order._cart_update(product_id=int(product_id), add_qty=int(add_qty), set_qty=int(set_qty)) if goto_shop: - attrib_list = request.httprequest.args.getlist('attrib') - keep = QueryURL('/shop', category=kw.get('category'), search=kw.get('search'), attrib=attrib_list) - return request.redirect( keep() ) + return request.redirect("/shop/product/%s" % slug(prod_obj.browse(cr, uid, product_id).product_tmpl_id)) else: return request.redirect("/shop/cart") diff --git a/addons/website_sale/data/demo.xml b/addons/website_sale/data/demo.xml index ca33ec8f8f0..952497bd547 100644 --- a/addons/website_sale/data/demo.xml +++ b/addons/website_sale/data/demo.xml @@ -6,58 +6,6 @@ - - - - Duration - hidden - - - 1 year - - - - 2 year - - - - - Warranty - 20.0 - 1000 - - service - - - Warranty, issued to the purchaser of an article by its manufacturer, promising to repair or replace it if necessary within a specified period of time. - W0001 - - - - W0002 - - - - - - - - - - - - - - - - - - - 18.00 - - - - color diff --git a/addons/website_sale/models/sale_order.py b/addons/website_sale/models/sale_order.py index 0715c01ba45..695d60a4f6a 100644 --- a/addons/website_sale/models/sale_order.py +++ b/addons/website_sale/models/sale_order.py @@ -51,20 +51,14 @@ class sale_order(osv.Model): 'order': order } - def _cart_find_product_line(self, cr, uid, ids, product_id=None, line_id=None, linked_line_id=None, optional_product_ids=None, context=None): + def _cart_find_product_line(self, cr, uid, ids, product_id=None, line_id=None, context=None): for so in self.browse(cr, uid, ids, context=context): + order_line_id = None domain = [('order_id', '=', so.id), ('product_id', '=', product_id)] if line_id: domain += [('id', '=', line_id)] - domain += linked_line_id and [('linked_line_id', '=', linked_line_id)] or [('linked_line_id', '=', False)] - if not line_id: - if optional_product_ids: - domain += [('option_line_ids.product_id', '=', pid) for pid in optional_product_ids] - else: - domain += [('option_line_ids', '=', False)] - order_line_id = None order_line_ids = self.pool.get('sale.order.line').search(cr, SUPERUSER_ID, domain, context=context) if order_line_ids: order_line_id = order_line_ids[0] @@ -99,15 +93,11 @@ class sale_order(osv.Model): quantity = 0 for so in self.browse(cr, uid, ids, context=context): - line_id = so._cart_find_product_line(product_id, line_id, linked_line_id, optional_product_ids, context=context) + line_id = so._cart_find_product_line(product_id, line_id, context=context) # Create line if no line with product_id can be located if not line_id: values = self._website_product_id_change(cr, uid, ids, so.id, product_id, context=context) - if linked_line_id and linked_line_id in map(int,so.order_line): - values["linked_line_id"] = linked_line_id - linked = sol.browse(cr, SUPERUSER_ID, linked_line_id, context=context) - values["name"] = _("%s\nLinked to: %s") % (values["name"], linked.product_id.name_get()[0][1]) line_id = sol.create(cr, SUPERUSER_ID, values, context=context) if add_qty: add_qty -= 1 @@ -118,23 +108,16 @@ class sale_order(osv.Model): elif add_qty != None: quantity = sol.browse(cr, SUPERUSER_ID, line_id, context=context).product_uom_qty + (add_qty or 0) - # select linked product - option_ids = [line.id for line in so.order_line if line.linked_line_id.id == line_id] - # Remove zero of negative lines if quantity <= 0: - sol.unlink(cr, SUPERUSER_ID, [line_id] + option_ids, context=context) + sol.unlink(cr, SUPERUSER_ID, [line_id], context=context) else: # update line values = self._website_product_id_change(cr, uid, ids, so.id, product_id, line_id, context=context) values['product_uom_qty'] = quantity sol.write(cr, SUPERUSER_ID, [line_id], values, context=context) - # change quantity of linked product - if option_ids: - sol.write(cr, SUPERUSER_ID, option_ids, {'product_uom_qty': quantity}, context=context) - - return (line_id, quantity, option_ids) + return (line_id, quantity) def _cart_accessories(self, cr, uid, ids, context=None): for order in self.browse(cr, uid, ids, context=context): @@ -143,28 +126,6 @@ class sale_order(osv.Model): product_ids = random.sample(s, min(len(s),3)) return self.pool['product.product'].browse(cr, uid, product_ids, context=context) - def _cart_optional_products(self, cr, uid, ids, context=None): - for order in self.browse(cr, uid, ids, context=context): - products = {} - for l in order.website_order_line: - opt = {} - for o in l.product_id.optional_product_ids: - if not opt.get(o.product_tmpl_id.id): - opt[o.product_tmpl_id.id] = { - 'product_tmpl_id': o.product_tmpl_id, - 'product_ids': [] - } - if o not in opt[o.product_tmpl_id.id]: - opt[o.product_tmpl_id.id]['product_ids'].append(o) - - if opt: - products[l.id] = { - 'order_line_id': l, - 'optional_product_tmpl': opt - } - - return products - class website(orm.Model): _inherit = 'website' diff --git a/addons/website_sale/views/templates.xml b/addons/website_sale/views/templates.xml index c05c9b3524e..25f617f552b 100644 --- a/addons/website_sale/views/templates.xml +++ b/addons/website_sale/views/templates.xml @@ -393,13 +393,10 @@

Product not available

- - Add to Cart - Add to Cart - + Add to Cart -