Odoo logged user data dont update for a login user - odoo

Please I need help with some custom rules configurations in Odoo 11.
Currently I'm doing a rule that allow an user only access to a certains product categories, for that I have a Many2many field which specify those categories:
product_category_ids = fields.Many2many('product.category')
Here is the rule that only allows access to that categories:
<record model="ir.rule" id="product_template_category_users">
<field name="name">product.template.category.users</field>
<field name="model_id" ref="product.model_product_template"/>
<field name="perm_read" eval="True"/>
<field name="perm_write" eval="True"/>
<field name="perm_unlink" eval="True"/>
<field name="perm_create" eval="True"/>
<field name="domain_force">[('categ_id', 'child_of', user.product_category_ids.ids)]</field>
</record>
The rule works fine, but I have this problem:
Login with user "A" who has that rule
Login in another sesion with user "B" and update user "A"
adding a new category to categories field
Return with user "A" and the rule doesn't show the new category added, also reload page doesn't work.
The changes only apply in "A" user when I change the current company or reload the Odoo Service.
I think that it has to be something with the user storing data when the user login, maybe is some way to update that data and allow the rule to read it from "user". I need that the changes doing to users apply in real time to that connected users without have to change the current company or reload the Odoo service.
Thanks for helping.

This is strange that it should work, but only after changing current company or restarting the Odoo server.
Can you try your modifications on a blank database and/or a new database with demo data loaded? If possible, it would be good to test this on an entirely different server to see if the problem might lay there.
Perhaps you can also try modifying your force_domain like this:
['|', ('categ_id', 'in', user.product_category_ids.ids), ('categ_id', 'child_of', user.product_category_ids.ids)]

If anyone has the same problem, I solve it using this funcion every time i made a change in the product categories field of user
self.env['ir.rule'].clear_cache()
That code clear the cache of rules so the rules apply the new domain.

Add self.env['ir.rule'].clear_cache() into create() and write() method of your model.

Related

How to domain/filter PO based on user allowed branch in odoo

I want to display PO based on allowed branch in odoo. So if you log in as user Ana with allowed branch = Singapore, then the PO that appear are only PO with the Singapore branch. Maybe someone knows and can give advice?
Thanks
Best Regards
you can use the domain field on a model to filter records based on certain criteria. This can be useful for restricting which records are visible to a user based on their allowed.
Another alterative to make a custom domain (maybe dificult because you need to access the current user)
I think you can define a new security rule, to discard PO with no match in the branch. but not sure if you can do user.branch_id or some other field distinct od id.
<record id="branch_po_rule" model="ir.rule">
<field name="name">PO own branch</field>
<field name="model_id" ref="model_purchase_order"/>
<field eval="True" name="global"/>
<field name="domain_force">('branch_id','=',user.branch_id))]</field>
</record>
But security rules are meant to do this kind of discriminations

For Admin, Manager i want show every record , for normal user I want show records created by that particular user only

How to filter records for tree view based on logged in user. For Admin, Manager i want show every record , for normal user I want show records created by that particular user only.
Below code sample I tried
For manager uid=12
For admin uid=1
<field name="domain">[('|',('create_uid','=',uid),('|',(uid,'=','1'),(uid,'=','12')))]</field>
Above code sample is throwing error
"ValueError: Invalid leaf ['|', ['create_uid', '=', 1], ['|', [1, '=',
'1'], [1, '=', '12']]]"
Row-level access rules are defined in the ir.rule model and can be created by adding a corresponding xml file to the module. The file is usually stored under security/ folder in your module directory.
For example I've taken user.purchase.records as the model
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="user_purchase_record_rule" model="ir.rule">
<field name="name">Records created by current user only</field>
<field name="model_id" ref="model_user_purchase_records"/>
<field name="domain_force">[('create_uid','=',user.id)]</field>
<field name="groups" eval="[(4,ref('base.group_user'))]"/>
</record>
</data>
</odoo>
Finally add this file path in your manifest.py file.
A domain it's composed by 3 elements, (field_in_your_model, operator, value), I don't know if you are working with a known model or it's your definition, but you got that error because uid it's a reserved word for odoo, not a field in a model.
And the best approach for your requirement its make rules, for your groups of users, something like sale groups:
Rule for Sale / User: Own Documents Only:
['|',('user_id','=',user.id),('user_id','=',False)]
I hop this answer can be helpful you.

Invisible field on condition in odoo

I have one field which i want to hide when specific journal selected.
<field name="any_field" attrs="{'invisible':[('journal_id','=',10])]}"/>
And For that above code is working fine.
I sure it is not a way to check condition.So, i tried this way.
<field name="any_field" attrs="{'invisible':[('journal_id','=',ref('my_module.account_journal_10'))]}"/>
It's working then i tried by using the static field on the journal eg. code.
<field name="any_field" attrs="{'invisible':[('journal_id.code','=','CARD')]}"/>
But still not working and getting error from view.
I am thinking if i can return the attrs from .py like i do for domain.
eg.
return {'domain':
{
'any_field':[('journal_id','=',self.env.ref('my_module.account_journal_10').id)]
}
}
Can anybody help me in this?
Thank you.
As far as i know these attrs domains/filters are client-side so you can't use something like journal_id.code or partner_id.customer, because the client doesn't know about such data.
A possible workaround is to define a related field on the model you're trying to do this. Let's assume the model is my.model and already has this Many2one field journal_id:
journal_code = fields.Char(string="Journal Code", related="journal_id.code")
Now you need to extend the view of my.model:
<field name="journal_code" invisible="1" />
<field name="any_field" attrs="{'invisible':[('journal_code','=','CARD')]}"/>

