Search code examples
pythonodooodoo-15

search Method Optimization for searching a field in odoo


actually I have a field in sale order which I update in onchange with a datetime field, and that has to be optimized, I am unable to figure out what to be done so need help, here is the method

here the time_slot field is a selection field with AM and PM

 def test(self):
        tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)
        date_list = [(tomorrow + datetime.timedelta(days=x)).date() for x in range(30)]

        technician_line = self.env["allocated_technician_line"].search(
            [
                ("service_type_id", "=", self.service_type_id.id),
                ("time_slot", "=", self.time_slot),
                (
                    "technician_id.user_id",
                    "in",
                    self.area_id.technicians.ids,
                ),
                (
                    "allocated_technician_id.date",
                    "in",
                    date_list,
                ),

            ]
        )
        allocated_technician_schedule_ids = []
        for line in technician_line:
            allocated_sale_order = self.env["sale.order"].search(
                [
                    (
                        "preferred_service_schedule",
                        "=",
                        line.allocated_technician_id.id,
                    ),
                    ("service_type_id", "=", line.service_type_id.id),
                    ("time_slot", "=", line.time_slot),
                    ("state", "!=", "cancel"),
                ]
            )

            if self.env.context.get('active_id'):
                allocated_sale_order = allocated_sale_order.filtered(
                    lambda r: r.id != self.env.context.get('active_id')
                )

            available_allocation = line.allocation - len(allocated_sale_order)
            if available_allocation > 0:
                allocated_technician_schedule_ids.append(line.allocated_technician_id.id)

        allocated_technician_schedule_recs = self.env["allocated_technician"].browse(allocated_technician_schedule_ids)
        user_ids = allocated_technician_schedule_recs.mapped('user_id').ids

        if (self.assigned_asc.id not in user_ids):
            self["assigned_asc"] = False

need help in this method to be optimized just to bring allocated_sale_order search method out of for loop.


Solution

  • That's only a small optimization, but you can use search_count instead of search. But you also have to rearrange how to use active_id for that:

    # no changes before
    for line in technician_line:
        order_domain = [
            (
                "preferred_service_schedule",
                "=",
                line.allocated_technician_id.id,
            ),
            ("service_type_id", "=", line.service_type_id.id),
            ("time_slot", "=", line.time_slot),
            ("state", "!=", "cancel"),
        ]
        if self.env.context.get('active_id'):
            order_domain.append(('id', '!=', self.env.context.get('active_id')))
        order_count = self.env["sale.order"].search_count(order_domain)
        available_allocation = line.allocation - order_count
    # and so on