Files
odoo_source/addons/product/wizard/product_label_layout.py
T
Touati Djamel (otd) 48a6f88c50 [FIX] product: prevent printing labels for archived products
Steps to reproduce the bug:
- Create a storable product
- Archive it
- Try to print labels

Problem:
A traceback is triggered, Because when we click on print the label
we have to open the wizard, so we load the wizard model
`product.label.layout` via an onchange:
https://github.com/odoo/odoo/blob/15.0/odoo/models.py#L6357-L6360
thanks to the context, it will find the concerned product
(via default_product_id) Except that the product is archived,
so ignored in the result returned by the onchange

therefore, in the confirmation, we will create the wizard,
but it will not have a product filled, so in the `_prepare_report_data`
function we do not enter in the if or the elif:
https://github.com/odoo/odoo/blob/d9f45ba6941939b3d4b40beb5abbc330be84d695/addons/product/wizard/product_label_layout. py#L49-L54

it will therefore raise a traceback when we want to iterate on it:
https://github.com/odoo/odoo/blob/d9f45ba6941939b3d4b40beb5abbc330be84d695/addons/product/wizard/product_label_layout.py#L60

opw-2855659

closes odoo/odoo#97226

X-original-commit: 6dcec12074014434ce1283449700aa352993ac0c
Signed-off-by: Adrien Widart <awt@odoo.com>
2022-08-01 19:37:20 +02:00

75 lines
2.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, api, fields, models
from odoo.exceptions import UserError
class ProductLabelLayout(models.TransientModel):
_name = 'product.label.layout'
_description = 'Choose the sheet layout to print the labels'
print_format = fields.Selection([
('dymo', 'Dymo'),
('2x7xprice', '2 x 7 with price'),
('4x7xprice', '4 x 7 with price'),
('4x12', '4 x 12'),
('4x12xprice', '4 x 12 with price')], string="Format", default='2x7xprice', required=True)
custom_quantity = fields.Integer('Quantity', default=1, required=True)
product_ids = fields.Many2many('product.product')
product_tmpl_ids = fields.Many2many('product.template')
extra_html = fields.Html('Extra Content', default='')
rows = fields.Integer(compute='_compute_dimensions')
columns = fields.Integer(compute='_compute_dimensions')
@api.depends('print_format')
def _compute_dimensions(self):
for wizard in self:
if 'x' in wizard.print_format:
columns, rows = wizard.print_format.split('x')[:2]
wizard.columns = int(columns)
wizard.rows = int(rows)
else:
wizard.columns, wizard.rows = 1, 1
def _prepare_report_data(self):
if self.custom_quantity <= 0:
raise UserError(_('You need to set a positive quantity.'))
# Get layout grid
if self.print_format == 'dymo':
xml_id = 'product.report_product_template_label_dymo'
elif 'x' in self.print_format:
xml_id = 'product.report_product_template_label'
else:
xml_id = ''
active_model = ''
if self.product_tmpl_ids:
products = self.product_tmpl_ids.ids
active_model = 'product.template'
elif self.product_ids:
products = self.product_ids.ids
active_model = 'product.product'
else:
raise UserError(_("No product to print, if the product is archived please unarchive it before printing its label."))
# Build data to pass to the report
data = {
'active_model': active_model,
'quantity_by_product': {p: self.custom_quantity for p in products},
'layout_wizard': self.id,
'price_included': 'xprice' in self.print_format,
}
return xml_id, data
def process(self):
self.ensure_one()
xml_id, data = self._prepare_report_data()
if not xml_id:
raise UserError(_('Unable to find report template for %s format', self.print_format))
report_action = self.env.ref(xml_id).report_action(None, data=data)
report_action.update({'close_on_report_download': True})
return report_action