1

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.

altela
  • 306
  • 2
  • 13
  • 1
    Is there another access group involved for this "assigned + done" privilege? – CZoellner Jan 28 '22 at 09:07
  • Hi @CZoellner thanks for replying, there are no other access group involved. In the same `date_done` field i wish that all user (base.group_user ) have privilege to change the value when the picking state are only in "Ready" but not in "Done" ones. Then, another custom group (change_date_group_privilege) will have the privilege to be able to change correspondent field in all state. Is it possible to achieve in Odoo XML? – altela Jan 28 '22 at 12:19
  • 1
    So another group is involved? xD Because you can make extension views only for a set of access groups. I will write an answer with an example what i mean. – CZoellner Jan 28 '22 at 14:05

2 Answers2

2

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)

Kenly
  • 24,317
  • 7
  • 44
  • 60
1

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!

CZoellner
  • 13,553
  • 3
  • 25
  • 38
  • Thank you so much for your guidance and explanation, i followed the concept you provided and it's working great now! So basically I had to "Inherit" the "Inherited" views that comes from the default views provided by Odoo (inheriting 2 times), and then changes the 2nd inherit position of `attributes` to `replace`. My mistake was trying to merge everything into one views as i believe that there are something like "conditional statement" that accept condition in XML markup language xD My bad, i have to learn more – altela Jan 28 '22 at 15:05