Odoo - Hide button for specific user

前端 未结 2 1589
醉话见心
醉话见心 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!

    0 讨论(0)
  • 2021-02-20 04:09

    One of the thing i know to use a field in attrs the field must be Mentionsed in the form. i don't know how to get the value of the user id in the form. but if there is not a short way like uid or user you can work arround this, just create a m2o field to res.users make this field compute field with store = False.

        # by default store = False this means the value of this field
        # is always computed.
        current_user = fields.Many2one('res.users', compute='_get_current_user')
    
        @api.depends()
        def _get_current_user(self):
            for rec in self:
                rec.current_user = self.env.user
    

    and you can use this field in your form.

        <xpath expr="//sheet" position="before">
                  <header>
                     <!-- fin a good place for the field if i make the header look ugly -->
                    <!-- make invisible -->
                     <field name="current_user" invisible="1"/>
                                                                                        <!-- hope it work like this -->
                    <button name="update_approve" attrs="{'invisible':[('first_approve', '=', current_user)]}" string="Approve" type="object" class="oe_highlight"/>
                    <button name="update_reject" attrs="{'invisible':[('second_approve', '=', current_user)]}" string="Reject" type="object" class="btn-danger"/>
                  </header>
         </xpath>
    

    sorry for my english.

    0 讨论(0)
提交回复
热议问题