How To Write Odoo Record Rules? - python-3.x

I am working on a project, and I have failed to bypass this Record Rule
['|',('company_id','=',False),('company_id', 'in', company_ids)]
What I want to achieve is that if Company A makes a request to Company B for a medical(lab) test request, this request should be available in both Company Lab Request List View.
I have a field called laboratory_id (which is the same as company_id).
How can I use laboratory_id in a record rule to show both the requests to both Company A & B?

You can write your rule like that.
<record id="request_list" model="ir.rule">
<field name="name">request.rule</field>
<field name="model_id" ref="your_module.your_model_name"/>
<field name="domain_force">['|',('laboratory_id','=',False),('laboratory_id', 'in', company_ids)]</field>
<field name="groups" eval="[(4, ref('base.group_user'))]"/>
</record>

Please re-write the domain as below:
['|','|',('company_id','=',False),('company_id', 'in', company_ids),('laboratory_id', 'in', company_ids)]

Related

Odoo search Control panel model extension failed to evaluate domain

Im doing the odoo getting started tutorial and I get an error that I dont understand and dont know how to debug it. Im adding the search record, and have the following now.
<record id="estate_property_search" model="ir.ui.view">
<field name="name">estate.property.view.search</field>
<field name="model">estate.property</field>
<field name="arch" type="xml">
<search string="Search Opportunities">
<field name="name"/>
<field name="postcode"/>
<field name="expected_price"/>
<field name="bedrooms"/>
<field name="living_area"/>
<field name="facades"/>
<filter string="Available" name="available" domain="[('date_availability', '=', context_today() )]"/>
<filter string="Archived" name="inactive" domain=" [('active', '=', False )]"/>
</search>
</field>
</record>
The archived filter is working correctly, the available filter is giving me the error:
Error: Control panel model extension failed to evaluate domain:/n{}
I'm pretty sure that this error is the result of code that is not correct because if I replace it False, then it works, and if I replace it with something random like toooday() then i get the same error. However, I see many examples on the internet using this code so I think it should work. I also tried odoo.fields.Date.context_today() and that doesnt work either.
It could be that Odoo is evaluating the domain on module update to get a safe domain for later use.
So context_today() alone is not working when evaluating because you can't use a pure date object in a domain. Just add .strftime('%Y-%m-%d') to it, and it should evaluate without an error.

Odoo hide some view type on specific model

I need the following view types on CRM hide calendar,pivot,graph,cohort,dashboard using a custom module
I tried with this without success
<record id="crm_action" model="ir.actions.act_window">
<field name="name">CRM views</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">crm.lead</field>
<field name="view_type">crm.lead.kanban</field>
<field name="view_mode">tree,form,calendar,graph</field>
</record>
thanks
UPDATE
Your module should depend on crm. Then, the code must be this:
<record model="ir.actions.act_window" id="crm.crm_lead_action_pipeline">
<field name="view_mode">kanban,tree,form</field>
</record>

Odoo 12 - Model not found even though it exists

