Search code examples
pythonpython-3.xodoocall

how to set conditions in a method and call its output in another one in python?


I'm working in a bite long code so I need to make it shorter as I could

In my code there is a lot of what to do if some condition and here is a short example

@api.multi
def product_summary_test(self):
    product_summary_dict = {}
    data = []

    if self.date_from and self.date_to and self.pos_config:
        order_detail = self.env['pos.order'].search([('date_order', '>=', self.date_from),
                                                     ('date_order', '<=', self.date_to),
                                                     ('config_id', '=', self.pos_config.id)])
   do something with order_detail....

   if self.date_from and self.date_to and self.pos_config:
        order_detail = self.env['pos.order'].search([('date_order', '>=', self.date_from),
                                                     ('date_order', '<=', self.date_to)])
   do the same something with order_detail......
    
   if  not nself.date_from and not self.date_to and self.pos_config:
        order_detail = self.env['pos.order'].search([('config_id', '=', self.pos_config.id)])
   
   also do the the same with order_detail......

so the only thing is changing in every condition which returns the data that I'm working on it and if repeated that it's taking along

so how can I make another method that return order_detail in every condition and I just call it returned values on my method to avoid repeating


Solution

  • I am proposing an answer:

    def product_summary_test(self):
        product_summary_dict = {}
        data = []
        
        dom = []
        
        if self.date_from:
            dom.append(('date_order', '>=', self.date_from))
        if self.date_to:
            dom.append(('date_order', '<=', self.date_to))
        if self.pos_config:
            dom.append(('config_id', '=', self.pos_config.id))
            
        order_detail = self.env['pos.order'].search(dom)
        #do something with order_detail....