How to link Leads (emails) to Marketing emails sent in Odoo v8?

I have Leads and all my leads have emails set. Now I have sent emails to leads with "Mass Mailings" from "Marketing". But now when I click on my lead I do not see any link between the Lead (email) to emails I have sent with "Mass Mailings".
Is there a way to make a link between Lead (email) to email I have sent?
The information about sent emails is available trough the model mail.mail.statistics. In this model you have everything you need. The following fields may be of interest for your task:
model - as emails may be related to any Odoo model, this field tells you to which model was related the email. You are interested in records with crm.lead in this field
res_id - the id of the corresponding model instance. In your case this fields links you to the id of your lead.
mail_mail_id_int - the id of the objects of type email.email - the emails themselves
etc.
You can use this to create a list of emails related to a lead and show them in the crm lead form.
To do that, create a new Odoo module, extend the crm.lead object adding a One2many relation to the mail.mail.statistics model and extend the crm.lead view to show this new field.
For instance, in a file called models/lead.py in this new module, put the following:
from openerp import models, fields
class crm_lead(models.Model):
_inherit = 'crm.lead'
emails = fields.One2many(comodel_name='mail.mail.statistics',
inverse_name='res_id',
domain=[('model', '=', 'crm.lead')])
crm_lead()
Respectively, to extend the view, create a file views/lead_view.xml like this:
<?xml version="1.0"?>
<openerp>
<data>
<record model="ir.ui.view" id="mail_crm_stats.crm_lead_form">
<field name="name">mail_crm_stats.crm_lead.form</field>
<field name="model">crm.lead</field>
<field name="type">form</field>
<field name="inherit_id" ref="crm.crm_case_form_view_leads"/>
<field name="arch" type="xml">
<xpath expr="//notebook/page[#string='Extra Info']"
position="after">
<page string="Emails sent">
<group name="emails">
<div>
<field name="emails" nolabel="1"
class="oe_inline"/>
</div>
</group>
</page>
</xpath>
</field>
</record>
</data>
</openerp>
Now you should see additional tab 'EMails sent' in your lead form. Of course, this is just and example and the module may be improved to show better information about the sent emails. As the case is interesting I may commit soon a new version in the github repository I created for the purpose..
You can download the entire module and test it from my github repository like this:
git clone https://github.com/andreiboyanov/odoo-mails_crm_stats mails_crm_stats
In V8 there is a fields between leads and Marketing Campaign See in any Lead > Extra Info (tab) > Marketing, You can manually add your campaign to that, but if you need to automatically sync then you need to create a new module for that, but just for now you can manually put entry.You can view various modules on here.

OpenERP, error while creating a view filter

Im having problems with creating a filter on stock.picking object. Just recently i build a simple "privilege relay" - in each stock location you can define "Assigned User Group", thanks to that users that are in particular group can or cannot confirm moves for or out of the location.
Stock.picking:location_id -> assigned_user_group -> users
Now I would like to create a filter (later to be set default) on stock picking tree view that will show only the moves which locations (source location and destination location; i use them in stock.picking object) can be managed by a viewing user.
By far I wrote a filter that looks like that:
<record id="view_picking_internal_search_pl" model="ir.ui.view">
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.view_picking_internal_search"/>
<field name="arch" type="xml">
<filter icon="terp-dialog-close" name="done" string="Done" domain="[('state','=','done')]" help="Pickings already processed" position="after">
<filter icon="terp-check" name="locgroup" string="Location Group" domain="[('user_id','in',[user.id for user in location_id.user_group.users])]" context="{'group_by':'date'}"/>
</filter>
</field>
</record>
I also added field location_id to the tree view.
But Im still getting an error (after choosing the filter) that even google doesnt know anything about:
TypeError: results.group_by is undefined
My questions are:
By looking on domain in filter field - what am i doing wrong?
Is something like that even possible?
I will gladly welcome any help.
Firstly, i think your domain is not correct, it could have been :
[('user_group.users.id', '=', uid)]
(because the first element of the tuple is a field on the model; and uid is a special value supplied in search views)
Next, This error :
TypeError: results.group_by is undefined
Seems to be a Javascript Error (coming from openerp-web interface), it often throws error like that when it receives unexpected values (when we make a mistake defining a view for example).
can you tell us if using the domain above solved your problem ?
NB: does your field user_group is a required field ? If not, i think the domain above won't display picking where user_group is not set, if you want to display picking where user_group is not set too, you can set a domain like that:
['|',('user_group.users.id', '=', uid), ('user_group','=',False)]
Regards