Odoo Mindscape. Understand Odoo faster. Explore modules, fields, dependencies and version differences in one place instead of digging through the source code.

Module: Mail Whatsapp Gateway

Entity: mail.whatsapp.template

Compute Method: _compute_variable_ids

Inputs Method Outputs
for template in self:
            to_remove = self.env["mail.whatsapp.template.variable"]
            to_keep = self.env["mail.whatsapp.template.variable"]
            new_values = []
            header_variables = list(re.findall(REG_VARIABLE, template.header or ""))
            body_variables = set(re.findall(REG_VARIABLE, template.body or ""))
            # header
            current_header_variable = template.variable_ids.filtered(
                lambda line: line.line_type == "header"
            )
            if header_variables and not current_header_variable:
                new_values.append(
                    {
                        "name": header_variables[0],
                        "line_type": "header",
                        "template_id": template.id,
                    }
                )
            elif not header_variables and current_header_variable:
                to_remove += current_header_variable
            elif current_header_variable:
                to_keep += current_header_variable
            # body
            current_body_variables = template.variable_ids.filtered(
                lambda line: line.line_type == "body"
            )
            new_body_variable_names = [
                var_name
                for var_name in body_variables
                if var_name not in current_body_variables.mapped("name")
            ]
            deleted_variables = current_body_variables.filtered(
                lambda var, body_variables=body_variables: var.name
                not in body_variables
            )

            new_values += [
                {"name": var_name, "line_type": "body", "template_id": template.id}
                for var_name in set(new_body_variable_names)
            ]
            to_remove += deleted_variables
            to_keep += current_body_variables - deleted_variables
            template.variable_ids = [(3, to_remove.id) for to_remove in to_remove] + [
                Command.create(vals) for vals in new_values
            ]

Back to entity