[IMP] moved openerpweb into base.common to be renamed web.commom
[odoo/odoo.git] / addons / base_module_record / wizard / base_module_save.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 wizard
23 import osv
24 import pooler
25 from tools.translate import _
26
27 info = '''<?xml version="1.0"?>
28 <form string="Module Recording">
29     <label string="Thanks For using Module Recorder" colspan="4" align="0.0"/>
30 </form>'''
31
32 info_start_form = '''<?xml version="1.0"?>
33 <form string="Module Recording">
34     <separator string="Recording Information" colspan="4"/>
35     <field name="info_status"/>
36     <field name="info_text" colspan="4" nolabel="1"/>
37     <field name="info_yaml" colspan="4"/>
38 </form>'''
39
40 info_start_fields = {
41     'info_text': {'string':'Information', 'type':'text', 'readonly':True},
42     'info_status': {'string':'Status','type':'selection', 'selection':[('no','Not Recording'),('record','Recording')], 'readonly':True},
43     'info_yaml': {'string':'YAML','type':'boolean'}
44 }
45
46
47
48 intro_start_form = '''<?xml version="1.0"?>
49 <form string="Module Recording">
50     <separator string="Module Information" colspan="4"/>
51     <field name="name"/>
52     <field name="directory_name"/>
53     <field name="version"/>
54     <field name="author"/>
55     <field name="website" colspan="4"/>
56     <field name="category" colspan="4"/>
57     <field name="data_kind"/>
58     <newline/>
59     <field name="description" colspan="4"/>
60 </form>'''
61
62 intro_start_fields = {
63     'name': {'string':'Module Name', 'type':'char', 'size':64, 'required':True},
64     'directory_name': {'string':'Directory Name', 'type':'char', 'size':32, 'required':True},
65     'version': {'string':'Version', 'type':'char', 'size':16, 'required':True},
66     'author': {'string':'Author', 'type':'char', 'size':64, 'default': lambda *args: 'OpenERP SA', 'required':True},
67     'category': {'string':'Category', 'type':'char', 'size':64, 'default': lambda *args: 'Vertical Modules/Parametrization', 'required':True},
68     'website': {'string':'Documentation URL', 'type':'char', 'size':64, 'default': lambda *args: 'http://www.openerp.com', 'required':True},
69     'description': {'string':'Full Description', 'type':'text', 'required':True},
70     'data_kind': {'string':'Type of Data', 'type':'selection', 'selection':[('demo','Demo Data'),('update','Normal Data')], 'required':True, 'default': lambda *args:'update'},
71 }
72
73 intro_save_form = '''<?xml version="1.0"?>
74 <form string="Module Recording">
75     <separator string="Module successfully created !" colspan="4"/>
76     <field name="module_filename"/>
77     <newline/>
78     <field name="module_file" filename="module_filename"/>
79     <separator string="Information" colspan="4"/>
80      <label string="If you think your module could interest other people, we'd like you to publish it on http://www.openerp.com, in the 'Modules' section. You can do it through the website or using features of the 'base_module_publish' module." colspan="4" align="0.0"/>
81     <label string="Thanks in advance for your contribution." colspan="4" align="0.0"/>
82 </form>'''
83
84 intro_save_fields = {
85     'module_file': {'string': 'Module .zip File', 'type':'binary', 'readonly':True},
86     'module_filename': {'string': 'Filename', 'type':'char', 'size': 64, 'readonly':True},
87 }
88
89 yaml_save_form = '''<?xml version="1.0"?>
90 <form string="Module Recording">
91     <separator string="YAML file successfully created !" colspan="4"/>
92     <newline/>
93     <field name="yaml_file" filename="module_filename"/>
94 </form>'''
95
96 yaml_save_fields = {
97     'yaml_file': {'string': 'Module .zip File', 'type':'binary'},
98 }
99
100 import zipfile
101 import StringIO
102 import base64
103
104 def _info_default(self, cr, uid, data, context):
105     pool = pooler.get_pool(cr.dbname)
106     mod = pool.get('ir.module.record')
107     result = {}
108     info = "Details of "+str(len(mod.recording_data))+" Operation(s):\n\n"
109
110     for line in mod.recording_data:
111         result.setdefault(line[0],{})
112         result[line[0]].setdefault(line[1][3], {})
113         result[line[0]][line[1][3]].setdefault(line[1][3], 0)
114         result[line[0]][line[1][3]][line[1][3]]+=1
115     for key1,val1 in result.items():
116         info+=key1+"\n"
117         for key2,val2 in val1.items():
118             info+="\t"+key2+"\n"
119             for key3,val3 in val2.items():
120                 info+="\t\t"+key3+" : "+str(val3)+"\n"
121     return {'info_text': info, 'info_status':mod.recording and 'record' or 'no'}
122
123 def _create_yaml(self, cr, uid, data, context):
124     pool = pooler.get_pool(cr.dbname)
125     mod = pool.get('ir.module.record')
126     try:
127         res_xml = mod.generate_yaml(cr, uid)
128     except Exception, e:
129         raise wizard.except_wizard(_('Error'),_(str(e)))
130     return {
131     'yaml_file': base64.encodestring(res_xml),
132 }
133     
134 def _create_module(self, cr, uid, data, context):
135     pool = pooler.get_pool(cr.dbname)
136     mod = pool.get('ir.module.record')
137     res_xml = mod.generate_xml(cr, uid)
138     
139     s=StringIO.StringIO()
140     zip = zipfile.ZipFile(s, 'w')
141     dname = data['form']['directory_name']
142     data['form']['update_name'] = ''
143     data['form']['demo_name'] = ''
144     if data['form']['data_kind'] =='demo':
145         data['form']['demo_name'] = '"%(directory_name)s_data.xml"' % data['form']
146     else:
147         data['form']['update_name'] = '"%(directory_name)s_data.xml"' % data['form']
148     data['form']['depends'] = ','.join(map(lambda x: '"'+x+'"',mod.depends.keys()))
149     _terp = """{
150         "name" : "%(name)s",
151         "version" : "%(version)s",
152         "author" : "%(author)s",
153         "website" : "%(website)s",
154         "category" : "%(category)s",
155         "description": \"\"\"%(description)s\"\"\",
156         "depends" : [%(depends)s],
157         "init_xml" : [ ],
158         "demo_xml" : [ %(demo_name)s],
159         "update_xml" : [%(update_name)s],
160         "installable": True
161 } """ % data['form']
162     filewrite = {
163         '__init__.py':'#\n# Generated by the OpenERP module recorder !\n#\n',
164         '__openerp__.py':_terp,
165         dname+'_data.xml': res_xml
166     }
167     for name,datastr in filewrite.items():
168         info = zipfile.ZipInfo(dname+'/'+name)
169         info.compress_type = zipfile.ZIP_DEFLATED
170         info.external_attr = 2175008768
171         if not datastr:
172             datastr = ''
173         zip.writestr(info, datastr)
174     zip.close()
175     return {
176         'module_file': base64.encodestring(s.getvalue()),
177         'module_filename': data['form']['directory_name']+'-'+data['form']['version']+'.zip'
178     }
179 def _check(self, cr, uid, data, context):
180      pool = pooler.get_pool(cr.dbname)
181      mod = pool.get('ir.module.record')
182      if len(mod.recording_data):
183          if data['form']['info_yaml']:
184              return 'save_yaml'
185          else:
186              return 'info'
187      else:
188          return 'end'
189
190 class base_module_publish(wizard.interface):
191     states = {
192         'init': {
193             'actions': [_info_default],
194             'result': {
195                 'type':'form',
196                 'arch':info_start_form,
197                 'fields': info_start_fields,
198                 'state':[
199                     ('end', 'Cancel', 'gtk-cancel'),
200                     ('check', 'Continue', 'gtk-ok'),
201                 ]
202             }
203         },
204         'check': {
205             'actions': [],
206             'result': {'type':'choice','next_state':_check}
207         },
208         'info': {
209             'actions': [],
210             'result': {
211                 'type':'form',
212                 'arch':intro_start_form,
213                 'fields': intro_start_fields,
214                 'state':[
215                     ('end', 'Cancel', 'gtk-cancel'),
216                     ('save', 'Continue', 'gtk-ok'),
217                 ]
218             }
219         },
220         'save': {
221             'actions': [_create_module],
222             'result': {
223                 'type':'form',
224                 'arch':intro_save_form,
225                 'fields': intro_save_fields,
226                 'state':[
227                     ('end', 'Close', 'gtk-ok'),
228                 ]
229             }
230         },
231         'save_yaml': {
232             'actions': [_create_yaml],
233             'result': {
234                 'type':'form',
235                 'arch':yaml_save_form,
236                 'fields': yaml_save_fields,
237                 'state':[
238                     ('end', 'Close', 'gtk-ok'),
239                 ]
240             }
241          },
242          
243         'end': {
244             'actions': [],
245             'result': {'type':'form', 'arch':info, 'fields':{}, 'state':[('end','OK')]}
246         },
247     }
248 base_module_publish('base_module_record.module_save')
249
250 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
251