how to disable a BUTTON after first click? - odoo

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.

Related

How to hide Edit button based on state in Odoo 11

how can I hide the edit button when the state is "In Progress"
I tried doing ir.rule like this but it didn't work it only filter(domain) my treeview
I also tried to doing it in JavaScript but i can't find any odoo 11 sample
This can be done by inserting conditional CSS.
Frist add a html field with sanitize option set to False:
x_css = fields.Html(
string='CSS',
sanitize=False,
compute='_compute_css',
store=False,
)
Then add a compute method with your own dependances and conditions:
# Modify the "depends"
#api.depends('state_str_modify_me')
def _compute_css(self):
for application in self:
# Modify below condition
if application.state_str_modify_me= 'In Progress':
application.x_css = '<style>.o_form_button_edit {display: none !important;}</style>'
else:
application.x_css = False
Finally add it to the view:
<field name="x_css" invisible="1"/>

How to use Chatter in Odoo?

I want to use Chatter for students model, so that, when value of some field is changed then it is logged under student form
To achieve this, I did the following things:
1. Added this div
<div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers"/>
<field name="message_ids" widget="mail_thread"/>
</div>
in the student form.
It added the chatter, but when i clicked on New Message button, it gave the following error.
This could be because i haven't inherited mail.thread in student model.
Then i inherited this class in student model.
Then it again gave an error as shown below
I search this topic, but couldn't found anything.
It would be appreciated if someone could help me out.
In order to log changes of specific fields you need so set the track_visibility attribute on each field you want to track:
class OpStudent(models.Model):
_name = 'op.student'
_inherits = {
'res.partner': 'partner_id',
}
_inherit = [
'mail.thread',
'ir.needaction_mixin',
]
foo = fields.Char(track_visibility='always')
You may read more about it in the official documentation.
You're using Chatter for keeping the track on student details.
So I'll suggest another module which is working absolutely fine and keeps the track on student or any other model you want as I have personally used it.
I used audit log. It tracks all the CRUD operations. It will create Audit
the menu in setting tab from there you can set the model which you want to track.
For reference you can check this image also.
I had the same problem, but with res.company, I solved it like this:
class ResCompany(models.Model):
_name = 'res.company'
_inherit = ['res.company','mail.thread', 'mail.activity.mixin']
date_end = fields.Date(string="Fin Date",tracking=True)
and in xml:
<xpath expr="//form/sheet" position="after">
<div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers"/>
<field name="message_ids" widget="mail_thread" />
</div>
</xpath>
I hope, that it serves you.

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

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.

different view form for edit and create in odoo

I'd like to know is it possible to have different form views for edit mode and create mode in odoo ?
Actually I just want to hide some elements in create mode and show it in edit mode.
I've tried to using attrs like :
<button name="%(print_invoice)d" string="Cetak Struk" type="action" attrs="{'invisible':[('id', '!=', False)]}" />
But when i open the form it gives me error like this :
Uncaught Error: Unknown field id in domain [["id","!=",false]]
Any help would be appreciated.
Thank you
I have used attrs="{'invisible': [('id', '=', False)]}" to hide a field on creation. You must have id as a (hidden) field in your view, like <field name="id" invisible="1" />
you can easily work around this by using "create_date" as a trafic light.
1st expose the field
# make creation date visible
create_date = fields.Date(
'Data',
invisible=False,
readonly=True,
)
then add it to the form and use it into attrs property
<field name="create_date" invisible="1" />
<ELEM attrs="{'invisible': [('create_date', '!=', False)]}">
[...]
</ELEM>
You can have different views for read, edit and create like this if desired
<div class="oe_read_only">
READ ONLY
</div>
<div class="oe_edit_only" attrs="{'invisible':[('id', '=', False)]}">
EDIT ONLY
</div>
<div attrs="{'invisible':[('id', '!=', False)]}">
CREATE ONLY
</div>
#qatz
You cannot have different views based on "Edit" or "Create" of record.
You can try this by adding "state" field and based on the value of state you can hide show the elements.
Hope this helps !!

how to get value of "active_id" for openerp kanban view

I'm creating a new module where I want to put a button in kanban view of Contact screen. When any user click on that button I need active_id of the selected contact. It is possible to get for tree and form view but I don't know how to do that for kanban view.
Can anyone please help here?
Thanks
You need to use the contex dictionary variably for this purpose ,
If you click on the bu
<button string="Click Me" type="object" name="my_func" context="{'active' : active_id}" />
Method:
def my_func(self, cr, uid, ids, context={}):
# do stuff using the info passed in the context variable...