在OpenERP中以当地语言获取产品名称?

时间:2014-04-24 11:57:02

标签: python odoo xmlrpclib

这是一个非常简单但难以回答的问题,因为我认为只有少数几个"人们使用这个。

我在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

它应该在OpenERP数据库中找到名称为' my_product'的所有产品,但事实并非如此。我知道原因:

我不在一个谈论英语的国家,所以我在OpenERP中安装了一种语言,并且搜索' xmlrpc正在寻找名为' my_product'的产品。但只有英文。问题是,显然没有一个字段可以保存翻译的名称......似乎是' name'太!如果我用我的语言指定他们的名字,我就找不到产品。

世界上任何人都有同样的问题吗?

修改

好的,我有一个线索:如果不是在搜索',我会这样做:'

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

这样我可以指定语言并且有效!但我不能用“搜索”来做同样的事情,我会收到错误。谁知道的方式???

新编辑

context = {'lang': 'es_ES'}

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

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

3 个答案:

答案 0 :(得分:5)

如果你在lang中传递context,我几乎可以肯定它应该有效。

例如:context = {'lang': u'pl_PL'}

如果它不起作用,您应该尝试覆盖name_search方法。

答案 1 :(得分:1)

搜索方法是这样的:

JNIEXPORT jobjectArray JNICALL Java_jssc_SerialNativeInterface_getPortProperties
  (JNIEnv *env, jclass cls, jstring portName) {
    const char* portNameChar = (const char*)env->GetStringUTFChars(portName, NULL);
    jclass stringClass = env->FindClass("Ljava/lang/String;");
    jobjectArray ret = env->NewObjectArray(5, stringClass, NULL);

#ifdef __APPLE__

    // this code is based on QtSerialPort
    CFMutableDictionaryRef matching = IOServiceMatching(kIOSerialBSDServiceValue);
    io_iterator_t iter = 0;
    kern_return_t kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matching, &iter);
    if (kr != kIOReturnSuccess) {
        env->ReleaseStringUTFChars(portName, portNameChar);
        return ret;
    }

    io_registry_entry_t service;
    while ((service = IOIteratorNext(iter))) {

        // compare portName against cu and tty devices
        bool found = false;

        CFTypeRef cu = 0;
        cu = IORegistryEntrySearchCFProperty(service, kIOServicePlane, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
        if (cu) {
            char buffer[MAXPATHLEN];
            CFStringGetCString(CFStringRef(cu), buffer, sizeof(buffer), kCFStringEncodingUTF8);
            //fprintf(stdout, "getPortProperties: %s\n", buffer);
            //fflush(stdout);
            if (strcmp(portNameChar, buffer) == 0) {
                found = true;
            }
            CFRelease(cu);
        }

        CFTypeRef tty = 0;
        tty = IORegistryEntrySearchCFProperty(service, kIOServicePlane, CFSTR(kIODialinDeviceKey), kCFAllocatorDefault, 0);
        if (tty) {
            char buffer[MAXPATHLEN];
            CFStringGetCString(CFStringRef(tty), buffer, sizeof(buffer), kCFStringEncodingUTF8);
            //fprintf(stdout, "getPortProperties: %s\n", buffer);
            //fflush(stdout);
            if (strcmp(portNameChar, buffer) == 0) {
                found = true;
            }
            CFRelease(tty);
        }

        if (!found) {
            // not port we're looking for
            //fprintf(stderr, "getPortProperties: %s not found", portNameChar);
            //fflush(stderr);
            IOObjectRelease(service);
            continue;
        }

        io_registry_entry_t entry = service;
        do {
            int val = 0;
            char buffer[255];

            CFTypeRef idProduct = 0;
            idProduct = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBProductID), kCFAllocatorDefault, 0);
            if (idProduct && !env->GetObjectArrayElement(ret, 0)) {
                CFNumberGetValue(CFNumberRef(idProduct), kCFNumberIntType, &val);
                sprintf(buffer, "%04x", val);
                jstring tmp = env->NewStringUTF(buffer);
                env->SetObjectArrayElement(ret, 0, tmp);
                env->DeleteLocalRef(tmp);
                CFRelease(idProduct);
            }

            CFTypeRef idVendor = 0;
            idVendor = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBVendorID), kCFAllocatorDefault, 0);
            if (idVendor && !env->GetObjectArrayElement(ret, 1)) {
                CFNumberGetValue(CFNumberRef(idVendor), kCFNumberIntType, &val);
                sprintf(buffer, "%04x", val);
                jstring tmp = env->NewStringUTF(buffer);
                env->SetObjectArrayElement(ret, 1, tmp);
                env->DeleteLocalRef(tmp);
                CFRelease(idVendor);
            }

            CFTypeRef manufacturer = 0;
            manufacturer = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBVendorString), kCFAllocatorDefault, 0);
            if (manufacturer && !env->GetObjectArrayElement(ret, 2)) {
                CFStringGetCString(CFStringRef(manufacturer), buffer, sizeof(buffer), kCFStringEncodingUTF8);
                jstring tmp = env->NewStringUTF(buffer);
                env->SetObjectArrayElement(ret, 2, tmp);
                env->DeleteLocalRef(tmp);
                CFRelease(manufacturer);
            }

            CFTypeRef product = 0;
            product = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBProductString), kCFAllocatorDefault, 0);
            if (product && !env->GetObjectArrayElement(ret, 3)) {
                CFStringGetCString(CFStringRef(product), buffer, sizeof(buffer), kCFStringEncodingUTF8);
                jstring tmp = env->NewStringUTF(buffer);
                env->SetObjectArrayElement(ret, 3, tmp);
                env->DeleteLocalRef(tmp);
                CFRelease(product);
            }

            CFTypeRef serial = 0;
            serial = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, CFSTR(kUSBSerialNumberString), kCFAllocatorDefault, 0);
            if (serial && !env->GetObjectArrayElement(ret, 4)) {
                CFStringGetCString(CFStringRef(serial), buffer, sizeof(buffer), kCFStringEncodingUTF8);
                jstring tmp = env->NewStringUTF(buffer);
                env->SetObjectArrayElement(ret, 4, tmp);
                env->DeleteLocalRef(tmp);
                CFRelease(serial);
            }

            kr = IORegistryEntryGetParentEntry(entry, kIOServicePlane, &entry);
        } while (kr == kIOReturnSuccess);

        IOObjectRelease(entry);

        IOObjectRelease(service);
    }

    IOObjectRelease(iter);

#endif  // __APPLE__

    env->ReleaseStringUTFChars(portName, portNameChar);
    return ret;
}

如果要发送上下文,还必须在args和context之间发送参数:

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

答案 2 :(得分:0)

在openerp / osv / expression.py中,只有在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'
           ]