Hide edit button Odoo15 - odoo

How can i hide the edit button depends on the condition?
For example: If the john created the data nobody can change it. So hide the edit button..
<record id="hideEdit_rule" model="ir.rule">
<field name="name">Hide Edit </field>
<field name="model_id" ref="model_hospital_patient" />
<field name="domain_force">[('user', '=', 'john')]</field>
<field name="groups" eval="[(4, ref('om_hospital.hospital_management_pratisyen_doctor'))]"/>
<field name="perm_read" eval="True"/>
<field name="perm_write" eval="False"/>
<field name="perm_create" eval="True"/>
<field name="perm_unlink" eval="True"/>
</record>
I have already tried this code if the created user is john, edit is false.. It does not work.

Record rules are conditions that records must satisfy for an operation (create, read, update or delete) to be allowed.
The record rule above will be checked when a user tries to read, create or delete a hospital.patient record. to check the user who created a record use create_uid on the left and use the context value user to get the current user record.
To check if the current user created the record, use the following domain:
<field name="domain_force">[('create_uid', '=', user.id)]</field>
It can't be used to hide the Edit button since access rules are checked after calling the write method
There is an OCA module (Web Access Rules Buttons) that disables the Edit button on the form views if the user cannot edit the current record according to the record access rules.
Usage:
When using Odoo, even if a user has no rights to edit a record, the Edit button is shown. The user can edit the record but won't be able to save his changes. Now, the user won't be able to click on the Edit button.
Now the module is available for version 14.0, but if you update the manifest file like the following, it should work.
Comment the following line:
"views/web_access_rule_buttons.xml",
Add the following entry:
"assets": {'web.assets_backend': ['web_access_rule_buttons/static/src/js/form_controller.js'],},
Try with following record rule:
<record id="hideEdit_rule" model="ir.rule">
<field name="name">Hide Edit</field>
<field name="model_id" ref="model_hospital_patient"/>
<field name="domain_force">[('create_uid', '=', user.id)]</field>
<field name="groups" eval="[(4, ref('om_hospital.hospital_management_pratisyen_doctor'))]"/>
<field name="perm_write" eval="True"/>
</record>

