How can I make a field readonly from fields_view_get in OpenERP7? - openerp-7

What I want:
Based on two context variables which must BOTH exist (one default value, and a flag), I must make a field readonly. If either of the context variables is not present, the field should be editable as usual.
I have this file:
from osv import fields, osv
from lxml import etree
class ir_sequence(osv.osv):
_name = 'ir.sequence'
_inherit = 'ir.sequence'
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
"""
We set the field to readonly depending on the passed flags. This means:
* We must specify to fix the sequence type (fixed_sequence_type=True).
* We must specify a default value for the "code" (default_code=my.custom.seq.code).
This only applies to context (e.g. a context node in a ir.action.act_window object, or a <field /> tag for a
relational field to this object, with a context with these values).
"""
res = super(ir_sequence, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type,
context=context, toolbar=toolbar, submenu=submenu)
context = context or {}
is_fixed = context.get('fixed_sequence_type', False) and bool(context.get('default_code', False))
if is_fixed and 'code' in res['fields']:
res['fields']['code']['readonly'] = 1
#arch = etree.XML(res['arch'])
#for node in arch.xpath("//field[#name='code']"):
# node.set('readonly', '1')
#res['arch'] = etree.tostring(arch)
return res
ir_sequence()
And I tried two alternatives to change the readonly attribute of a field to True when a condition was met (condition is given by the is_fixed variable - by debugging I see that it gets the True value it needs, when I trigger it in the intended manner).
The first alternative was edit the arch content as XML, find the node for the field 'code', and fix it. The code for that alternative is commented.
The second alternative was edit the fields dictionary, find the 'code' field, and set readonly=True on that field.
Neither of them worked (symptoms: the field is not readonly when the condition evaluates to True).
What must I do to make it work?

Try this,
<field name="field_name" invisible="context.get('flag1',False) and context.get('flag2',False)" />
you can pass context to list view using action which is bind to the list view.
<record id="action_account_tree1" model="ir.actions.act_window">
<field name="name">Analytic Items</field>
<field name="res_model">account.analytic.line</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="context">{'flag1':True,'flag2':True}</field>
</record>
You have to manage logical conditions as per your need.
No need to override fields_view_get if your aim is to hide fields from list view based on context then.
Thanks.

Related

How to make a field readonly if the field store a value odoo 12?

I tried like this given below but its not getting what i wanted,what im trying to aquire is if that field(Char) is updated(stored a value) by model B it needs to be readonly and else by other model fields needs to be not readonly :
<field name = 'name' attrs="{'readonly': [('name','=', True)]}"/>
To check if a char field is not set in attrs, compare it to False, you can find an example in the account partner view.
Example:
<field name='name' attrs="{'readonly': [('name','!=', False)]}"/>
If we use the above definition and try to set the field value, the field value will not be saved because the field is read-only, to avoid that we need to set the force_save attribute to True to force Odoo to save the value on the read-only field.
Example:
<field name='name' attrs="{'readonly': [('name','!=', False)]}" force_save='True'/>

how to disable a BUTTON after first click?

This might be a simple question.but,does any one know how to disable a button after clicking it in Odoo? Thanks for all your help.
Most of Odoo module use state for this kind of problem. the general idea of it that you must have a field and the button is displayed based on the value of that field.
Ex:
# in you model
bool_field = fields.Boolean('Same text', default=False)
In your view:
<button name="some_method"......... attrs="{'invisible': [('bool_field', '=', True)]}" />
...
...
...
<!-- keep the field invisible because i don't need the user to see
it, the value of this field is for technical purpuse -->
<field name="bool_field" invisible="1"/>
In your model:
#api.multi
def some_method(self):
# in this method i will make sure to change the value of the
# field to True so the button is not visible any more for user
self.bool_field = True
...
...
...
So if you all ready have a field that the button change it's value you can use it directly
or create a special field for this purpose.

Warehouse stock restriction not working in edit mode odoo10?

