Open ERP Forum
   IndexIndex   SearchSearch     RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   RSSRSS   Mailing ListMailing List   Log inLog in 

Partner filtering
 
Post new topic   Reply to topic    Open Object Forum Index -> General discussion
View previous topic :: View next topic  
Author Message
jacorbacho



Joined: 08 Jan 2010
Posts: 23

PostPosted: Mon Jan 18, 2010 11:20 am    Post subject: Partner filtering Reply with quote

Hi all,
I am trying to do partner filtering based on a new attribute associated to partner's categories. This attribute must match the user's groups. Since I cannot do this using Security Rules, I am trying to do it extending the partner object and modifying the method to display them.
My question is: which method should I overwrite? Do I have to do any other thing after overwriting the method (I already tried name_search with no effect)?

Thanks,
JAC
Back to top
View user's profile Send private message
kboden



Joined: 14 Aug 2009
Posts: 8
Location: Antwerp, BE

PostPosted: Tue Jan 19, 2010 11:11 am    Post subject: Reply with quote

Hi,

maybe you should take a look at my multi_company_category module in the community branch. It will add a company attribute to the partner category which is used in the domain of the Company Partners view. The domain uses a placeholder that will be replaced by the actual query criteria while opening the view. More info can be found here: http://www.cogetix.net/node/7

Regards,
________________
Kristof Boden
Cogetix bvba - http://www.cogetix.com
Back to top
View user's profile Send private message Visit poster's website
jacorbacho



Joined: 08 Jan 2010
Posts: 23

PostPosted: Fri Jan 29, 2010 12:22 pm    Post subject: Reply with quote

Hi Kristof,
thanks for your reply. I will take a look to your code and see if I can use it.
In the days from the first post to this reply some changes have come:
- Partners are visible only to users that are associated to the same department (we created a relationship Partner-Department and use "hr" module to associate users to depts)

I created a function field in partner object called "visible" that computes if a partner belongs to any of the user's departments.
I added a global record rule where this field is checked to be True.
It works well to display any partner list. However, I am getting an error when trying to create a invoice (and I bet in other places too??) saying "You try to bypass an access rule (Document type: Partner)". This exception is not thrown if the record rule is disabled and the creation of the invoice is done.

The invoice includes partners that are visible by the user doing the operation.

Maybe I am not using the best approach to this issue: any suggestion is welcome.

Cheers,

Jose A. Corbacho
Back to top
View user's profile Send private message
jacorbacho



Joined: 08 Jan 2010
Posts: 23

PostPosted: Mon Feb 01, 2010 9:44 am    Post subject: Reply with quote

Hi all,
Finally, I've got something up and running.

Objective: Filter partners depending on user's departments

Pre-requisites: Module hr installed

How to:
1) Create a relationship many2many Partner-Department
2) Add field "department_ids" to object user (same as hr.department.members)
3) Create a record rule on Partner object where:
- Partner.departments in User.departments

This solution filters partners in any part of the framework by user's departments.

Thanks Kristoff for your help though, eventually, we decided it was better to restrict access on the object itself rather than in the view. Anyway, I learned a good deal with the code you suggested me.


As a side effect, now in "Partner Addresses" we have an "Access Denied" string for all partners no matter if they are accesible by the user or not. But that's for season 2 of "Partner Visibility".

If anyone is interested in the code, I could upload it here later.

Cheers,

Jose A. Corbacho
Back to top
View user's profile Send private message
kboden



Joined: 14 Aug 2009
Posts: 8
Location: Antwerp, BE

PostPosted: Mon Feb 01, 2010 9:56 am    Post subject: Reply with quote

Hi Jose,

I'm glad you found a solution.

I guess my requirements were easier. Users still want to able to access all partner data (across departments and companies), they only needed a filtered view of their own partners.

If you can find the time to upload your code, I would still be interested to take a look.

Regards,
________________
Kristof Boden
Cogetix bvba - http://www.cogetix.com
Back to top
View user's profile Send private message Visit poster's website
jacorbacho



Joined: 08 Jan 2010
Posts: 23

PostPosted: Tue Feb 02, 2010 6:55 am    Post subject: Reply with quote

