0

There is a default many2one field in 'hr.employee' model called 'parent_id'(Manager) My requirement is:

If there is any record on that parent_id field then the action of 'calendar.event' domain will be [('user_id','=',user.id)] if no record is present then domain will be [(1,'=',1)]

Any help will be appreciated, Thanks

Sidharth Panda
  • 404
  • 3
  • 17

1 Answers1

0

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>
benjibo
  • 1
  • 3
  • no i am not trying to pull that up, I am trying that, if the user logged in currently, for that user in hr.employee has any record in parent_id field then I want a domain in calendar.event "[('user_id', '=', self.user_id.id)]" and if there is no record in parent_id field the domain will be [('1', '=', '1')] on the action.,Thats all – Sidharth Panda May 04 '22 at 06:59
  • @SidharthPanda See the edit I added. – benjibo May 04 '22 at 20:36