[FIX] Use a commit to close the delete statement
[odoo/odoo.git] / bin / addons / base / ir / ir_translation.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import fields, osv
24 import tools
25
26 TRANSLATION_TYPE = [
27     ('field', 'Field'),
28     ('model', 'Object'),
29     ('rml', 'RML'),
30     ('selection', 'Selection'),
31     ('view', 'View'),
32     ('wizard_button', 'Wizard Button'),
33     ('wizard_field', 'Wizard Field'),
34     ('wizard_view', 'Wizard View'),
35     ('xsl', 'XSL'),
36     ('help', 'Help'),
37     ('code', 'Code'),
38     ('constraint', 'Constraint'),
39 ]
40
41 class ir_translation(osv.osv):
42     _name = "ir.translation"
43     _log_access = False
44
45     def _get_language(self, cr, uid, context):
46         lang_obj = self.pool.get('res.lang')
47         lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)],
48                 context=context)
49         langs = lang_obj.browse(cr, uid, lang_ids, context=context)
50         res = [(lang.code, lang.name) for lang in langs]
51         for lang_dict in tools.scan_languages():
52             if lang_dict not in res:
53                 res.append(lang_dict)
54         return res
55
56     _columns = {
57         'name': fields.char('Field Name', size=128, required=True),
58         'res_id': fields.integer('Resource ID', select=True),
59         'lang': fields.selection(_get_language, string='Language', size=5),
60         'type': fields.selection(TRANSLATION_TYPE, string='Type', size=16, select=True),
61         'src': fields.text('Source'),
62         'value': fields.text('Translation Value'),
63     }
64
65     def _auto_init(self, cr, context={}):
66         super(ir_translation, self)._auto_init(cr, context)
67         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltns',))
68         if not cr.fetchone():
69             cr.execute('CREATE INDEX ir_translation_ltns ON ir_translation (lang, type, name, src)')
70             cr.commit()
71
72         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltn',))
73         if not cr.fetchone():
74             cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (lang, type, name)')
75             cr.commit()
76
77         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_lts',))
78         if not cr.fetchone():
79             cr.execute('CREATE INDEX ir_translation_lts ON ir_translation (lang, type, src)')
80             cr.commit()
81
82     @tools.cache(skiparg=3)
83     def _get_ids(self, cr, uid, name, tt, lang, ids):
84         translations = {}
85         if ids:
86             cr.execute('select res_id,value ' \
87                     'from ir_translation ' \
88                     'where lang=%s ' \
89                         'and type=%s ' \
90                         'and name=%s ' \
91                         'and res_id in ('+','.join(map(str, ids))+')',
92                     (lang,tt,name))
93             for res_id, value in cr.fetchall():
94                 translations[res_id] = value
95         for res_id in ids:
96             if res_id not in translations:
97                 translations[res_id] = False
98         return translations
99
100     def _set_ids(self, cr, uid, name, tt, lang, ids, value):
101         cr.execute('delete from ir_translation ' \
102                 'where lang=%s ' \
103                     'and type=%s ' \
104                     'and name=%s ' \
105                     'and res_id in ('+','.join(map(str,ids))+')',
106                 (lang,tt,name))
107         cr.commit()
108         for id in ids:
109             self.create(cr, uid, {
110                 'lang':lang,
111                 'type':tt,
112                 'name':name,
113                 'res_id':id,
114                 'value':value,
115                 })
116         return len(ids)
117
118     @tools.cache(skiparg=3)
119     def _get_source(self, cr, uid, name, tt, lang, source=None):
120         if source:
121             #if isinstance(source, unicode):
122             #   source = source.encode('utf8')
123             cr.execute('select value ' \
124                     'from ir_translation ' \
125                     'where lang=%s ' \
126                         'and type=%s ' \
127                         'and name=%s ' \
128                         'and src=%s',
129                     (lang, tt, str(name), source))
130         else:
131             cr.execute('select value ' \
132                     'from ir_translation ' \
133                     'where lang=%s ' \
134                         'and type=%s ' \
135                         'and name=%s',
136                     (lang, tt, str(name)))
137         res = cr.fetchone()
138         trad = res and res[0] or ''
139         return trad
140
141 ir_translation()
142
143 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
144