[IMP] base: language export wizard
[odoo/odoo.git] / openerp / addons / base / module / wizard / base_export_language.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Business Applications
5 #    Copyright (c) 2004-2012 OpenERP S.A. <http://openerp.com>
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 base64
23 import contextlib
24 import cStringIO
25
26 from openerp import tools
27 from openerp.osv import fields,osv
28 from openerp.tools.translate import _
29 from openerp.tools.misc import get_iso_codes
30
31 NEW_LANG_KEY = '__new__'
32
33 class base_language_export(osv.osv_memory):
34     _name = "base.language.export"
35
36     def _get_languages(self, cr, uid, context):
37         lang_obj = self.pool.get('res.lang')
38         ids = lang_obj.search(cr, uid, [('translatable', '=', True)])
39         langs = lang_obj.browse(cr, uid, ids)
40         return [(NEW_LANG_KEY, _('New Language (Empty translation template)'))] + [(lang.code, lang.name) for lang in langs]
41    
42     _columns = {
43             'name': fields.char('File Name', readonly=True),
44             'lang': fields.selection(_get_languages, 'Language', required=True), 
45             'format': fields.selection([('csv','CSV File'),
46                                         ('po','PO File'),
47                                         ('tgz', 'TGZ Archive')], 'File Format', required=True),
48             'modules': fields.many2many('ir.module.module', 'rel_modules_langexport', 'wiz_id', 'module_id', 'Modules To Export', domain=[('state','=','installed')]),
49             'data': fields.binary('File', readonly=True),
50             'state': fields.selection([('choose', 'choose'),   # choose language
51                                        ('get', 'get')])        # get the file
52     }
53     _defaults = { 
54         'state': 'choose',
55         'lang': NEW_LANG_KEY,
56         'format': 'csv',
57     }
58
59     def act_getfile(self, cr, uid, ids, context=None):
60         this = self.browse(cr, uid, ids, context=context)[0]
61         lang = this.lang if this.lang != NEW_LANG_KEY else False
62         mods = sorted(map(lambda m: m.name, this.modules)) or ['all']
63
64         with contextlib.closing(cStringIO.StringIO()) as buf:
65             tools.trans_export(lang, mods, buf, this.format, cr)
66             out = base64.encodestring(buf.getvalue())
67
68         filename = 'new'
69         if lang:
70             filename = get_iso_codes(lang)
71         elif len(mods) == 1:
72             filename = mods[0]
73         name = "%s.%s" % (filename, this.format)
74         this.write({ 'state': 'get', 'data': out, 'name': name })
75         return {
76             'type': 'ir.actions.act_window',
77             'res_model': 'base.language.export',
78             'view_mode': 'form',
79             'view_type': 'form',
80             'res_id': this.id,
81             'views': [(False, 'form')],
82             'target': 'new',
83         }
84
85
86 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: