Remove sql injection problem
[odoo/odoo.git] / bin / addons / base / ir / ir_translation.py
index a74aa62..8e2a6d9 100644 (file)
@@ -1,40 +1,30 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
 ##############################################################################
+#    
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
 #
-# Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
 #
-# $Id$
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
 #
-# WARNING: This program as such is intended to be used by professional
-# programmers who take the whole responsability of assessing all potential
-# consequences resulting from its eventual inadequacies and bugs
-# End users who are looking for a ready-to-use solution with commercial
-# garantees and support are strongly adviced to contract a Free Software
-# Service Company
-#
-# This program is Free Software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
 #
 ##############################################################################
 
 from osv import fields, osv
-from osv.osv  import Cacheable
 import tools
 
 TRANSLATION_TYPE = [
     ('field', 'Field'),
-    ('model', 'Model'),
+    ('model', 'Object'),
     ('rml', 'RML'),
     ('selection', 'Selection'),
     ('view', 'View'),
@@ -47,7 +37,7 @@ TRANSLATION_TYPE = [
     ('constraint', 'Constraint'),
 ]
 
-class ir_translation(osv.osv, Cacheable):
+class ir_translation(osv.osv):
     _name = "ir.translation"
     _log_access = False
 
@@ -64,49 +54,61 @@ class ir_translation(osv.osv, Cacheable):
 
     _columns = {
         'name': fields.char('Field Name', size=128, required=True),
-        'res_id': fields.integer('Resource ID'),
+        'res_id': fields.integer('Resource ID', select=True),
         'lang': fields.selection(_get_language, string='Language', size=5),
-        'type': fields.selection(TRANSLATION_TYPE, string='Type', size=16),
+        'type': fields.selection(TRANSLATION_TYPE, string='Type', size=16, select=True),
         'src': fields.text('Source'),
         'value': fields.text('Translation Value'),
     }
-    _sql = """
-        create index ir_translation_ltn on ir_translation (lang,type,name);
-        create index ir_translation_res_id on ir_translation (res_id);
-    """
 
+    def _auto_init(self, cr, context={}):
+        super(ir_translation, self)._auto_init(cr, context)
+        cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltns',))
+        if not cr.fetchone():
+            cr.execute('CREATE INDEX ir_translation_ltns ON ir_translation (lang, type, name, src)')
+            cr.commit()
+
+        cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltn',))
+        if not cr.fetchone():
+            cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (lang, type, name)')
+            cr.commit()
+
+        cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_lts',))
+        if not cr.fetchone():
+            cr.execute('CREATE INDEX ir_translation_lts ON ir_translation (lang, type, src)')
+            cr.commit()
+
+    @tools.cache(skiparg=3, multi='ids')
     def _get_ids(self, cr, uid, name, tt, lang, ids):
-        translations, to_fetch = {}, []
-        for id in ids:
-            trans = self.get((lang, name, id))
-            if trans is not None:
-                translations[id] = trans
-            else:
-                to_fetch.append(id)
-        if to_fetch:
+        translations = dict.fromkeys(ids, False)
+        if ids:
             cr.execute('select res_id,value ' \
                     'from ir_translation ' \
                     'where lang=%s ' \
                         'and type=%s ' \
                         'and name=%s ' \
-                        'and res_id in ('+','.join(map(str, to_fetch))+')',
+                        'and res_id in ('+','.join(map(str, ids))+')',
                     (lang,tt,name))
             for res_id, value in cr.fetchall():
-                self.add((lang, tt, name, res_id), value)
                 translations[res_id] = value
-        for res_id in ids:
-            if res_id not in translations:
-                self.add((lang, tt, name, res_id), False)
-                translations[res_id] = False
         return translations
 
-    def _set_ids(self, cr, uid, name, tt, lang, ids, value):
+    def _set_ids(self, cr, uid, name, tt, lang, ids, value, src=None):
+        # clear the caches
+        tr = self._get_ids(cr, uid, name, tt, lang, ids)
+        for res_id in tr:
+            if tr[res_id]:
+                self._get_source.clear_cache(cr.dbname, uid, name, tt, lang, tr[res_id])
+        self._get_source.clear_cache(cr.dbname, uid, name, tt, lang)
+        self._get_ids.clear_cache(cr.dbname, uid, name, tt, lang, ids)
+
         cr.execute('delete from ir_translation ' \
                 'where lang=%s ' \
                     'and type=%s ' \
                     'and name=%s ' \
-                    'and res_id in ('+','.join(map(str,ids))+')',
+                    'and res_id in ('+','.join(map(str, ids))+')',
                 (lang,tt,name))
+        cr.commit()
         for id in ids:
             self.create(cr, uid, {
                 'lang':lang,
@@ -114,14 +116,12 @@ class ir_translation(osv.osv, Cacheable):
                 'name':name,
                 'res_id':id,
                 'value':value,
+                'src':src,
                 })
         return len(ids)
 
+    @tools.cache(skiparg=3)
     def _get_source(self, cr, uid, name, tt, lang, source=None):
-        trans = self.get((lang, tt, name, source))
-        if trans is not None:
-            return trans
-
         if source:
             #if isinstance(source, unicode):
             #   source = source.encode('utf8')
@@ -140,25 +140,35 @@ class ir_translation(osv.osv, Cacheable):
                         'and name=%s',
                     (lang, tt, str(name)))
         res = cr.fetchone()
-        
         trad = res and res[0] or ''
-        self.add((lang, tt, name, source), trad)
         return trad
-
-    def unlink(self, cursor, user, ids, context=None):
-        self.clear()
-        return super(ir_translation, self).unlink(cusor, user, ids,
-                context=context)
-
+        
     def create(self, cursor, user, vals, context=None):
-        self.clear()
-        return super(ir_translation, self).create(cursor, user, vals,
-                context=context)
+        if not context:
+            context = {}
+        ids = super(ir_translation, self).create(cursor, user, vals, context=context)
+        for trans_obj in self.read(cursor, user, [ids], ['name','type','res_id'], context=context):
+            self._get_source.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], lang=context.get('lang','en_US'))
+            self._get_ids.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], context.get('lang','en_US'), [trans_obj['res_id']])
+        return ids    
 
     def write(self, cursor, user, ids, vals, context=None):
-        self.clear()
-        return super(ir_translation, self).write(cursor, user, ids, vals,
-                context=context)
+        if not context:
+            context = {}
+        result = super(ir_translation, self).write(cursor, user, ids, vals, context=context)
+        for trans_obj in self.read(cursor, user, ids, ['name','type','res_id'], context=context):
+            self._get_source.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], lang=context.get('lang','en_US'))
+            self._get_ids.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], context.get('lang','en_US'), [trans_obj['res_id']])
+        return result
+
+    def unlink(self, cursor, user, ids, context=None):
+        if not context:
+            context = {}
+        for trans_obj in self.read(cursor, user, ids, ['name','type','res_id'], context=context):
+            self._get_source.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], lang=context.get('lang','en_US'))
+            self._get_ids.clear_cache(cursor.dbname, user, trans_obj['name'], trans_obj['type'], context.get('lang','en_US'), [trans_obj['res_id']])
+        result = super(ir_translation, self).unlink(cursor, user, ids, context=context)
+        return result    
 
 ir_translation()