[FIX] Remove the unicode function
[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-2008 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 from osv.osv  import Cacheable
25 import tools
26
27 TRANSLATION_TYPE = [
28     ('field', 'Field'),
29     ('model', 'Object'),
30     ('rml', 'RML'),
31     ('selection', 'Selection'),
32     ('view', 'View'),
33     ('wizard_button', 'Wizard Button'),
34     ('wizard_field', 'Wizard Field'),
35     ('wizard_view', 'Wizard View'),
36     ('xsl', 'XSL'),
37     ('help', 'Help'),
38     ('code', 'Code'),
39     ('constraint', 'Constraint'),
40 ]
41
42 class ir_translation(osv.osv, Cacheable):
43     _name = "ir.translation"
44     _log_access = False
45
46     def _get_language(self, cr, uid, context):
47         lang_obj = self.pool.get('res.lang')
48         lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)],
49                 context=context)
50         langs = lang_obj.browse(cr, uid, lang_ids, context=context)
51         res = [(lang.code, lang.name) for lang in langs]
52         for lang_dict in tools.scan_languages():
53             if lang_dict not in res:
54                 res.append(lang_dict)
55         return res
56
57     _columns = {
58         'name': fields.char('Field Name', size=128, required=True),
59         'res_id': fields.integer('Resource ID', select=True),
60         'lang': fields.selection(_get_language, string='Language', size=5),
61         'type': fields.selection(TRANSLATION_TYPE, string='Type', size=16, select=True),
62         'src': fields.text('Source'),
63         'value': fields.text('Translation Value'),
64     }
65
66     def _auto_init(self, cr, context={}):
67         super(ir_translation, self)._auto_init(cr, context)
68         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltns',))
69         if not cr.fetchone():
70             cr.execute('CREATE INDEX ir_translation_ltns ON ir_translation (lang, type, name, src)')
71             cr.commit()
72
73         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltn',))
74         if not cr.fetchone():
75             cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (lang, type, name)')
76             cr.commit()
77
78         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_lts',))
79         if not cr.fetchone():
80             cr.execute('CREATE INDEX ir_translation_lts ON ir_translation (lang, type, src)')
81             cr.commit()
82
83     def _get_ids(self, cr, uid, name, tt, lang, ids):
84         translations, to_fetch = {}, []
85         for id in ids:
86             trans = self.get((lang, name, id))
87             if trans is not None:
88                 translations[id] = trans
89             else:
90                 to_fetch.append(id)
91         if to_fetch:
92             cr.execute('select res_id,value ' \
93                     'from ir_translation ' \
94                     'where lang=%s ' \
95                         'and type=%s ' \
96                         'and name=%s ' \
97                         'and res_id in ('+','.join(map(str, to_fetch))+')',
98                     (lang,tt,name))
99             for res_id, value in cr.fetchall():
100                 self.add((lang, tt, name, res_id), value)
101                 translations[res_id] = value
102         for res_id in ids:
103             if res_id not in translations:
104                 self.add((lang, tt, name, res_id), False)
105                 translations[res_id] = False
106         return translations
107
108     def _set_ids(self, cr, uid, name, tt, lang, ids, value):
109         cr.execute('delete from ir_translation ' \
110                 'where lang=%s ' \
111                     'and type=%s ' \
112                     'and name=%s ' \
113                     'and res_id in ('+','.join(map(str,ids))+')',
114                 (lang,tt,name))
115         for id in ids:
116             self.create(cr, uid, {
117                 'lang':lang,
118                 'type':tt,
119                 'name':name,
120                 'res_id':id,
121                 'value':value,
122                 })
123         return len(ids)
124
125     def _get_source(self, cr, uid, name, tt, lang, source=None):
126         trans = self.get((lang, tt, name, source))
127         if trans is not None:
128             return trans
129
130         if source:
131             #if isinstance(source, unicode):
132             #   source = source.encode('utf8')
133             cr.execute('select value ' \
134                     'from ir_translation ' \
135                     'where lang=%s ' \
136                         'and type=%s ' \
137                         'and name=%s ' \
138                         'and src=%s',
139                     (lang, tt, str(name), source))
140         else:
141             cr.execute('select value ' \
142                     'from ir_translation ' \
143                     'where lang=%s ' \
144                         'and type=%s ' \
145                         'and name=%s',
146                     (lang, tt, str(name)))
147         res = cr.fetchone()
148         
149         trad = res and res[0] or ''
150         self.add((lang, tt, name, source), trad)
151         return trad
152
153     def unlink(self, cursor, user, ids, context=None):
154         self.clear()
155         return super(ir_translation, self).unlink(cursor, user, ids,
156                 context=context)
157
158     def create(self, cursor, user, vals, context=None):
159         self.clear()
160         return super(ir_translation, self).create(cursor, user, vals,
161                 context=context)
162
163     def write(self, cursor, user, ids, vals, context=None):
164         self.clear()
165         return super(ir_translation, self).write(cursor, user, ids, vals,
166                 context=context)
167
168 ir_translation()
169
170 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
171