Adding a new record automatically
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
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)
python python-2.7 odoo
add a comment |Â
up vote
2
down vote
favorite
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)
python python-2.7 odoo
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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)
python python-2.7 odoo
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)
python python-2.7 odoo
edited Apr 18 at 1:32
Jamalâ¦
30.1k11114225
30.1k11114225
asked Apr 17 at 11:42
Tessnim
336
336
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
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?
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
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?
add a comment |Â
up vote
2
down vote
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?
add a comment |Â
up vote
2
down vote
up vote
2
down vote
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?
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?
answered Apr 17 at 17:15
pjz
2,036615
2,036615
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192282%2fadding-a-new-record-automatically%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password