How to hide Edit button based on state in Odoo 11 - odoo

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"/>

Related

How to update a value from a field without refreshing the page Odoo

I'm creating a new sector on the settings page of my module, where I have a value and an update icon, which aims to update the field value when I click on the icon.
But when I call the function to execute the function, the page is reloaded and I never get the value, but the value is printed in the terminal with a logger, does anyone have any suggestions?
My XML code:
<button type="object" name="refresh_credits" class="btn-link" icon="fa-refresh"/>
<span class="btn-link">Credits</span>
<field name="new_credits"/>
My python code inside a class:
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
new_credits = fields.Integer()
def refresh_credits(self):
data_details_credits = self.env['show.credits'].content_credits_info()
_logger.info(self.env['show.credits'].content_credits_info()[4])
self.new_credits = data_details_credits[4]
You have to override set_values and get_values method to update the values in settings. We cannot directly update the values in general settings.
Below is the example:
def set_values(self):
super(ResConfigSettings, self).set_values()
self.env['ir.config_parameter'].sudo().set_param('MODULE_NAME.new_credits',
self.new_credits)
#api.model
def get_values(self):
res = super(ResConfigSettings, self).get_values()
config = self.env['ir.config_parameter'].sudo()
config_key = config.get_param('MODULE_NAME.new_credits')
if config_zoom_key:
res.update(
field_name=config_key
)
return res
And also for better understanding, watch this video:
Add fields general settings

How to set dynamic domain for many2one field without onchange trigger?

I have a class in module
class CRMProjects(models.Model):
_name = 'crm.projects'
product_lines = fields.Many2many('crm.project.product.lines')
and a class in another module
class CoRG(models.Model):
_inherit = 'crm.lead'
project = fields.Many2one(comodel_name="crm.projects")
product_lines = fields.Many2one('crm.project.product.lines')
the first module has a button that opens new form from the second module and easily set dynamic domain on product_lines triggered by an onchange
domain = {'product_lines': [('id', 'in', self.project.product_lines.ids)]}
the problem is that if I go to the screen of records created from that form and edit the project_lines, the domain won't work
I tried to use
domain:lambda self:[('id', 'in', self.project.product_lines.ids)]
in the field of the 2nd module but this didn't work, any suggestions?
You do not need to set domain since the fields are related.
Change the as following:
class CoRG(models.Model):
_inherit = 'crm.lead'
project = fields.Many2one(comodel_name="crm.projects")
product_lines = fields.Many2one(related='project.product_lines')
Abdulrahman
You can set the default domain on many2one field like this,no need to trigger onchange
#api.model
def _default_tax_group(self):
return self.env['account.tax.group'].search([], limit=1)
tax_group_id = fields.Many2one('account.tax.group', string="Tax Group", default=_default_tax_group, required=True)
Thanks
Solved the issue using the following implementation
test1 = fields.Many2many('crm.project.product.lines', related='project.product_lines')
product_lines = fields.Many2one('crm.project.product.lines', domain="[('id', 'in', test1)]")
Thanks for your suggestions.

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.

Button in tree view odoo 9

I need a button in tree view for all line. After clicking on the button I need get line id.
I'm trying, but not working:
*.xml
<button name="copy_line" class="text-right" icon="fa-files-o" type="object"/>
*.py
#api.multi
def copy_line(self):
print("Not come here!")
for r in self:
print(r.id)
object has no attribute 'copy_line'
To call the method on button click that record should be saved.
But in this case, Record was not saved so you are not able to call the method on button click.
Alternet way is, you can create a new line based on onchange or button in the footer and add self._cr.commit() to commit and raise ValidationError.
You define copy_line in the wrong model.
If your button is included inside a tree view defined for a One2Many field line_ids and that field is referencing object.line, you method copy_line should be created in that model.
For example:
line_ids = fields.One2Many('object.line', 'ref_id', string='Lines')
class ObjectLine(models.Model):
_name = 'object.line'
#api.multi
def copy_line(self):
print("Not come here!")
for r in self:
print(r.id)

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...