In my custom module I use stock picking type to see the warehouse operations of the user.
class ResUsers(models.Model):
_inherit = 'res.users'
default_picking_type_ids = fields.Many2many(
'stock.picking.type', 'stock_picking_type_users_rel',
'user_id', 'picking_type_id', string='Warehouse Operations')
I added this in user form.
In my sexurity.xml file add
<record id="filter_user_stock_picking_type" model="ir.rule">
<field name="name">Filter Stock Picking Type</field>
<field name="model_id" search="[('model','=','stock.picking.type')]" model="ir.model"/>
<field name="groups" eval="[(4, ref('base.group_user'))]"/>
<field name="domain_force">[('id','in', [ s.id for s in user.default_picking_type_ids ])]</field>
</record>
so it is worked in when a user is created and assigns stock operation.
But when changed the stock operation to particular user, it will not affect.
How to resolve this issue??
I think, the problem is the Odoo Cache.
The old values are stored in the Cache for a long time and your change will not take any effect.
You can try it to clear the cache. It helped me to solve the similar problem.
class User(models.Model):
_inherit = 'res.users'
#api.multi
def write(self, vals):
ret = super(User, self).write(vals)
if 'default_picking_type_ids' in vals:
# clear default ir values when default_picking_type_ids changes
self.env['ir.values'].get_defaults_dict.clear_cache(self.env['ir.values'])
return ret

How to make the whole record readonly in openerp7

I know to make a field readonly with the "readonly" attribute. Is it possible to make the entire record readonly. That means that all field in a form should be readonly on a condition.
One insignificant way i found is to make this attrs="{'readonly':[('state','=','close')]}" in all the fileds present in the form.
<field name="responsible_id" class="oe_inline" attrs="{'readonly':
<field name="type" attrs="{ 'readonly':[('state','=','close')]}" class="oe_inline"/>
<field name="send_response" attrs="{'readonly':[('state','=','close')]}"/>[('state','=','close')]}"/>
However i don't think this be the right one. I expect some way to put readonly attribut common for the form. Kindly suggest.
In my example, People can view all the records and edit only their own records.
Thank You.
Put this in your Python imports:
from lxml import etree
from openerp.osv.orm import setup_modifiers
And modify the fields in the fields_view_get method like this:
def fields_view_get(self, cr, uid, view_id=None, view_type=None, context=None, toolbar=False, submenu=False):
res = super(MyClass, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type,
context=context, toolbar=toolbar, submenu=submenu)
if view_type == 'form':
# Set all fields read only when state is close.
doc = etree.XML(res['arch'])
for node in doc.xpath("//field"):
node.set('attrs', "{'readonly': [('state', '=', 'close')]}")
node_name = node.get('name')
setup_modifiers(node, res['fields'][node_name])
res['arch'] = etree.tostring(doc)
return res
This will modify every field on the form to include the attrs="{'readonly':[('state','=','close')]}" attribute.
put a group on entire form and use attrs to make it read only.

One Module fields use in other module

I am trying to use some fields of "Job Positions" in "Opportunities" and we can say it as a merger fields. but i am unable to do this task . i have no knowledge about Python language.
I have created a user defined fields and use it in opportunities by XML coding by developer options . I know that it is easy because user defined fields has "crm.lead" named module which is same in opportunities.
but now When i want to use this fields in "hr.job" then it give me error "fields not found" . i know that this fields is not in current module and it is part of "crm.lead" not "hr.job".
Is it possible to use one module fields in another module?
Yes you can do so. First you have to create an object for that and then browse the record and fetch the value of the field which you want.
For example create one method and then browse the crm.lead record:
crm_obj = self.pool.get('crm.lead')
crm_brw = crm_obj.browse(cr, uid, crm_rec_id, context=context)
print "my field value:: ", crm_brw.your_field
Here, "crm_rec_id" is the id of the record of crm.lead object
There are lots of examples given in addons.
yes you can it done by _inherits.
for eg.
hr_job in hr module .
class hr_job(osv.osv):
_name = "hr.job"
_description = "job position"
_columns = {
'name': fields.char('job name', size=64)
}
crm_lead in crm module.
class crm_lead(osv.osv):
_name = "crm.lead"
_inherits = {'hr.job': 'job_id'}
_description = "Lead/Opportunity"
_columns = {
'partner_id': fields.many2one('res.partner', 'Partner')
}
in xml file of crm create form view.
<record id="crm_lead_form" model="ir.ui.view">
<field name="name">crm.lead</field>
<field name="model">crm.lead</field>
<field name="arch" type="xml">
<form>
<field name="name"/> # job name from hr_job
<field name="partner_id"/> # partner_id from crm.lead
</form>
</field>
</record>
don't forget add dependency.