1

I'm trying to create a form view.

<field name="is_positive" attrs="{'readonly':[('state','==','final')]}"/>

However there is many attributes like groups and invisible related to authorization so that certain group of people can see the field.

groups="base.group_hr_user"

But Is there a way for certain group can edit the field and the other group cannot?

Eric Lee
  • 700
  • 2
  • 9
  • 30
  • You can take reference from this answer : https://stackoverflow.com/questions/18912997/how-to-make-field-readonly-based-on-group-and-status – Keval Mehta Aug 03 '17 at 05:13

3 Answers3

4

add a new field to check whether the user is manager or user.

New Api Method

check_user = fields.Boolean(string='user',compute='_compute_user_check')

@api.multi
def _compute_user_check(self):
    if self.user_has_groups('purchase.group_purchase_manager'):
        self.check_user =True

In view

<field name="is_positive" attrs="{'readonly':[('check_user','=','True')]}"/>
Vishnu VaNnErI
  • 931
  • 6
  • 18
  • I don't think that self would have a method `user_has_groups`. @aslamsha22's answer is better. – Majikat Aug 03 '17 at 09:16
  • 1
    Please --> https://github.com/odoo/odoo/search?p=2&q=user_has_groups&type=&utf8=%E2%9C%93 – Vishnu VaNnErI Aug 07 '17 at 05:47
  • OH. My bad. That is a super handy functionnality you showed there. But still, your compute method is returning a value instead of setting check_user. – Majikat Aug 07 '17 at 08:26
3

First of all, you cannot use a domain like this one

<field name="is_positive" attrs="{'readonly':[('state','==','final')]}"/>

There is not a '==' operator, use = instead.

Now, to answer your question, if you want to create a special view for another group in which some elements are readonly for one group, and editable in the other, you have to it this way.

For the default view :

<record id="some_model_view" model="ir.ui.view">
    <field name="name">some.model.form</field>
    <field name="model">some.model</field>
    <field name="arch" type="xml">
        <form>
             <field name="some_field" readonly="1"/>
        </form>
    <field/>
</record>

For a certain group :

<record id="some_model_view_for_other_group" model="ir.ui.view">
    <field name="name">some.model.form</field>
    <field name="model">some.model</field>
    <field name="inherit_id" ref="my_module.some_model_view"
    <field name="groups_id" eval="[(6, 0, [ref('some.first_group')])]" />
    <field name="arch" type="xml">
        <field name="some_field" position="attributes">
            <attribute name="readonly">0</attribute>
        </field>
    <field/>
</record>
KbiR
  • 4,047
  • 6
  • 37
  • 103
Majikat
  • 722
  • 4
  • 13
  • you answer is more efficient nice answer but you didn't use xpath expression does it work like this? – Charif DZ Aug 03 '17 at 07:40
  • 1
    Yes it works. It works if there are no ambiguity in elements that are called. With Xpath, you have more control. But if, let's say, you are sure that there is only one field named `some_field` in the form, you can use this syntax to code faster. – Majikat Aug 03 '17 at 09:08
  • Thanks! It worked for me and This is the one I was looking for to use Odoo efficiently. – Eric Lee Aug 04 '17 at 00:48
1

I will show one example to how this functionality works in sale group.

I make the unit price field in the sale order line makes readonly we select the user group user:own documents only The field is editable for other 2 groups user:All documets and manager

Firstly I create a boolean field for checking the user belongs to which group

is_own_user = fields.Boolean(string="Own user", compute='compute_own_user')

Then assigns the boolean field is True when the user belongs the group user:own documents only otherwise assigns to False

@api.depends('product_id')
def compute_own_user(self):
    res_user_id = self.env['res.users'].search([('id', '=', self._uid)])
    for rec in self:
        if res_user_id.has_group('sales_team.group_sale_salesman') and not res_user_id.has_group('sales_team.group_sale_salesman_all_leads'):
            rec.is_own_user = True
        else:
            rec.is_own_user = False

in xml make is_own_user invisible and replaces the unit price field

<xpath expr="//notebook/page/field[@name='order_line']/tree/field[@name='price_unit']" position="replace">
    <field name="price_unit" attrs="{'readonly': [('isown_user', '=', True)]}" />
</xpath>
code_explorer
  • 472
  • 6
  • 18