Hi all,
We still have a problem with this issue.
We have extended the partner and user objects as follows:
Code:
class res_partner(osv.osv):
   """Extension of class res.partner to link a Partner object with one or more Department objects.
      Adds a new field departments_id
   """

   _inherit = 'res.partner'

   # Columns added to res_partner table
   _columns = {
      'departments_id': fields.many2many('hr.department', 'hr_department_partner_rel', 'partner_id', 'department_id', 'Departments'),
   }

res_partner()


class res_users(osv.osv):
   """ Add departments to user object. This way we can compare the departments a user belongs to with a partner's ones"""
   _inherit = 'res.users'
   _description = 'res.users'
   
   _columns = {
      'department_ids': fields.many2many('hr.department', 'hr_department_user_rel', 'user_id', 'department_id', 'Departments'),
   }
res_users()


- A global record rule has been created on object Partner:
Code:
 Partner/Departments in User/Departments


- Partners are displayed properly according to this configuration.

However, when a user tries to create an invoice, an exception "You try to bypass an access rule (Document type: Partner)" is thrown.

The exception is thrown in invoice.py, method action_move_create at line
Code:
context.update({'lang': inv.partner_id.lang})


Commenting this line makes the creation of invoices work.

inv.partner_id is the function
Code:
browse_record(res.partner, 4)


Anyone has found a similar problem?
Is it due to the many2many relation in both directions (Partner <-> Department; User <-> Department)?[/code]
_________________
Jose A. Corbacho
Trobz (www.trobz.com)
Back to top
View user's profile Send private message
jacorbacho



Joined: 08 Jan 2010
Posts: 23

PostPosted: Tue Feb 02, 2010 12:28 pm    Post subject: Reply with quote

Finally we found the problem!!

Being the main company a partner too, it has to be associated to all departments!
_________________
Jose A. Corbacho
Trobz (www.trobz.com)
Back to top
View user's profile Send private message
jacorbacho



Joined: 08 Jan 2010
Posts: 23

PostPosted: Fri Feb 05, 2010 9:49 am    Post subject: Reply with quote

It looked good but we still have a problem when we try to create an object where a rule exists for it.
In our case, we cannot create Partners when the rule "Partner/Departments in User/Departments" is on.

There is a previous post about this. I will try to get some answers from the people who dealt with it.

I opened a new topic about this.
_________________
Jose A. Corbacho
Trobz (www.trobz.com)
Back to top
View user's profile Send private message
jacorbacho



Joined: 08 Jan 2010
Posts: 23

PostPosted: Mon Feb 08, 2010 1:01 pm    Post subject: Reply with quote

Some updates about this issue:
First of all I'd like to say that I don't know if I'm in the right path. I am trying to do the filtering by record rules but maybe it can be done easier.
I have tried doing it via domain in the views but I can only restrict it this way for type=form. For type=view I haven't been able.

I have also tried to simplify the case to see if the problem is a generic problem or something in the code I'm writing.
These are my conclusions:
- Filtering works in all cases.
- The problem is to create an object when this record rule is activated.
I tried to relate a Partner with a set of users but the same problem is there.
Finally, I found out the problem is within many2many relations.

We're really stuck on this. Any help would be appreciated.

Thanks.
_________________
Jose A. Corbacho
Trobz (www.trobz.com)
Back to top
View user's profile Send private message
jacorbacho



Joined: 08 Jan 2010
Posts: 23

PostPosted: Tue Feb 09, 2010 11:15 am    Post subject: Reply with quote

SOLVED:

The solution I've found is not the prettiest but it works (and I'm glad and I like it).
As the problem with the many2many fields in record rules seems to persists, I have removed them.
Instead, in every object I want to restrict, I have overriden the search method forcing it to take into account the restriction on departments.

Code:

   def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
      if context is None:
         context = {}
      res = self._get_partner_ids(cr, uid, args, context)
      args.append(('partner_id','in', res))
      return super(res_partner, self).search(cr, uid, args, offset, limit, order, context=context, count=count)


   def _get_partner_ids(self, cr, uid, args, context=None):
      """Get a list of partner visible by current user"""
      cr.execute('select distinct partner_id from hr_department_partner_rel where department_id in (select department_id from hr_department_user_rel where user_id = %s)', (uid,))
      resultSet = cr.fetchall()
      res = []
      for result in resultSet:
         res.append(result[0])
      return res


Sorry I'm not a Python guy (yet) and the code can be improved but this works for all the views where partners are listed.
_________________
Jose A. Corbacho
Trobz (www.trobz.com)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Open Object Forum Index -> General discussion All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum