3

This is a very simple but a difficult question to answer, because I think only a "few" people work with this.

I have this simple script in Python:

import xmlrpclib

username = 'my_openerp_user'
pwd = 'my_password'
dbname = 'my_openerp_database'

sock = xmlrpclib.ServerProxy('http://localhost:8063/xmlrpc/common')
uid = sock.login(dbname, username, pwd)
sock = xmlrpclib.ServerProxy('http://localhost:8063/xmlrpc/object')

args = [('name', 'ilike', 'my_product')]
ids = sock.execute(dbname, uid, pwd, 'product.product', 'search', args)

print ids

It should find all the products in the OpenERP database with the name 'my_product', but it does not. And I know why:

I am not in a country in which English is talked, so I have one language installed in OpenERP, and the 'search' of xmlrpc is looking for products with name 'my_product' but only in english. The problem is that apparently there is not a field to save the translated name... It seems to be 'name' too! So I can't find the products if I specify their names in my language.

Anyone in the world had the same problem?

EDIT

Ok, I have a clue: if instead of doing 'search', I do 'read':

product_names = sock.execute(dbname, uid, pwd, 'product.product', 'read', ids, ['name'], {'lang': 'es_ES'})

This way I can specify the language and it works! But I can't do the same with 'search', I get errors. Anyone knows the way???

NEW EDIT

context = {'lang': 'es_ES'}

args = [('name', 'ilike', 'my_product')]  # consulta

ids = sock.execute(dbname, uid, pwd, 'product.product', 'search', args, context)
forvas
  • 9,801
  • 7
  • 62
  • 158

3 Answers3

5

I'm almost sure it should work if you pass lang in context.

For example: context = {'lang': u'pl_PL'}.

If it doesn't work you should try to overwrite name_search method.

Lukasz Puchala
  • 466
  • 1
  • 6
  • 11
  • It doesn't? It looks it does. Look on expression.parse method (in else block at the end of method). – Lukasz Puchala Apr 24 '14 at 12:45
  • Sorry, I am a bit lost, where do I have to look for that expression.parse? – forvas Apr 24 '14 at 14:01
  • `openerp/osv/expression.py` class `expression` method `parse`. When you call search it goes `BaseModel._search()->BaseModel._where_calc()->expression.init()->expression.parse()` and at the end of this method it gets translations from ir_translation table. I understand you have tried to pass `lang` in `context` and it doesn't work? – Lukasz Puchala Apr 24 '14 at 14:08
  • When I use exactly the same variable 'context' you wrote above (with my language), it works, but only in 'read' and 'create' methods. I get this error if I pass 'context' as a keyword argument in 'search', context={'lang: es_ES'} TypeError: __call__() got an unexpected keyword argument 'context' – forvas Apr 24 '14 at 14:26
  • Can you show me how you call search? It looks weird. Can you show whole stacktrace? – Lukasz Puchala Apr 24 '14 at 14:35
  • I have just gotten what you are telling me!!! I changed one of the last lines of expression.parse(): I overwrote context.get('lang', False) or 'en_US' with context.get('lang', False) or 'es_ES' and it seems to work... but I am going to update what you ask for. – forvas Apr 24 '14 at 14:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51358/discussion-between-lukasz-puchala-and-user3198727) – Lukasz Puchala Apr 24 '14 at 14:43
1

search method is like this:

search(cr, uid, args, offset=0, limit=None, order=None, context=None, count=False)

If you want to send the context you also have to send the parameters between args and context:

ids = sock.execute(dbname, uid, pwd, 'product.product', 'search', args, 0, 0, False, context)
0

In openerp/osv/expression.py the query has been reformulated to search the translation in first place an the original name in English only if where not exist a translation in ir_translation. OpenERP Server 6.0.4

query1 = '( SELECT res_id'          \
         '    FROM ir_translation'  \
         '   WHERE name = %s'       \
         '     AND lang = %s'       \
         '     AND type = %s'
instr = ' %s'
#Covering in,not in operators with operands (%s,%s) ,etc. 
if operator in ['in','not in']:
    instr = ','.join(['%s'] * len(right))
    query1 += '     AND value ' + operator +  ' ' +" (" + instr + ")" + ')'
else:
    query1 += '     AND value ' + operator + instr + ')'
query1 +=' UNION ('                \ 
         '  SELECT id'              \
         '    FROM "' + working_table._table + '"' + ' as wt '   \
         '   WHERE "'+ left + '" ' + operator + instr +  \
         '   AND NOT EXISTS (   ' \   
         '       SELECT res_id FROM ir_translation ' \
         '       WHERE wt.id = res_id ' \
         '       AND name = %s'       \
         '       AND lang = %s'       \
         '       AND type = %s)' +  ")"


query2 = [working_table._name + ',' + left,
          context.get('lang', False) or 'en_US',
          'model',
          right,
          right,
          working_table._name + ',' + left,
          context.get('lang', False) or 'en_US',
          'model'
           ]
nithal
  • 1
  • 2