OpenERP, error while creating a view filter - odoo

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

Related

What does the model option in odoo xml mean?

I found this code in the odoo and the options="{'model': 'crm.lead'} confused me?
What does this mean? And when should I use it?
<field name="assignment_domain" string="Domain" widget="domain"
options="{'model': 'crm.lead'}"
attrs="{'invisible': ['|', ('assignment_max', '=', 0), ('assignment_optout', '=', True)]}"/>
The Domain field allows the user to construct a technical-prefix domain and see the selected records in real-time.
The model option is used to set the model the field must work with, the model and domain are used to fetch records.

Filter available partners for a new invoice in Odoo

I would like to filter the available partner selection for 'Customer' when creating an invoice in Odoo. Specifically, I'd like to limit the partner contact records to those of type 'invoice address', i.e. a domain on res_partner of [('type','=','invoice')].
Can anyone point me in the right direction please?
I can see that res_partner._name_search will accept Args, and by the look of the code in models.py the Args could be the required domain, becoming a where clause. However, I can't see how to specify this in the xml (or anywhere else). The standard xml for the Customer drop-down is
<field name="partner_id" widget="res_partner_many2one" context="{'res_partner_search_mode': 'customer', 'show_address': 1, 'show_vat': True}" options='{"always_reload": True}'/>
res_partner_search_mode looked kind of promising, but seems to only be used in setting up customer_rank, so no help here. Maybe just override _name_search to filter the records?? I'd be glad of any help. Thanks!
Try something like that
<field name="partner_id" widget="res_partner_many2one" domain="[('type','=','invoice')]" ...```
Solved. Paxmees' suggestion didn't work (the domain in the XML didn't seem to flow through to anywhere in the python code). But the args in _name_search turn out to be a domain. So I made an override for _name_search:
def _name_search(self, name, args, operator, limit, name_get_uid=None):
restrict = self.env.context.get('restrict_types')
if restrict == 'invoice':
args.append(('type', '=', restrict))
return super()._name_search(name, args, operator=operator, limit=limit, name_get_uid=name_get_uid)
and then added the controlling context in the xml for views where I want this to happen:
<xpath expr="//field[#name='partner_id']" position="attributes">
<attribute name="context">{'restrict_types': 'invoice'}</attribute>
</xpath>
This works OK. Thanks for reading my question!

Odoo logged user data dont update for a login user

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.

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')]}"/>