altela
altela

Reputation: 354

Odoo Add Multiple Group With Different Condition For the Same Field

Hope all is well for you guys, gday.

So I inherited a field called date_done from stock.picking model, which has readonly:true attribute by default. I revoked it to readonly but not in 'assigned' status for all Odoo user in the code below :

<field name="date_done" groups="base.group_user" position="attributes" >
    <attribute name="attrs">{'readonly':[('state','not in',('assigned'))],'invisible':['|',('state','not in',('assigned','done'))]}</attribute>
</field>

Then, i want to add one group called change_date_group_privilege that later will have readonly but not in 'assigned' + 'done' status attributes.

In the end, i expect that the base.group_user will only able to change field when the status is not in assigned, and change_date_group_privilege will be able to change when the status is not in assigned + done.

I cannot simply add change_date_group_privilege into groups="base.group_user", because i also need to add 'done' status inside readonly':[('state','not in',('assigned'))], which later will also give base.group_user privilege that i don't want.

How is it possible to add condition in this XML block of code? And if there's other way i'd be glad to get through it.

Thank you for your help.

Upvotes: 1

Views: 3154

Answers (2)

Kenly
Kenly

Reputation: 26678

You can use several times a field in a form view and define the attributes separately for internal users and privileged users using the groups attribute.

Check the form view documentation for Semantic components:

renders (and allow editing of, possibly) a single field of the current record. Using
several times a field in a form view is supported and the fields can receive different
values for modifiers ‘invisible’ and ‘readonly’. However, the behavior is not
guaranteed when several fields exist with different values for modifier ‘required’.

You can precede the group by ! to make the component invisible if a user does not belong to that group.

Example:

<field name="date_done" position="replace">
    <label for="date_done"/>
    <div>
        <div groups="!MODULE_NAME.change_date_group_privilege">
            <field name="date_done" 
                   attrs="{'readonly':[('state','not in',('assigned'))]}"/>
        </div>
        <div groups="MODULE_NAME.change_date_group_privilege">
            <field name="date_done" 
                   attrs="{'readonly':[('state','not in',('assigned', 'done'))]}"/>
        </div>
    </div>
</field>

Note that Odoo will use the value of the last field when using the groups attribute on the field tag

The following domain (used in attrs domain):

['|',('state','not in',('assigned','done'))]  

is wrong because | has arity 2 (need two criterion)

Upvotes: 2

CZoellner
CZoellner

Reputation: 14768

Presumptions:

  • you have two level of access groups
  • one normal group (typically base.group_user "Internal Users")
  • and one special group "Change Date on Pickings when done" with id my_module.group_change_date_when_done

So the normal Picking view looks like this (short example!)

<record id="default_view" model="ir.ui.view">
    <field name="name">default example view</field>
    <field name="model">stock.picking</field>
    <field name="arch" type="xml">
        <form>
            <field name="date_done" readonly="1" />
            <field name="state" />
        </form>
    </field>
</record>

Now the requirements wants normal users to have the possibility to change the date_done field while the state is not assigned. So you extend and change the view for every normal user (no groups needed in view).

<record id="default_view_extension_level1" model="ir.ui.view">
    <field name="name">default example view</field>
    <field name="model">stock.picking</field>
    <field name="inherit_id" ref="default_view" />
    <field name="arch" type="xml">
        <field name="date_done" position="attributes">
            <attribute name="readonly" />
            <attribute name="attrs">{'readonly':[('state','not in',('assigned'))]</attribute>
        </field>
    </field>
</record>

Now you can set on view on top of that and just add the special group to it. That will lead to a use of that special view only for users in that special group.

<record id="default_view_extension_level2" model="ir.ui.view">
    <field name="name">default example view</field>
    <field name="model">stock.picking</field>
    <field name="inherit_id" ref="default_view_extension_level1" />
    <field name="groups_id" eval="[(4, ref('my_module.group_change_date_when_done'))]"/>
    <field name="arch" type="xml">
        <field name="date_done" position="replace">
            <field name="date_done" attrs="{'readonly':[('state','not in',('done', 'assigned'))]" />
        </field>
    </field>
</record>

In the end normal users will load the first two views and special users will also load the third view which will lead them to change the done date in done state.

Keep in mind: that code is just example code to show the principle of extensions and using groups on views. It's not guaranteed to work!

Upvotes: 1

Related Questions