Post by caalb »

Hey guys,

I want to ask you how your module sends it's data to odoo?

Background:
I wrote a module that keeps employee_ids (ressources) and user_id (assigned to) on the task level in sync.
Works fine on kanban. When you assign a task to users the emplyee_ids are set, too, and vice versa.
However, selecting ressources on the gantt does not trigger the writing of the user_ids.

I implemented this as an extension off the write-function.

Here is my code:

 # sync user_id and employee_id
        if 'employee_ids' in vals or 'user_ids' in vals or 'resource_id' in vals:
            for task in self:
                # Start with current values
                current_user_ids = set(task.user_ids.ids)
                current_employee_ids = set(task.employee_ids.ids)

            user_write = 'user_ids' in vals
            emp_write = 'employee_ids' in vals
            ressource_write = 'ressource_id' in vals

            # Both fields need syncing only if one is changed
            if user_write:
                new_user_ids = self._resolve_m2m_command(current_user_ids, vals['user_ids'])

                if not new_user_ids:
                    new_employee_ids = set()
                else:
                    new_employee_ids = set(self.env['res.users'].browse(new_user_ids).mapped('employee_ids').ids)

            elif emp_write or ressource_write:
                new_employee_ids = self._resolve_m2m_command(current_employee_ids, vals['employee_ids'])

                if not new_employee_ids:
                    new_user_ids = set()
                else:
                    new_user_ids = set(self.env['hr.employee'].browse(new_employee_ids).mapped('user_id').ids)
            else:
                continue  # nothing to sync

            vals['user_ids'] = [(6, 0, list(new_user_ids))]
            vals['employee_ids'] = [(6, 0, list(new_employee_ids))]

Do you have a suggestion why this might not be triggered by the gantt?

Cheers
caalb


Post by tomb »

Hi caalb

in the source code of the module in the "controllers" folder you will find "/bryntum_gantt/send/update" function which takes a json and processes it. This function writes to "assigned_resources" field which is a one2many to a model that hosts pairs of user / percentage data. This field in turn links to employee_ids because employee_ids is a computed field that takes its info from which employees are linked in that one2many. Also if you write to employee_ids then in the background it will write to assigned_resources instead.

    assigned_resources = fields.One2many(
        "project.task.assignment", inverse_name="task", string="Assignments"
    )
    @api.depends("assigned_resources")
    def _compute_employee_ids(self):
        for this in self:
            resources = this.assigned_resources.mapped("resource_base")
            employees = self.env["hr.employee"].search(
                [("resource_id", "in", resources.ids)]
            )
            this.employee_ids = employees

In short the issue is that your write trigger only works when it's employee_ids being written to, but not when assigned_resources is written to.

What would work is when you put the write hook on "assigned_resources" instead, though you'll also have to probably catch any write/create/unlink on "project.task.assignment" model instead. So that's 4 overrides instead of one.


Post by caalb »

Ah great!

Thanks for the quick reply and the insight.
This I can work with.

Best
caalb


Post Reply