So i have 3 categorys. first is without childs, second is always has a child but some times dosnt have parent and some times does and third category is always without a parent.
And my goal is to go over this 3 categorys starting from bottom to top.
first i need to check category without child, second the secodn category and then third.
if my condition is met i just desplay the message, if conditions not meet i go to another category and check for condition.
so i wrote this code and it's kinda ok, but maybe i could do it without repeating myself , and make my code more simple?
msg = _("some %s %s message: %s.")
msgs = []
for line in order.order_line:
parent_id = line.product_id.categ_id.parent_id
parent_parent = line.product_id.categ_id.parent_id
categ_id = line.product_id.categ_id
categorys = parent_id + parent_parent + categ_id
categorys = parent_id + parent_parent + categ_id
for categ in categorys:
if not categ.childs_id and categ.qty_for_discount:
if line.product_qty < categ.qty_for_discount:
msgs.append(
msg % (
categ.qty_for_discount - line.product_qty,
line.product_id.uom_id.name,
categ.name
)
)
elif categ.parent_id and categ.child_id and categ.qty_for_discount:
if line.product_qty < categ.qty_for_discount:
msgs.append(
msg % (
categ.qty_for_discount - line.product_qty,
line.product_id.uom_id.name,
categ.name
)
)
else:
if line.product_qty < categ.qty_for_discount:
msgs.append(
msg % (
categ.qty_for_discount - line.product_qty,
line.product_id.uom_id.name,
categ.name
)
)
Take advantage of Python's variable scope. Try something like this:
def your_method(self):
msgs = []
def append_msg():
msgs.append( _("some %s %s message: %s.")% (
categ.qty_for_discount - line.product_qty,
line.product_id.uom_id.name,
categ.name
))
for line in order.order_line:
parent_id = line.product_id.categ_id.parent_id
parent_parent = line.product_id.categ_id.parent_id
categ_id = line.product_id.categ_id
categories = parent_id + parent_parent + categ_id
categories = parent_id + parent_parent + categ_id
for categ in categories:
if not categ.childs_id and categ.qty_for_discount:
if line.product_qty < categ.qty_for_discount:
append_msg()
elif categ.parent_id and categ.child_id and categ.qty_for_discount:
if line.product_qty < categ.qty_for_discount:
append_msg()
else:
if line.product_qty < categ.qty_for_discount:
append_msg()