I am porting a localization accountability model from Odoo 8 to Odoo 12, I ported a function that worked flawlessly but then right after that I tried porting some other code that does not interfere with the first on, even though, whenever it is initialized by the init.py Odoo brings up an error telling me that the model cannot be found, even though it is correctly set up.
All I've tried is to ignore the new modules I added right after setting up the one that should be perfectly working right now. Without them (or without any new module at all, because I've tried porting different ones) it works, but this shouldn't be the case at all.
wizard_nro_ctrl.py
class WizNroctrl(models.TransientModel):
_name = 'nroctrl'
_description = "Wizard that changes the invoice control number"
new_nroctrl = fields.Char('Control Number', required=True)
sure = fields.Boolean('Are you sure?')
# Change control number of the invoice
def set_noctrl(self):
if not self.sure:
raise except_orm('Error!', 'Please confirm that you want to do this by checking the option')
current_id = self._context['current_id']
inv_object = self.env['account.invoice'].browse(current_id)
inv_object.nro_ctrl = self.new_nroctrl
return True
wizard_nro_ctrl_view.xml
<?xml version='1.0' encoding='UTF-8'?>
<odoo>
<data>
<record model="ir.ui.view" id="wizard_nro_ctrl_form">
<field name="name">wizard.nro.ctrl.form</field>
<field name="model">nroctrl</field>
<field name="arch" type="xml">
<form string="Changing the Control Number">
<field name="new_nroctrl" placeholder="New control number"/>
<separator string="Are you sure you want to do this?" colspan="4"/>
<field name="sure"/>
<footer>
<button name="set_noctrl" string="Confirm" type="object"/>
<button special="cancel" string="Cancel"/>
</footer>
</form>
</field>
</record>
<record id="action_wiz_nroctrl" model="ir.actions.act_window">
<field name="name">Change control number</field>
<field name="type">ir.actions.act_window</field>
<field name="model">wiz.nroctrl</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
</data>
</odoo>
The init.py has the model initialized and the folder as well.
The model should work just fine, without this error. There's no reason to my knowledge for this issue.
odoo.tools.convert.ParseError: "Error while validating constraint
Model not found: nroctrl
Please make sure that in the model action you have wrote include model name 'nroctrl'. give the below xml code (nroctrl):
<record id="action_wiz_nroctrl" model="ir.actions.act_window">
<field name="name">Change control number</field>
<field name="type">ir.actions.act_window</field>
<field name="model">nroctrl</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
Also please ensure that you have given file name in init.py of wizard folder. also the folder name in ini.py of module.

How to restrict access to partners by create_uid

I need to limit user access only to partners added himself.
That's what I have made:
security.xml of my module:
<odoo>
<data noupdate="1">
<record model="ir.rule" id="partner_access_user_rule">
<field name="name">Parners only for editors</field>
<field name="model_id" ref="base.model_res_partner"/>
<field name="domain_force"> [('create_uid','=',user.id)] </field>
<field name="groups" eval="[(4,ref('base.group_user'))]"/>
</record>
</data>
</odoo>
Update I have changed ref="base.model_res_partner" as Lucas told, and my module have installed, but the rule didn't uppear in settings > security>"record rules" and didn't apply. I've added record rule via web interface and it works. How I to export it to apply to my module? The record rule settings I've added to question.
Export file:
id,"perm_create","perm_unlink","perm_read","perm_write","domain_force","groups/id","name","model_id/id"
__export__.ir_rule_97,"True","True","True","True","[('create_uid','=',user.id)]","base.group_user","Partners only for editors","account.model_res_partner"
Your code is looking for the model res.partner on mail.partner.access, when it should look on base.
Try the following:
<field name="model_id" ref="base.model_res_partner"/>
If your row-level access rule doesn't work, create and test it using Odoo interface, export it to csv and change your original XML. Or create xml directly from CSV (look at attached picture). In my case I've found mistake: model_id should refer to base.model_res_partner

How to give access rights to users with certain conditions?

If there are four records created by four users. The Manager can be able to see all the records of all users. But a user should not be able view another user's record.
user – Can create/view/edit only his/her own record.
This is my need.
Thank You in advance.
you need to create ir.rules. For example in your case it would be something like this:
For administrators:
<record id="manager_rule" model="ir.rule">
<field name="name">My first rule</field>
<field name="model_id" ref="model_your_model"/>
<field name="domain_force">[(1,'=',1)]</field>
<field name="groups" eval="[(4,ref('your_manager_group'))]"/>
</record>
For other users:
<record id="others_rule" model="ir.rule">
<field name="name">My second rule</field>
<field name="model_id" ref="model_your_model"/>
<field name="domain_force">[('user_id','=',user.id)]</field>
<field name="groups" eval="[(4,ref('base.group_user'))]"/>
</record>
For more information about access rules you could visit odoo_security, access_rules.
I hope this help you!
Your problem solved using domain like
Eg.
domain = [('user_id','=',user.id)]
i hope it helps you

Resources