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