If you want a calendar.event
action to be updated based on an hr.employee
record, then you will need to connect the two models. What is your plan on connecting them? Will the calendar.event
have the [('user_id', '=', user.id)]
domain if user.employee_id.parent_id
? Are you trying to pull up a tree view of calendar.event
for all the records associated with the hr.employee
record that the user is looking at?
If this is the case, then one possible solution is to add a button to the form view of the employee. You will need to replace <view_id>
with the view you want it to redirect to. Here is the code for the Python:
from odoo import _, api, fields, models
class HrEmployee(models.Model):
_inherit = 'hr.employee'
def action_list_events(self):
self.ensure_one()
if not self.parent_id:
domain = [('1', '=', '1')]
elif self.user_id:
domain = [('user_id', '=', self.user_id.id)]
else:
return
return {
'name': _("Create Event"),
'res_model': 'calendar.event',
'type': 'ir.actions.act_window',
'view_id': self.env.ref('<view_id>').id,
'view_mode': 'tree',
'domain': domain,
}
And the button:
<button name="action_list_events"/>
Edit: Since you're wanting to limit the all calendar events based on the current user, try this instead:
from odoo import api, fields, models
class CalendarEvent(models.Model):
_inherit = 'calendar.event'
allowed_user_ids = fields.Many2many('res.users', compute='_compute_allowed_user_ids', store=True, string="Allowed Users")
@api.depends('user_id')
def _compute_allowed_user_ids(self):
for record in self:
employee_id = self.env.user.employee_id
if employee_id and employee_id.parent_id:
domain = [('user_id', '=', self.env.user.id)]
elif employee_id and not employee_id.parent_id:
domain = []
else:
record.allowed_user_ids = False
continue
record.allowed_user_ids = self.env['res.users'].search(domain)
And you can either use a record rule or apply it to a specific action window:
<record id="calendar_event_allowed_user_ir_rule" model="ir.rule">
<field name="name">Allowed Users</field>
<field name="model_id" ref="model_calendar_event"/>
<field name="domain_force">[('allowed_user_ids', 'in', [user.id])]</field>
<field name="groups" eval="[(4, ref('base.group_user'))]"/>
</record>
<record id="calendar_event_allowed_user_window" model="ir.actions.act_window">
<field name="name">Calendar Events</field>
<field name="res_model">calendar.event</field>
<field name="view_mode">calendar,tree,form</field>
<field name="domain">[('allowed_user_ids', 'in', [user.id])]</field>
</record>