Skip to Content
Menu
This question has been flagged
1 Reply
1801 Views

I'm new to Odoo, and I'm tring to Migrate an addon from version 8 to 12.

So here the code from version 8:​

from openerp import models, exceptions, _, api
from functools import partial
from reportlab.graphics.barcode import createBarcodeDrawing
import logging
_logger = logging.getLogger(__name__)
# _logger.debug('1111111111 %s',docargs)

class ProductLabelReport(models.AbstractModel):
_name = 'report.print_formats_product_label.label'

@api.model
def format_currency(self, value):
return str('%.2f' % value).replace('.', ',')

@api.model
def get_price(self, product):
pricelists = self.env['product.pricelist'].search([
('name', 'ilike', 'Public Pricelist'), ('type', '=', 'sale')])
if not pricelists.exists():
pricelists = self.env['product.pricelist'].search([('type', '=', 'sale')])
if pricelists:
prices = pricelists[0].price_get(product.id, 1)
price_unit = prices[pricelists[0].id]
price = product.taxes_id.compute_all(price_unit, 1)
_logger.debug('2222222222 %s',price)
return price['total_included']
else:
return 0.00

@api.model
def print_barcode(self, value, width, height):
try:
width, height = int(width), int(height)
barcode = createBarcodeDrawing('EAN13', value=value, format='png', width=width, height=height)
barcode = barcode.asString('png')
barcode = barcode.encode('base64', 'strict')
except (ValueError, AttributeError):
raise exceptions.Warning(_('Cannot convert into barcode.'))
return barcode

@api.model
def format_size(self, value, size):
try:
return value[:size]
except:
return value

@api.model
def get_variant_name(self, product):
return '%s %s' % (product.name,'-'.join([v.name for v in product.attribute_value_ids]))

@api.multi
def render_product_label(self, docargs, data):
docargs.update({
'docs': self,
'tmpl_name': 'print_formats_product_label.label_document'})
return docargs

@api.multi
def render_html(self, data=None):
docargs = {
'doc_ids': self.ids,
'doc_model': 'product.product',
'format_currency': partial(self.format_currency),
'get_price': partial(self.get_price),
'print_barcode': partial(self.print_barcode),
'format_size': partial(self.format_size),
'get_variant_name': partial(self.get_variant_name)}
render_func = self.env.context.get(
'render_func', 'render_product_label')
if hasattr(self, render_func):
fnc = getattr(self, render_func)
fnc(docargs, data)
else:
raise exceptions.Warning(_(
'Don\'t have render func %s in object') % render_func)
if not self.ids:
raise exceptions.Warning(_('You must choose at least one record.'))
return self.env['report'].browse(self.ids[0]).with_context(self.env.context).render('print_formats_product_label.label', docargs)


And my migrate on version 12:

from odoo import models, exceptions, _, api
from functools import partial
from reportlab.graphics.barcode import createBarcodeDrawing
import logging
_logger = logging.getLogger(__name__)
# _logger.debug('1111111111 %s',docargs)

class ProductLabelReport(models.AbstractModel):
_name = 'report.print_formats_product_label.label'

@api.model
def format_currency(self, value):
return str('%.2f' % value).replace('.', ',')

@api.model
def get_price(self, product):
pricelists = self.env['product.pricelist'].search([('name', 'ilike', 'Public Pricelist'),('type', '=', 'sale')])
if not pricelists.exists():
pricelists = self.env['product.pricelist'].search([('type', '=', 'sale')])
if pricelists:
prices = pricelists[0].price_get(product.id ,1)
price_unit = prices[pricelists[0].id]
price = product.taxes_id.compute_all(price_unit, 1)
return price['total_included']
else:
return 0.00

@api.model
def print_barcode(self, value, width, height):
try:
width, height = int(width), int(height)
barcode = createBarcodeDrawing('EAN13', value=value, format='png', width=width, height=height)
barcode = barcode.asString('png')
barcode = barcode.encode('base64', 'strict')
except (ValueError, AttributeError):
raise exceptions.Warning(_('Cannot convert into barcode.'))
return barcode

@api.model
def format_size(self, value, size):
try:
return value[:size]
except:
return value

@api.model
def get_variant_name(self, product):
return '%s %s' % (prod_name.name,'-'.join([v.name for v in product.attribute_value_ids]))

@api.multi
def render_product_label(self, docargs, data):
docargs.update({
'docs': self.env['report.print_formats_product_label.label'].browse(data),
'tmpl_name': 'print_formats_product_label.label_document'
})
return docargs

@api.model
def _get_report_values(self, docids, data=None):
docargs = {
'doc_ids': docids,
'doc_model': 'product.product',
'format_currency': partial(self.format_currency),
'get_price': partial(self.get_price),
'print_barcode': partial(self.print_barcode),
'format_size': partial(self.format_size),
'get_variant_name': partial(self.get_variant_name),
}

render_func = self.env.context.get('render_func', 'render_product_label')
if hasattr(self, render_func):
fnc = getattr(self, render_func)
fnc(docargs, docids)
else:
raise exceptions.Warning(_('Don\'t have render func %s in object') % render_func)
if not docids[0]:
raise exceptions.Warning(_('You must choose at least one record.'))

return self.env['ir.actions.report'].browse(docids[0]).with_context(self.env.context).render('print_formats_product_label.label', docargs)

Fast forward the only changes i made is on render_html to _get_report_values and inside.​

The output on v8 from debug:

{'format_size': <functools.partial object at 0x7f9a68fc0ba8>,
'doc_model': 'product.product',
'format_currency': <functools.partial object at 0x7f9a5a06de10>,
'get_price': <functools.partial object at 0x7f9a5a06dc00>,
'get_variant_name': <functools.partial object at 0x7f9a40d7a470>,
'docs': report.print_formats_product_label.label(28451,),
'tmpl_name': 'print_formats_product_label.label_document',
'print_barcode': <functools.partial object at 0x7f9a5a06d158>,
'doc_ids': [28451]}


The output on v12 from debug:

{'get_price': functools.partial(<bound method ProductLabelReport.get_price of report.print_formats_product_label.label()>), 
'tmpl_name': 'print_formats_product_label.label_document',
'docs': report.print_formats_product_label.label(60,),
'doc_model': 'product.product',
'format_size': functools.partial(<bound method ProductLabelReport.format_size of report.print_formats_product_label.label()>),
'print_barcode': functools.partial(<bound method ProductLabelReport.print_barcode of report.print_formats_product_label.label()>),
'format_currency': functools.partial(<bound method ProductLabelReport.format_currency of report.print_formats_product_label.label()>),
'get_variant_name': functools.partial(<bound method ProductLabelReport.get_variant_name of report.print_formats_product_label.label()>),
'doc_ids': [60]}

As you can see im not getting any object id.

So basically on v12 partial is not working, how should modify my code?

Avatar
Discard
Best Answer

Hi Fotic
There is no error in your code. You are getting different debug output because of different python versions.
odoov8 uses python2 while odoov12 is compatible with python3.

Check below code with both python2 and python3, the final result will be same.

from functools import partial

def multiply(x,y):
return x * y

# create a new function that multiplies by 2
dbl = partial(multiply,2)
print (dbl(4))

Hope this will help you!


Regards,




Email: odoo@aktivsoftware.com

Skype: kalpeshmaheshwari

   

Avatar
Discard