[IMP] website_sale_options: new api migration
This commit is contained in:
@@ -1,39 +1,27 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from openerp import SUPERUSER_ID
|
||||
from openerp.addons.web import http
|
||||
from openerp.addons.web.http import request
|
||||
from openerp.addons.website_sale.controllers.main import WebsiteSale
|
||||
from odoo import http
|
||||
from odoo.http import request
|
||||
from odoo.addons.website_sale.controllers.main import WebsiteSale
|
||||
|
||||
class website_sale_options(WebsiteSale):
|
||||
class WebsiteSaleOptions(WebsiteSale):
|
||||
|
||||
@http.route(['/shop/product/<model("product.template"):product>'], type='http', auth="public", website=True)
|
||||
def product(self, product, category='', search='', **kwargs):
|
||||
r = super(website_sale_options, self).product(product, category, search, **kwargs)
|
||||
|
||||
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
|
||||
template_obj = pool['product.template']
|
||||
|
||||
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))
|
||||
|
||||
r.qcontext['optional_product_ids'] = optional_product_ids
|
||||
r = super(WebsiteSaleOptions, self).product(product, category, search, **kwargs)
|
||||
r.qcontext['optional_product_ids'] = map(lambda p: p.with_context({'active_id': p.id}), product.optional_product_ids)
|
||||
return r
|
||||
|
||||
@http.route(['/shop/cart/update_option'], type='http', auth="public", methods=['POST'], website=True, multilang=False)
|
||||
def cart_options_update_json(self, product_id, add_qty=1, set_qty=0, goto_shop=None, lang=None, **kw):
|
||||
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
|
||||
if lang:
|
||||
context = dict(context, lang=lang)
|
||||
request.website = request.website.with_context(context)
|
||||
request.website = request.website.with_context(lang=lang)
|
||||
|
||||
order = request.website.sale_get_order(force_create=1)
|
||||
product = pool['product.product'].browse(cr, uid, int(product_id), context=context)
|
||||
product = request.env['product.product'].browse(int(product_id))
|
||||
|
||||
|
||||
option_ids = [p.id for tmpl in product.optional_product_ids for p in tmpl.product_variant_ids]
|
||||
option_ids = product.optional_product_ids.mapped('product_variant_ids').ids
|
||||
optional_product_ids = []
|
||||
for k, v in kw.items():
|
||||
if "optional-product-" in k and int(kw.get(k.replace("product", "add"))) and int(v) in option_ids:
|
||||
@@ -55,22 +43,19 @@ class website_sale_options(WebsiteSale):
|
||||
|
||||
@http.route(['/shop/modal'], type='json', auth="public", methods=['POST'], website=True)
|
||||
def modal(self, product_id, **kw):
|
||||
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
|
||||
pricelist = request.website.get_current_pricelist()
|
||||
product_context = dict(context)
|
||||
product_context = dict(request.context)
|
||||
if not product_context.get('pricelist'):
|
||||
product_context['pricelist'] = int(pricelist)
|
||||
# fetch quantity from custom context
|
||||
product_context.update(kw.get('kwargs', {}).get('context', {}))
|
||||
|
||||
from_currency = pool['res.users'].browse(cr, uid, uid, context=context).company_id.currency_id
|
||||
from_currency = request.env.user.company_id.currency_id
|
||||
to_currency = pricelist.currency_id
|
||||
compute_currency = lambda price: pool['res.currency']._compute(cr, uid, from_currency, to_currency, price, context=context)
|
||||
product = pool['product.product'].browse(cr, uid, int(product_id), context=product_context)
|
||||
request.website = request.website.with_context(context)
|
||||
|
||||
compute_currency = lambda price: request.env['res.currency']._compute(from_currency, to_currency, price)
|
||||
product = request.env['product.product'].with_context(product_context).browse(int(product_id))
|
||||
return request.website._render("website_sale_options.modal", {
|
||||
'product': product,
|
||||
'compute_currency': compute_currency,
|
||||
'get_attribute_value_ids': self.get_attribute_value_ids,
|
||||
})
|
||||
'product': product,
|
||||
'compute_currency': compute_currency,
|
||||
'get_attribute_value_ids': self.get_attribute_value_ids,
|
||||
})
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from openerp import tools
|
||||
from openerp.osv import osv, fields
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
class product_template(osv.Model):
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class ProductTemplate(models.Model):
|
||||
_inherit = "product.template"
|
||||
|
||||
_columns = {
|
||||
'optional_product_ids': fields.many2many('product.template','product_optional_rel','src_id','dest_id',string='Optional Products', help="Products to propose when add to cart."),
|
||||
}
|
||||
optional_product_ids = fields.Many2many('product.template', 'product_optional_rel', 'src_id', 'dest_id', string='Optional Products', help="Products to propose when add to cart.")
|
||||
|
||||
@@ -1,24 +1,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from openerp import SUPERUSER_ID, api
|
||||
from openerp.osv import osv, orm, fields
|
||||
from openerp.tools.translate import _
|
||||
from odoo import api, fields, models, _
|
||||
|
||||
|
||||
class sale_order_line(osv.Model):
|
||||
class SaleOrderLine(models.Model):
|
||||
_inherit = "sale.order.line"
|
||||
_columns = {
|
||||
'linked_line_id': fields.many2one('sale.order.line', 'Linked Order Line', domain="[('order_id','!=',order_id)]", ondelete='cascade'),
|
||||
'option_line_ids': fields.one2many('sale.order.line', 'linked_line_id', string='Options Linked'),
|
||||
}
|
||||
|
||||
class sale_order(osv.Model):
|
||||
linked_line_id = fields.Many2one('sale.order.line', string='Linked Order Line', domain="[('order_id', '!=', order_id)]", ondelete='cascade')
|
||||
option_line_ids = fields.One2many('sale.order.line', 'linked_line_id', string='Options Linked')
|
||||
|
||||
class SaleOrder(models.Model):
|
||||
_inherit = "sale.order"
|
||||
|
||||
@api.multi
|
||||
def _cart_find_product_line(self, product_id=None, line_id=None, **kwargs):
|
||||
self.ensure_one()
|
||||
lines = super(sale_order, self)._cart_find_product_line(product_id, line_id)
|
||||
lines = super(SaleOrder, self)._cart_find_product_line(product_id, line_id)
|
||||
if line_id:
|
||||
return lines
|
||||
linked_line_id = kwargs.get('linked_line_id', False)
|
||||
@@ -32,30 +30,31 @@ class sale_order(osv.Model):
|
||||
lines = lines.filtered(lambda line: not line.option_line_ids)
|
||||
return lines
|
||||
|
||||
def _cart_update(self, cr, uid, ids, product_id=None, line_id=None, add_qty=0, set_qty=0, context=None, **kwargs):
|
||||
@api.multi
|
||||
def _cart_update(self, product_id=None, line_id=None, add_qty=0, set_qty=0, **kwargs):
|
||||
""" Add or set product quantity, add_qty can be negative """
|
||||
value = super(sale_order, self)._cart_update(cr, uid, ids, product_id, line_id, add_qty, set_qty, context=context, **kwargs)
|
||||
value = super(SaleOrder, self)._cart_update(product_id, line_id, add_qty, set_qty, **kwargs)
|
||||
|
||||
sol = self.pool.get('sale.order.line')
|
||||
line = sol.browse(cr, SUPERUSER_ID, value.get('line_id'), context=context)
|
||||
SaleOrderLineSudo = self.env['sale.order.line'].sudo()
|
||||
line = SaleOrderLineSudo.browse(value.get('line_id'))
|
||||
|
||||
# link a product to the sale order
|
||||
if kwargs.get('linked_line_id'):
|
||||
linked_line_id = sol.browse(cr, SUPERUSER_ID, kwargs['linked_line_id'], context=context)
|
||||
linked_line = SaleOrderLineSudo.browse(kwargs['linked_line_id'])
|
||||
line.write({
|
||||
"name": _("%s\nOption for: %s") % (line.name, linked_line_id.product_id.name_get()[0][1]),
|
||||
"linked_line_id": linked_line_id.id
|
||||
"name": _("%s\nOption for: %s") % (line.name, linked_line.product_id.display_name),
|
||||
"linked_line_id": linked_line.id
|
||||
})
|
||||
|
||||
value['option_ids'] = set()
|
||||
for so in self.browse(cr, uid, ids, context=context):
|
||||
for so in self:
|
||||
# select all optional products linked to the updated line
|
||||
option_line_ids = [l for l in so.order_line if l.linked_line_id.id == line.id]
|
||||
option_lines = so.order_line.filtered(lambda l: l.linked_line_id.id == line.id)
|
||||
|
||||
# update line
|
||||
for option_line_id in option_line_ids:
|
||||
super(sale_order, self)._cart_update(cr, uid, ids, option_line_id.product_id.id, option_line_id.id, add_qty, set_qty, context=context, **kwargs)
|
||||
option_line_id.write({"name": _("%s\nOption for: %s") % (option_line_id.name, option_line_id.linked_line_id.product_id.name_get()[0][1])})
|
||||
for option_line_id in option_lines:
|
||||
super(SaleOrder, self)._cart_update(option_line_id.product_id.id, option_line_id.id, add_qty, set_qty, **kwargs)
|
||||
option_line_id.write({"name": _("%s\nOption for: %s") % (option_line_id.name, option_line_id.linked_line_id.product_id.display_name)})
|
||||
value['option_ids'].add(option_line_id.id)
|
||||
|
||||
value['option_ids'] = list(value['option_ids'])
|
||||
|
||||
Reference in New Issue
Block a user