Skip to Content
Menu
This question has been flagged
2 Replies
1614 Views

Hi,


I just updated Odoo Enterprise from v12 to v12.3. In v12 the Attendance application is linked to the manual_attendance boolean field from the Employee model.


The manual_attendance field has beind removed from Odoo v12.3. I have been trying to replicate this behaviour following this guide:


    https://www.odoo.com/forum/help-1/question/how-to-assign-group-for-a-particular-employee-via-python-code-odoo11-148769


I created two boolean fields on the Employee Model (x_manual_attendance, x_manual_attendance_copy), x_manual_attendance_copy is dependend of x_manual_attendance and I added this python code:


Compute:

for rec in self:

  if rec.x_manual_attendance:

    your_group = self.env.ref('hr_attendance.group_hr_attendance_user')   

    users = self.env['res.users'].sudo().search([('name', '=', rec.resource_id.name)], limit=1)

    for user in users:

        your_group.write({'users': [(4, user.id)]})

      rec['x_manual_attendance_copy'] = rec.x_manual_attendance


I'm getting this error:

Odoo Server Error

Error while validating constraint

maximum recursion depth exceeded while calling a Python object


I cannot figure out what is wrong. Some help please?

Avatar
Discard
Best Answer

Hello,

Since you are using limit =1 then no need to loop for users so remove for loop and write direct line of write on group.

One more thing you can check if you are writting on "x_manual_attendance_copy" it should not be in depends of compute otherwise it could go in loop.

Regards,

Mustufa Rangwala

Avatar
Discard
Author

Hi Mustufa,

Thanks for the help, but I give up. I tried your idea of writing in user, but Im still getting an error. This is the code:

Field Name: x_manual_attendance_copy

Dependencies x_manual_attendance

Compute:

for rec in self:

rec['x_manual_attendance_copy'] = rec.x_manual_attendance

if rec.x_manual_attendance:

user = self.env['res.users'].browse(rec.resource_id.user_id)

your_group = self.env.ref('studio_customization.attendances_attendances')

user.write({'groups_id': [(4, your_group.id)]})

Im getting an error:

hr.employee(<odoo.models.NewId object at 0x7f4f8aa9e9d8>,).x_manual_attendance_copy

I was able to get it working using a different approach. Instead of writing to the group, I made a Tree View of res.users with a Toggle button for my group, and then on added this code to x_manual_attendance_copy:

for rec in self:

user = self.env['res.users'].browse(rec.resource_id.user_id)

if user.has_group('studio_customization.attendances_attendances'):

#hr_attendance.group_hr_attendance_user

rec['x_manual_attendance_copy'] = 1

else:

rec['x_manual_attendance_copy'] = 0

Basically, x_manual_attendance_copy is to read the group, not to write it. Is not what I wanted to do originally, but I works.

Thanks

Oscar

Author Best Answer

Hi Mustafa,

Makes sense to remove the loop, but Im not sure how is the new syntax. What do u mean about dependencies?

Currently, I'm trying the following code:


Model: Employee
Dependencies:   x_manual_attendance
Compute:

for rec in self:
  rec['x_manual_attendance_copy'] = rec.x_manual_attendance
  
  if rec.x_manual_attendance:
    your_group = self.env.ref('hr_attendance.group_hr_attendance_user')
    user = self.env['res.users'].sudo().search([('name', '=', rec.resource_id.name)], limit=1)
    your_group.write( [(4, user.id)]) 

I keep trying different options regarding the line your_group.write( [(4, user.id)]) , I keep getting an error



Avatar
Discard

Hello,

Can you try writting on user instead of writting on group ?

user.write({'groups_id': [(4, your_group.id)]})

See example here for detail: https://github.com/odoo/odoo/blob/12.0/addons/portal/wizard/portal_wizard.py#L143

Regards,

Mustufa Rangwala (Probuse)