2
\$\begingroup\$

I created this method that adds a new situation to the situation list. The situation has a many-to-one relation with project.task. This method seems a little longer than it should be and I don't know how to optimize it:

@api.multi
def create_situation(self):
    active_id = self.env.context.get('active_ids', []) or []
    proj_obj = self.env['project.task'].browse(active_id)
    situation_orders = self.env['project.task.situation'].browse([])      
    task_lines = self.env['task.situation.line'].browse([])
    situation_ids = self.env['project.task.situation'].search([('task_id', '=', proj_obj.id)], order='id desc', limit=1)
    size_list_situation_ids = len(situation_ids)
    last_id = situation_ids and max(situation_ids)
    s_sequence = "Situation N° " + str(proj_obj.sequence)

    for record in proj_obj:   
        task_lines = [(0, 0, {'ref': line.ref,
                                      'product_id': line.product_id.id,
                                      'quantity': line.quantity,
                                      'um_id': line.um_id,
                                      'price_unit': line.price_unit,
                                      'price_subtotal': line.price_subtotal,
                                      'progress': line.progress,
                                      'cumulative_amount_ht': line.cumulative_amount_ht,
                                      'previous_amount_ht':line1.cumulative_amount_ht if size_list_situation_ids != 0 else 0,
                                      'amount_net_ht': line.cumulative_amount_ht if size_list_situation_ids == 0 else (line.cumulative_amount_ht - line1.cumulative_amount_ht)}) for line, line1 in izip_longest(record.task_line_ids, last_id.task_line)]
    situation_order = {'partner_id': proj_obj.project_id.partner_id.id,
                        'task_id':proj_obj.id,
                        'task_line':task_lines,
                        'user_id': proj_obj.user_id.id,
                        'name':s_sequence,
                        'tyoe': 'situation' if self._context.get('type', 'situation') else 'final_count',
                        'project_id':proj_obj.project_id.id,
                        'due_date': self.date_id.id}
    situation_orders+=situation_orders.with_context(self._context).create(situation_order) 
    proj_obj.write({'sequence':proj_obj.sequence+1})
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

First off, make it more maintainable: break out that long list comprehension into a for loop so it's clearer where line is coming from.

As for optimization, since size_list_situation_ids doesn't change through the loop, you can refactor those trinary ops into functions and then set them just once at the beginning:

if size_list_situation_ids:
    prev_amount_ht = lambda l: l.cumulative_amount_ht
    amount_net_ht = lambda l, l1: l.cumulative_amount_ht - l1.cumulative_amount_ht
else:
    prev_amount_ht = lambda l: 0
    amount_net_ht = lambda l, l1: l.cumulative_amount_ht

and then do:

...
'previous_amount_ht': prev_amount_ht(line1),
'amount_net_ht': amount_net_ht(line, line1),
...

which saves a couple of comparisons every time through the loop.

Much more than that is difficult to suss out, since the code looks non-functional overall: where does situation_orders come from? Why is the initial task_lines even being loaded, as it doesn't seem to be used?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.