This question has been flagged
4 Replies
11330 Views

Hi all,

I have a custom module in hr , I want to set  'remarks' field set as read only for employees, and  I want to set read and write operation access  for manager user .How to achieve this task?


This is my code---------


lass hr_general(osv.osv):

_name = 'hr.general'

_columns = {

'name':fields.many2one('hr.employee','Name',required=True),

'state': fields.selection([('draft', 'To Submit'), ('reject', 'Rejected'), ('validate_hr', 'HR Approved'),('validate_mgr','Manager Approved')],'State'),

'description':fields.char('Subject',required=True),

'date_issue':fields.date('Date'),

'details':fields.text('Details',required=True),

'remarks':fields.text('Remarks')

}

_defaults={

'state':lambda *a: 'draft',

'date_issue': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),

'name': lambda s, cr, uid, c: uid,

}





xml ---------------------


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

<field name="name">hr.general.form</field>

<field name="model">hr.general</field>

<field name="type">form</field>

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

<form string="Genaral" version="7.0">

<header>

<button name="action_button_manager" class="oe_highlight" states="draft" string="Manager Approval" type="object" groups="base.group_hr_user"/>

<button name="action_button_reject" string="Reject" type="object" groups="base.group_hr_user"/>

<button name="action_button_validate" states="validate_mgr" class="oe_highlight" string="HR Approval" type="object" groups="base.group_hr_user"/>

<button name="action_button_draft" string="Reset to Draft" type="object" groups="base.group_hr_user"/>

<field name="state" widget="statusbar" statusbar_visible="draft,reject,validate_hr,validate_mgr" statusbar_colors='{"draft":"blue","reject":"blue","validate_hr":"blue","validate_mgr":"blue"}'/>

</header>

<sheet>

<group>

<field name="name"/>

<field name="description"/>

<field name="date_issue"/>

<field name="details"/>

<field name="remarks"/></group>

</sheet>

</form>

</field>

</record>




=======================

 These are the existing groups ,so how to apply this groups to  my code  groups="base.group_hr_manager,base.group_hr_user,base.group_user"

 

Avatar
Discard
Author

Hello deep, hr.general.form hr.general form

hr.general.form hr.general <xpath expr="//field[@name='remarks']" position="attributes"> 0 when user have access rights for groups "group_hr_manager" after the fields "remarks" is editable othewize it's not editable

Best Answer

Sample code with similar scenario available here

First, set remarks as READONLY in XML and add this view in your XML file


 <record model="ir.ui.view" id="hr_manager_form_view">
	<field name="name">hr.general.form</field>
	<field name="model">hr.general</field>
	<field name="inherit_id" ref="hrgeneral_form_view" />
	<field name="groups_id" eval="[(6,0,[ref('base.group_hr_manager')])]" />
	<field name="arch" type="xml">
		<xpath expr="//field[@name='remarks']" position="attributes">
			<attribute name="readonly">0</attribute>
		</xpath>
	</field>
</record>
Avatar
Discard
Author

i have some daught why you are using inherit_id, this is not an independent model not depending any existing model like (hr.employee ..) i just want to add read only for employees and all other permission given to manager.....

This is done to overwrite the field properties/attributes with a new one but with a group linked to it. Thus the changes are only applied if the group is enabled for the user. Normally, if we have to do a change in XML, we normally inherit the view and do so, the same logic applies here.