Odoo - Hide button for specific user

前端 未结 2 1622
醉话见心
醉话见心 2021-02-20 03:11

I am using odoo 10 enterpeise . I want to show buttons to specific users not to groups because there would be many users in a group and i want to only show below button who have

2条回答
  •  北荒
    北荒 (楼主)
    2021-02-20 04:05

    I have understood that you have a fixed list of users who will be able to approve and other fixed list of users who will be able to reject. In spite of being a few users, I would create two groups and use groups attribute on your buttons, but if even so you do not want to create a couple of groups for them, you can do this:

    from openerp import models, api
    import json
    from lxml import etree
    
    FIRST_APPROVE = []  # Fill this list with the IDs of the users who can update approve
    SECOND_APPROVE = []  # Fill this list with the IDs of the users who can update reject
    
    class YourClass(models.Model):
        _inherit = 'your.class'
    
        def update_json_data(self, json_data=False, update_data={}):
            ''' It updates JSON data. It gets JSON data, converts it to a Python
            dictionary, updates this, and converts the dictionary to JSON data
            again. '''
            dict_data = json.loads(json_data) if json_data else {}
            dict_data.update(update_data)
            return json.dumps(dict_data, ensure_ascii=False)
    
        def set_modifiers(self, element=False, modifiers_upd={}):
            ''' It updates the JSON modifiers with the specified data to indicate
            if a XML tag is readonly or invisible or not. '''
            if element is not False:  # Do not write only if element:
                modifiers = element.get('modifiers') or {}
                modifiers_json = self.update_json_data(
                    modifiers, modifiers_upd)
                element.set('modifiers', modifiers_json)
    
        @api.model
        def fields_view_get(self, view_id=None, view_type='form', toolbar=False,
                            submenu=False):
            res = super(YourClass, self).fields_view_get(
                view_id=view_id, view_type=view_type, toolbar=toolbar,
                submenu=submenu)
    
            doc = etree.XML(res['arch'])
    
            if view_type == 'form':
                if self.env.uid in FIRST_APPROVE:
                    upd_approve_btn_search = doc.xpath("//button[@name='update_approve']")
                    upd_approve_btn = upd_approve_btn_search[0] \
                        if upd_approve_btn_search else False
                    if upd_approve_btn:
                        self.set_modifiers(upd_approve_btn, {'invisible': False, })
    
                if self.env.uid in SECOND_APPROVE:
                    upd_reject_btn_search = doc.xpath("//button[@name='update_reject']")
                    upd_reject_btn = upd_reject_btn_search[0] \
                        if upd_reject_btn_search else False
                    if upd_reject_btn:
                        self.set_modifiers(upd_reject_btn, {'invisible': False, })
    
            res['arch'] = etree.tostring(doc)
            return res
    

    FIRST APPROVE and SECOND_APPROVE will be const in which you must introduce the fixed IDS of the users who can perform the respective action (for example: FIRST APPROVE = [2, 7, 9]).

    YourClass must be the class in which you have declared the methods of your buttons (the one in which you have declared update_approve and update_reject).

    IMPORTANT: with this code, your buttons must be always invisible (write invisible="1" at your XML view), because after loading the XML code, the fields_view_get will overwrite the invisible value to set 0.

    This is an uncommon way to manage your purpose, but unfortunately I think it is the simplest one if you do not want to create groups. I hope it helps you and other users!

提交回复
热议问题