From 49fbcdb70a590d566a635b83afd6ecc6d7763737 Mon Sep 17 00:00:00 2001 From: "P. Christeas" Date: Wed, 8 Jun 2011 12:32:54 +0300 Subject: [PATCH] ir.translation: select restriction must be an SQL constraint select fields validate themselves in ORM, through _check_selection_field_value(), which /retrieves/ the .selection list and checks if value to be written is among possible ones. This, however, is very expensive for models that have variable selection lists (eg. ones that require an SQL query). In fact, for ir.translation, the query would be repeat for every single write() to ir.translation. The 'lang' field is actually a reference for res.lang.code (not the .id, sadly), so let SQL implement the constraint for us. bzr revid: xrg@linux.gr-20110608093254-ua66p5co6dc203zs --- openerp/addons/base/base.sql | 6 ++++++ openerp/addons/base/ir/ir_translation.py | 12 ++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/openerp/addons/base/base.sql b/openerp/addons/base/base.sql index 17378061b29..80384a9e327 100644 --- a/openerp/addons/base/base.sql +++ b/openerp/addons/base/base.sql @@ -314,6 +314,12 @@ CREATE TABLE res_company ( primary key(id) ); +CREATE TABLE res_lang ( + id serial PRIMARY KEY, + name VARCHAR(64) NOT NULL UNIQUE, + code VARCHAR(16) NOT NULL UNIQUE +); + CREATE TABLE ir_model_data ( id serial NOT NULL, create_uid integer, diff --git a/openerp/addons/base/ir/ir_translation.py b/openerp/addons/base/ir/ir_translation.py index 7580b4da185..11a5c248d23 100644 --- a/openerp/addons/base/ir/ir_translation.py +++ b/openerp/addons/base/ir/ir_translation.py @@ -62,8 +62,12 @@ class ir_translation(osv.osv): 'src': fields.text('Source'), 'value': fields.text('Translation Value'), } + + _sql_constraints = [ ('lang_fkey_res_lang', 'FOREIGN KEY(lang) REFERENCES res_lang(code)', + 'Language code of translation item must be among known languages' ), ] - def _auto_init(self, cr, context={}): + + def _auto_init(self, cr, context=None): super(ir_translation, self)._auto_init(cr, context) # FIXME: there is a size limit on btree indexed values so we can't index src column with normal btree. @@ -88,7 +92,11 @@ class ir_translation(osv.osv): cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (name, lang, type)') cr.commit() - + def _check_selection_field_value(self, cr, uid, field, value, context=None): + if field == 'lang': + return + return super(ir_translation, self)._check_selection_field_value(cr, uid, field, value, context=context) + @tools.cache(skiparg=3, multi='ids') def _get_ids(self, cr, uid, name, tt, lang, ids): translations = dict.fromkeys(ids, False)