a2c653471e2877d34c1809486c746b2d28bb90aa
[odoo/odoo.git] / bin / addons / base / module / wizard / wizard_module_import.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
24 import wizard
25 import osv
26 import pooler
27 import os
28 import tools
29
30 import zipfile
31 from StringIO import StringIO
32 import base64
33
34 finish_form ='''<?xml version="1.0"?>
35 <form string="Module import">
36     <label string="Module successfully imported !" colspan="4"/>
37 </form>
38 '''
39
40 ask_form ='''<?xml version="1.0"?>
41 <form string="Module import">
42     <separator string="Module import" colspan="4"/>
43     <label string="Please give your module .ZIP file to import." colspan="4"/>
44     <field name="module_file"/>
45 </form>
46 '''
47
48 ask_fields = {
49     'module_file': {'string': 'Module .ZIP file', 'type': 'binary', 'required': True},
50 }
51
52 class move_module_wizard(wizard.interface):
53     def importzip(self, cr, uid, data, context):
54         module_obj=pooler.get_pool(cr.dbname).get('ir.module.module')
55         module_data = data['form']['module_file']
56
57         val =base64.decodestring(module_data)
58         fp = StringIO()
59         fp.write(val)
60         fdata = zipfile.ZipFile(fp, 'r')
61         fname = fdata.namelist()[0]
62         module_name = os.path.split(fname)[0]
63
64         ad = tools.config['addons_path']
65
66         fname = os.path.join(ad,module_name+'.zip')
67         try:
68             fp = file(fname, 'wb')
69             fp.write(val)
70             fp.close()
71         except IOError, e:
72             raise wizard.except_wizard(_('Error !'), _('Can not create the module file: %s !') % (fname,) )
73
74         pooler.get_pool(cr.dbname).get('ir.module.module').update_list(cr, uid)
75         return {'module_name': module_name}
76
77     def _action_module_open(self, cr, uid, data, context):
78         return {
79             'domain': str([('name', '=', data['form']['module_name'])]),
80             'name': 'Module List',
81             'view_type': 'form',
82             'view_mode': 'tree,form',
83             'res_model': 'ir.module.module',
84             'view_id': False,
85             'type': 'ir.actions.act_window'
86         }
87
88     states = {
89         'init': {
90             'actions': [],
91             'result': {
92                 'type': 'form',
93                 'arch': ask_form,
94                 'fields': ask_fields,
95                 'state': [
96                     ('end', 'Cancel', 'gtk-cancel'),
97                     ('import', 'Import module', 'gtk-ok', True)
98                 ]
99             }
100         },
101         'import': {
102             'actions': [importzip],
103             'result': {
104                 'type':'form',
105                 'arch':finish_form,
106                 'fields':{},
107                 'state':[('open_window','Close')]
108             }
109         },
110         'open_window': {
111             'actions': [],
112             'result': {'type': 'action', 'action': _action_module_open, 'state':'end'}
113         },
114     }
115 move_module_wizard('base.module.import')
116
117 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
118