You can overide fields_view_get method and disable edit according to condition.
#api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
res = super(<MODEL NAME>, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
if view_type == 'form' and <ADD YOUR CONDITION HERE>:
root = etree.fromstring(res['arch'])
root.set('edit', 'false')
res['arch'] = etree.tostring(root)
else:
pass
return res

Related

the first record is not processed by the create method, the second one is

I'm trying to add some demo data to a module
one of the models extends res.partner and has a create method (it overrides it from the parent)
The create method does 2 things
it checks that the current user is in some groups
it populates a serial number field with a value (calculated with a sequence)
This is the code
#api.model
def create(self, vals_list):
if not self.env.user.has_group('my-project.group_super') and not self.env.user.has_group(
'my-project.group_some_other_group'):
raise ValidationError(
f"Warning! You have no permission to create a patient. Contact your administrator")
vals_list['patient_number'] = self.env['ir.sequence'].next_by_code('my-project.res.partner')
return super(Patient, self).create(vals_list)
please, note the ValidationError in this method. The message it reports is
Warning! You have no permission to create a patient. Contact your
administrator
in the xml file I'm using to add demo data to this model I have this record
<record id="johnsmith" model="res.partner">
<field name="active" eval="True"></field>
<field name="is_patient" eval="True"></field>
<field name="company_type">person</field>
<field name="firstname">John</field>
<field name="lastname">Smith</field>
<field name="phone">+39 345 345 345</field>
<field name="email">asdfasdfasd</field>
<field name="birthdate">1999-02-17</field>
<field name="place_of_birth">Milan</field>
<field name="fiscalcode">IUCGIUCHEUIOHD38</field>
<field name="city">Milan</field>
<field name="country_id" ref="base.it"></field>
<field name="gender">male</field>
<field name="type">contact</field>
</record>
This record mostly works.
Meaning, it shows up as a demo record when I load demo data.
I say "mostly" because the field patient-number is not populated
But then I copy and paste this record and I only change the id to johnsmith2, like this
<record id="johnsmith2" model="res.partner">
<field name="active" eval="True"></field>
<field name="is_patient" eval="True"></field>
<field name="company_type">person</field>
<field name="firstname">John</field>
<field name="lastname">Smith</field>
<field name="phone">+39 345 345 345</field>
<field name="email">asdfasdfasd</field>
<field name="birthdate">1999-02-17</field>
<field name="place_of_birth">Milan</field>
<field name="fiscalcode">IUCGIUCHEUIOHD38</field>
<field name="city">Milan</field>
<field name="country_id" ref="base.it"></field>
<field name="gender">male</field>
<field name="type">contact</field>
</record>
this second record doesn't work
when installing the module in a database with demo data enabled, I get the error message in the body of the create method override
Warning! You have no permission to create a patient. Contact your
administrator
in the log and the loading of demo data fails
It seems that the first record is not being processed by the create method override, the second one is
what is going on here ?
EDIT
I'm adding the definition of user groups as this came up in the comments
These are the 2 groups mentioned in the create method
<record id="my_project" model="ir.module.category">
<field name="name">My Project</field>
<field name="description">Users groups for My project</field>
</record>
[...]
<record id="group_super" model="res.groups">
<field name="name">Supervisor</field>
<field name="category_id" ref="my-project.my_project"/>
</record>
[...]
<record id="group_some_other_group" model="res.groups">
<field name="name">Some Other Group</field>
<field name="category_id" ref="my-project.my_project"/>
</record>
These are the relevant lines in the ir.model.access.csv file
sc_study_partner_super,res.partner.super,model_res_partner,group_super,1,1,1,1
[...]
sc_study_partner_some_other_role,res.partner.some_other_role,model_res_partner,group_some_other_group,1,1,1,1
The demo/patients.xml file does not contain the no update flag
You can use self.env.is_admin() to check if its runned by administrator environment
Also, it is common to add rights to the super user and the admisstrator:
<record model="res.users" id="base.user_root">
<field eval="[(4,ref('my-project.group_super'))]" name="groups_id"/>
</record>
<record model="res.users" id="base.user_admin">
<field eval="[(4,ref('my-project.group_super'))]" name="groups_id"/>
</record>

How to filter data in odoo calendar?

i want to filter some data in standart odoo calendar. I need to dont show events, that is private for users, who dont participate that event, i can do this by adding new filter in views, like:
<filter string="new filter" name="dont_show_others_private_events" domain="['|', ('privacy','=','public'), '&' ,('partner_ids.user_ids', 'in', [uid]), ('privacy', '=', 'private')]"/>
but problem is, that this filter is visible and any user can turn it off.
How can i do the same on server level, maybe in models.py file? or there is way to hide this filter but leave it active? i tried also white this filter in views:
<record id="action_calendar_event_type" model="ir.actions.act_window">
<field name="name">Meeting Types</field>
<field name="res_model">calendar.event.type</field>
<field name="view_id" ref="view_calendar_event_type_tree"/>
<field name="domain">['|', ('privacy','=','public'), '&' ,('partner_ids.user_ids', 'in', [uid]), ('privacy', '=', 'private')]</field>
</record>
but is doesnt work for me
if i understood your question correctly you want this "filter" to always be active and users cannot disable it ?
If that is the case then add a new Record Rule and not a filter.
You can add the new entry in the xml under the Security folder.
And here is an example of the record rule:
<record id="private_events_filter" model="ir.rule">
<field name="name">Private events filter</field>
<field name="model_id" ref="calendar.event"/>
<field name="domain_force">['|', ('privacy','=','public'), '&' ,('partner_ids.user_ids', 'in', [uid]), ('privacy', '=', 'private')]</field>
<field name="groups" eval="[(4, ref('base.group_portal'))]"/>
</record>
this works:
<record id="calendar_event_rule_private_dont_show_others" model="ir.rule">
<field name="model_id" ref="model_calendar_event"/>
<field name="name">Dont show other private ivents</field>
<field name="domain_force">['|', ('privacy','=','public'), '&' ,('partner_ids.user_ids', 'in', [user.id]), ('privacy', '=', 'private')]</field>
<field name="perm_read" eval="True"/>
<field name="perm_write" eval="True"/>
<field name="perm_create" eval="True"/>
<field name="perm_unlink" eval="True"/>
</record>

Odoo triggering a function

I have a module what lock the Sale Order.
I want to do auto-triggering this function when a Field in settings is True.
Because at this moment it's call the function only when i hit a button.
There is how i check if a field value is 'set':
#api.multi
def auto_order_finishing(self):
field_value = self.env['ir.config_parameter'].sudo().get_param('sale.activate_automate_so_locking')
if field_value:
self.confirm_finish_order()
return True
Please check server action, you can achieve your feature with that.
You need to add python code & event on which that code will be applied.
You can create a scheduled action to some function that calls auto_order_finishing on all relevant sale orders at a specific interval.
You can find examples of these by searching for model="ir.cron"
I've pasted an example below
<record forcecreate="True" id="ir_cron_mail_scheduler_action" model="ir.cron">
<field name="name">Mail: Email Queue Manager</field>
<field name="model_id" ref="model_mail_mail"/>
<field name="state">code</field>
<field name="code">model.process_email_queue()</field>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">1</field>
<field name="interval_type">hours</field>
<field name="numbercall">-1</field>
<field eval="False" name="doall"/>
</record>

Odoo 9 Is there a way to handle authorization with different groups on a certain field in form view?

I'm trying to create a form view.
<field name="is_positive" attrs="{'readonly':[('state','==','final')]}"/>
However there is many attributes like groups and invisible related to authorization so that certain group of people can see the field.
groups="base.group_hr_user"
But Is there a way for certain group can edit the field and the other group cannot?
add a new field to check whether the user is manager or user.
New Api Method
check_user = fields.Boolean(string='user',compute='_compute_user_check')
#api.multi
def _compute_user_check(self):
if self.user_has_groups('purchase.group_purchase_manager'):
self.check_user =True
In view
<field name="is_positive" attrs="{'readonly':[('check_user','=','True')]}"/>
First of all, you cannot use a domain like this one
<field name="is_positive" attrs="{'readonly':[('state','==','final')]}"/>
There is not a '==' operator, use = instead.
Now, to answer your question, if you want to create a special view for another group in which some elements are readonly for one group, and editable in the other, you have to it this way.
For the default view :
<record id="some_model_view" model="ir.ui.view">
<field name="name">some.model.form</field>
<field name="model">some.model</field>
<field name="arch" type="xml">
<form>
<field name="some_field" readonly="1"/>
</form>
<field/>
</record>
For a certain group :
<record id="some_model_view_for_other_group" model="ir.ui.view">
<field name="name">some.model.form</field>
<field name="model">some.model</field>
<field name="inherit_id" ref="my_module.some_model_view"
<field name="groups_id" eval="[(6, 0, [ref('some.first_group')])]" />
<field name="arch" type="xml">
<field name="some_field" position="attributes">
<attribute name="readonly">0</attribute>
</field>
<field/>
</record>
I will show one example to how this functionality works in sale group.
I make the unit price field in the sale order line makes readonly we select the user group user:own documents only The field is editable for other 2 groups user:All documets and manager
Firstly I create a boolean field for checking the user belongs to which group
is_own_user = fields.Boolean(string="Own user", compute='compute_own_user')
Then assigns the boolean field is True when the user belongs the group user:own documents only otherwise assigns to False
#api.depends('product_id')
def compute_own_user(self):
res_user_id = self.env['res.users'].search([('id', '=', self._uid)])
for rec in self:
if res_user_id.has_group('sales_team.group_sale_salesman') and not res_user_id.has_group('sales_team.group_sale_salesman_all_leads'):
rec.is_own_user = True
else:
rec.is_own_user = False
in xml make is_own_user invisible and replaces the unit price field
<xpath expr="//notebook/page/field[#name='order_line']/tree/field[#name='price_unit']" position="replace">
<field name="price_unit" attrs="{'readonly': [('isown_user', '=', True)]}" />
</xpath>

What is the right odoo 8 XML syntax for view_ids field

I am trying to customize the crm lead object. Class is defined this way
class yvleads(models.Model):
_inherit = 'crm.lead'
_name = 'crm.lead'
Now I added a menu item to display the added elements for which I have both a tree view and a form view. I have added a lett menu item referening a ir.actions.act_window. When this action is defined as
<record model="ir.actions.act_window" id="yvleads_mgt">
<field name="name">Leads Yves</field>
<field name="res_model">crm.lead</field>
<field name="view_type">form</field>
<field name="view_mode">form,tree</field>
<field name="view_id" ref="tree_view_yves_leads"/>
</record>
this works fine for the list-tree view but when I click on any item or the create button, I get the default form view for crm.
To add my customized view for form also, my understanding of odoo documentation is that I should use view_ids element but I was not able to get it right
I have tried several syntaxes with/without brackets, using eval="" or inside the xml definition but with no success. Searching this forum for string name="view_ids" was not very helfull, may be it is not the best practice to do that? any help appreciated
<record model="ir.actions.act_window" id="yvleads_mgt">
<field name="name">Leads Yves</field>
<field name="res_model">crm.lead</field>
<field name="view_type">form</field>
<field name="view_mode">form,tree</field>
<field name="view_ids">(tree,tree_view_yves_leads),(form,form_view_yves_leads)</field>
</record>
this is the right syntax :
<field name="view_ids"
eval="
[
(5, 0, 0),
(0, 0, {'view_mode': 'tree', 'view_id': ref('tree_external_id')}),
(0, 0, {'view_mode': 'form', 'view_id': ref('form_external_id')}),
]"
/>
The view_id you used in your action is valid only when you click on the menu connected to that action.
What you need to use is view inheritance, i.e. you need to redefine the existing views:
<record id="new_view" model="ir.ui.view">
<field name="name">new view</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm_lead.existing_view_id"/>
<field name="arch" type="xml">
<data>
...
</data>
</field>
</record>
inside the data tag you can accomplish the modifications you need following the instructions in the provided link.
In theory you could also change the whole view, but this could ba problematic if other modules inherits that same view.
This way every time you open crm.lead model in any way, your view is used.
Btw the correct syntax for view_ids would be:
<field name="view_ids" eval="[(6, False, [ref('view_id_1'), ref('view_id_2')])]">
I think very useful for this link so please check it.
https://www.odoo.com/forum/help-1/question/for-which-reason-can-i-add-a-value-to-view-ids-of-a-window-action-97682