[IMP] website_sale: some fix and imp

bzr revid: chm@openerp.com-20140221172816-5bb5l9z2xsvswz8u
This commit is contained in:
Christophe Matthieu 2014-02-21 18:28:16 +01:00
parent 758aa20c75
commit 1d4f8783b5
7 changed files with 69 additions and 106 deletions

View File

@ -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):

View File

@ -257,7 +257,7 @@
<link rel="stylesheet" href="/web/static/lib/select2/select2.css"/>
<link rel="stylesheet" href="/web/static/lib/select2-bootstrap-css/select2-bootstrap.css"/>
<link rel="stylesheet" href="/website/static/lib/select2-bootstrap-css/select2-bootstrap.css"/>
<link rel='stylesheet' href="/web/static/lib/jquery.ui/css/smoothness/jquery-ui-1.9.1.custom.css"/>
<script type="text/javascript" src="/web/static/lib/select2/select2.js"></script>

View File

@ -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)]

View File

@ -104,8 +104,7 @@ class website_sale(http.Controller):
pricelist = partner.property_product_pricelist
return pricelist
@http.route([
'/shop/',
@http.route(['/shop/',
'/shop/page/<int:page>/',
'/shop/category/<model("product.public.category"):category>/',
'/shop/category/<model("product.public.category"):category>/page/<int: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/<model("product.template"):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:

View File

@ -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:

View File

@ -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;
});

View File

@ -19,17 +19,18 @@
<template id="header" inherit_id="website.layout" name="Header Shop My Cart Link">
<xpath expr="//header//ul[@id='top_menu']/li" position="before">
<li t-att-class="'' if sale_order and sale_order._cart_qty() else 'hidden'">
<t t-set="website_sale_order" t-value="website.sale_get_order()"/>
<li t-att-class="'' if website_sale_order and website_sale_order.cart_quantity else 'hidden'">
<a href="/shop/cart/">
<i class="fa fa-shopping-cart"></i>
My cart <sup t-attf-class="my_cart_quantity label label-primary" t-esc="sale_order and sale_order._cart_qty() or ''"/>
My cart <sup t-attf-class="my_cart_quantity label label-primary" t-esc="website_sale_order and website_sale_order.cart_quantity or ''"/>
</a>
</li>
</xpath>
</template>
<template id="search" name="Search hidden fields">
<input t-if="search.get('filters')" type="hidden" name="filters" t-att-value="search.get('filters')"/>
<input t-if="attrib" type="hidden" name="filters" t-att-value="attrib"/>
<div class="input-group">
<input type="text" name="search" class="search-query form-control" placeholder="Search..." t-att-value="search"/>
<span class="input-group-btn">
@ -495,6 +496,7 @@
<h1 class="mb32">Shopping Cart</h1>
<div class="row">
<div class="col-md-8 col-sm-9 oe_cart">
<t t-set="website_sale_order" t-value="website.sale_get_order()"/>
<div t-if="not website_sale_order or not website_sale_order.website_order_line" class="well well-lg">
Your cart is empty!
</div>
@ -537,14 +539,14 @@
<td>
<div class="input-group">
<span class="input-group-addon">
<a t-attf-href="/shop/cart/update?product_id={{ line.product_id.id }&amp;add_qty=-1" class="mb8 js_add_cart_json">
<a t-attf-href="#" class="mb8 js_add_cart_json">
<i class="fa fa-minus"></i>
</a>
</span>
<input type="text" class="js_quantity form-control"
t-att-data-id="line.id" t-att-value="int(line.product_uom_qty)"/>
t-att-data-id="line.product_id.id" t-att-value="int(line.product_uom_qty)"/>
<span class="input-group-addon">
<a t-attf-href="/shop/cart/update?product_id={{ line.product_id.id }&amp;add_qty=1" class="mb8 float_left js_add_cart_json">
<a t-attf-href="#" class="mb8 float_left js_add_cart_json">
<i class="fa fa-plus"></i>
</a>
</span>
@ -597,28 +599,28 @@
<tr t-foreach="suggested_products" t-as="product">
<td>
<a t-attf-href="/shop/product/#{ slug(product.product_tmpl_id) }/">
<a t-attf-href="/shop/product/#{ slug(product) }/">
<span t-field="product.image_small"
t-field-options='{"widget": "image", "class": "img-rounded"}'/>
</a>
</td>
<td>
<div>
<a t-attf-href="/shop/product/#{ slug(product.product_tmpl_id) }/">
<a t-attf-href="/shop/product/#{ slug(product) }/">
<strong t-field="product.name"/>
</a>
</div>
<div class="text-muted" t-field="product.description_sale"/>
</td>
<td>
<t t-if="abs(product.lst_price - product.price) &gt; 0.2">
<t t-if="abs(product.product_variant_ids[0].lst_price - product.product_variant_ids[0].price) &gt; 0.2">
<del class="text-danger"
t-field="product.lst_price" t-field-options='{
t-field="product.product_variant_ids[0].lst_price" t-field-options='{
"widget": "monetary",
"display_currency": "website.pricelist_id.currency_id"
}'/>&amp;nbsp;
</t>
<span t-field="product.price" t-field-options='{
<span t-field="product.product_variant_ids[0].price" t-field-options='{
"widget": "monetary",
"display_currency": "website.pricelist_id.currency_id"
}'/>