[IMP] base module: improve form views (mostly wizard buttons)
[odoo/odoo.git] / openerp / addons / base / module / wizard / base_import_language.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import tools
23 import base64
24 from tempfile import TemporaryFile
25 from osv import osv, fields
26
27 class base_language_import(osv.osv_memory):
28     """ Language Import """
29
30     _name = "base.language.import"
31     _description = "Language Import"
32     _inherit = "ir.wizard.screen"
33
34     _columns = {
35         'name': fields.char('Language Name',size=64 , required=True),
36         'code': fields.char('Code (eg:en__US)',size=5 , required=True),
37         'data': fields.binary('File', required=True),
38         'overwrite': fields.boolean('Overwrite Existing Terms',
39                                     help="If you enable this option, existing translations (including custom ones) "
40                                          "will be overwritten and replaced by those in this file"),
41     }
42
43     def import_lang(self, cr, uid, ids, context=None):
44         """
45             Import Language
46             @param cr: the current row, from the database cursor.
47             @param uid: the current user’s ID for security checks.
48             @param ids: the ID or list of IDs
49             @param context: A standard dictionary
50         """
51         if context is None:
52             context = {}
53         import_data = self.browse(cr, uid, ids)[0]
54         if import_data.overwrite:
55             context.update(overwrite=True)
56         fileobj = TemporaryFile('w+')
57         fileobj.write(base64.decodestring(import_data.data))
58
59         # now we determine the file format
60         fileobj.seek(0)
61         first_line = fileobj.readline().strip().replace('"', '').replace(' ', '')
62         fileformat = first_line.endswith("type,name,res_id,src,value") and 'csv' or 'po'
63         fileobj.seek(0)
64
65         tools.trans_load_data(cr, fileobj, fileformat, import_data.code, lang_name=import_data.name, context=context)
66         fileobj.close()
67         return {}
68
69 base_language_import()
70
71 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: