Adding a new record automatically

The name of the pictureThe name of the pictureThe name of the pictureClash 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)






share|improve this question



























    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)






    share|improve this question























      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)






      share|improve this question













      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)








      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 18 at 1:32









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Apr 17 at 11:42









      Tessnim

      336




      336




















          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?






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            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






























            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?






            share|improve this answer

























              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?






              share|improve this answer























                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?






                share|improve this answer













                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?







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Apr 17 at 17:15









                pjz

                2,036615




                2,036615






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?