Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
9 Trả lời
62661 Lượt xem

In Attendance section there is 'name' field used to display/edit date. I am having two user groups 'base.group_user' and 'base.group_hr_user'. I want to use inheritance and make this field readonly for 'base.group_user' group and other group 'base.group_hr_user' should work as it is.

Searched over this community and did some changes like that

<record id="odoo_hr_attendance_view_form" model="ir.ui.view">
            <field name="name">hr.attendance.form.inherit</field>
            <field name="model">hr.attendance</field>
            <field name="inherit_id" ref="hr_attendance.view_attendance_form" />
            <field name="groups_id" eval="[(6,0, [ref('base.group_user')])]" />
            <field name="arch" type="xml">                
                <field name="name" position="attributes">
                   <attribute name="attrs">{'readonly':1}</attribute>                   
                </field>                
            </field>
</record>   

But now it's showing readonly for both the groups.

Can anyone please guide me how to achieve my results?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

It is because user, which is in base.group_hr_user is also in base.group_user - you should consider adding another attribute, that readonly is 0 when user belongs to base.group_hr_user.

Ảnh đại diện
Huỷ bỏ
Tác giả

@Mariusz Thanks for your answer. Tried following code as well but still not working : hr.attendance.form.inherithr.attendance{'readonly':1}hr.attendance.form.inherithr.attendance{'readonly':0}

Tác giả

Tried as per your suggestion by adding one more code block with group base.group_hr_user and readonly 1 but not working :(

Tác giả

Sorry readonly 0

Tác giả Câu trả lời hay nhất

Thanks @Mariusz. I was able to fix the issue as  per your suggestion with following code:

<record id="oodo_hr_attendance_view_form" model="ir.ui.view">
            <field name="name">hr.attendance.form.inherit</field>
            <field name="model">hr.attendance</field>
            <field name="inherit_id" ref="hr_attendance.view_attendance_form" />
            <field name="groups_id" eval="[(6, 0, [ref('base.group_hr_manager') ])]" />                
            <field name="arch" type="xml">                                   
                <field name="name" position="attributes">
                   <attribute name="readonly">0</attribute>                   
                </field>                             
            </field>
</record>
<r
ecord id="ids_hr_attendance_view_form_mgr" model="ir.ui.view">
            <field name="name">hr.attendance.form.inheritmgr</field>

            <field name="model">hr.attendance</field>
            <field name="inherit_id" ref="hr_attendance.view_attendance_form" />
            <field name="groups_id" eval="[(6, 0, [ref('base.group_user') ])]" />                
            <field name="arch" type="xml">                                   
                <field name="name" position="attributes">
                   <attribute name="readonly">1</attribute>                   
                </field>                              
            </field>
</record>    

Ảnh đại diện
Huỷ bỏ

Alternatively it is possible to use the @api.constraints definition, to handle the "access logic" by the server. This is my preferred way, as we use also the XML RPC Api, where this logic have to be covered:

@api.constrains("stage_id")
def check_stage_id_permission(self):
# for the default value on create
if self.env.context.get("disable_stage_check", False):
return True

if (
self.env.ref("my_extension.my_access_group_id").id
not in self.env.user.groups_id.ids
):
raise UserWarning(
_(
"You are not authorized to change stage"
)
)

# on create just call the super with the context to disable the stage change, to allow it as "default" value

@api.model
def create(self, values):
return super(
myMODEL, self.with_context(disable_stage_check=True)
).create(values)

@Andreas - nice solution but having that context to override might be a security issue, if somebody knows the context key can bypass it from any RPC call (browser or API), maybe use sudo() or some other way

Câu trả lời hay nhất

Hi Shiv modi,

i have a view as like below, but the field is still readable. can you please help me on this.

<record id="view_staff_reservation_form" model="ir.ui.view">

<field name="name">reservation.form.view</field>

<field name="model">reservations.reservation</field>

<field name="groups_id" eval="[(6,0, [ref('base.group_library_user')])]" />

<field name="arch" type="xml">

<tree string="Reservation">

<field name="id"></field>

<field name="reserve_item_id"></field>

<field name="from_date"></field>

<field name="to_date"></field>

<field name="is_reserve"></field>

<field name="is_reserve_accepted" position="attributes">

<attribute name="attrs">{'readonly':1}</attribute>

</field>

</tree>

</field>

</record>

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hello Nagarjuna,

Your code is :

<field name="is_reserve_accepted" position="attributes">

<attribute name="attrs">{'readonly':1}</attribute>

</field>

Replace it with:

<field name="is_reserve_accepted" position="attributes">

<attribute name="readonly">1</attribute>

</field>

Hope, it helps you.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 10 24
1436
8
thg 11 19
7580
3
thg 8 24
15733
0
thg 5 23
1613
1
thg 3 23
2166