[MERGE] merge with main addons
[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 zipfile
23 import StringIO
24 import base64
25
26 import tools
27 from tools.translate import _
28 from osv import osv, fields
29
30
31 def _create_yaml(self, cr, uid, data, context=None):
32     mod = self.pool.get('ir.module.record')
33     try:
34         res_xml = mod.generate_yaml(cr, uid)
35     except Exception, e:
36         raise osv.except_osv(_('Error'),_(str(e)))
37     return {
38     'yaml_file': base64.encodestring(res_xml),
39 }
40     
41 def _create_module(self, cr, uid, ids, context=None):
42     mod = self.pool.get('ir.module.record')
43     res_xml = mod.generate_xml(cr, uid)
44     data = self.read(cr, uid, ids, [], context=context)[0]
45     s = StringIO.StringIO()
46     zip = zipfile.ZipFile(s, 'w')
47     dname = data['directory_name']
48     data['update_name'] = ''
49     data['demo_name'] = ''
50     if ['data_kind'] =='demo':
51         data['demo_name'] = '"%(directory_name)s_data.xml"' % data
52     else:
53         data['update_name'] = '"%(directory_name)s_data.xml"' % data
54     data['depends'] = ','.join(map(lambda x: '"'+x+'"', mod.depends.keys()))
55     _terp = """{
56         "name" : "%(name)s",
57         "version" : "%(version)s",
58         "author" : "%(author)s",
59         "website" : "%(website)s",
60         "category" : "%(category)s",
61         "description": \"\"\"%(description)s\"\"\",
62         "depends" : [%(depends)s],
63         "init_xml" : [ ],
64         "demo_xml" : [ %(demo_name)s],
65         "update_xml" : [%(update_name)s],
66         "installable": True
67 } """ % data
68     filewrite = {
69         '__init__.py':'#\n# Generated by the OpenERP module recorder !\n#\n',
70         '__openerp__.py':_terp,
71         dname+'_data.xml': res_xml
72     }
73     for name,datastr in filewrite.items():
74         info = zipfile.ZipInfo(dname+'/'+name)
75         info.compress_type = zipfile.ZIP_DEFLATED
76         info.external_attr = 2175008768
77         if not datastr:
78             datastr = ''
79         zip.writestr(info, datastr)
80     zip.close()
81     return {
82         'module_file': base64.encodestring(s.getvalue()),
83         'module_filename': data['directory_name']+'-'+data['version']+'.zip'
84     }
85
86 class base_module_save(osv.osv_memory):
87     _name = 'base.module.save'
88     _description = "Base Module Save"
89
90     def default_get(self, cr, uid, fields, context=None):
91         mod = self.pool.get('ir.module.record')
92         result = {}
93         info = "Details of "+str(len(mod.recording_data))+" Operation(s):\n\n"
94         res = super(base_module_save, self).default_get(cr, uid, fields, context=context)
95         for line in mod.recording_data:
96             result.setdefault(line[0],{})
97             result[line[0]].setdefault(line[1][3], {})
98             result[line[0]][line[1][3]].setdefault(line[1][3], 0)
99             result[line[0]][line[1][3]][line[1][3]]+=1
100         for key1,val1 in result.items():
101             info+=key1+"\n"
102             for key2,val2 in val1.items():
103                 info+="\t"+key2+"\n"
104                 for key3,val3 in val2.items():
105                     info+="\t\t"+key3+" : "+str(val3)+"\n"
106         if 'info_text' in fields:
107             res.update({'info_text': info})
108         if 'info_status' in fields:
109             info_status = mod.recording and 'record' or 'no'
110             res.update({'info_status': info_status})
111         return res
112     
113     _columns = {
114         'info_text': fields.text('Information', readonly=True),
115         'info_status': fields.selection([('no', 'Not Recording'),('record', 'Recording')], 'Status', readonly=True),
116         'info_yaml': fields.boolean('YAML'),
117     }
118
119     def record_save(self, cr, uid, ids, context=None):
120         data = self.read(cr, uid, ids, [], context=context)[0]
121         mod = self.pool.get('ir.module.record')
122         mod_obj = self.pool.get('ir.model.data')
123         if len(mod.recording_data):
124             if data['info_yaml']:
125                 mod = self.pool.get('ir.module.record')
126                 res=_create_yaml(self, cr, uid, data, context)
127                 model_data_ids = mod_obj.search(cr, uid,[('model', '=', 'ir.ui.view'), ('name', '=', 'yml_save_form_view')], context=context)
128                 resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
129                 return {
130                     'name': _('Message'),
131                     'context':  {
132                         'default_yaml_file': tools.ustr(res['yaml_file']),
133                         },
134                     'view_type': 'form',
135                     'view_mode': 'form',
136                     'res_model': 'base.module.record.objects',
137                     'views': [(resource_id, 'form')],
138                     'type': 'ir.actions.act_window',
139                     'target': 'new',
140                 }
141             else:
142                 model_data_ids = mod_obj.search(cr, uid,[('model', '=', 'ir.ui.view'), ('name', '=', 'info_start_form_view')], context=context)
143                 resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
144                 return {
145                     'name': _('Message'),
146                     'context': context,
147                     'view_type': 'form',
148                     'view_mode': 'form',
149                     'res_model': 'base.module.record.objects',
150                     'views': [(resource_id, 'form')],
151                     'type': 'ir.actions.act_window',
152                     'target': 'new',
153                 }
154         model_data_ids = mod_obj.search(cr, uid,[('model', '=', 'ir.ui.view'), ('name', '=', 'module_recording_message_view')], context=context)
155         resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
156         
157         return {
158             'name': _('Message'),
159             'context': context,
160             'view_type': 'form',
161             'view_mode': 'form',
162             'res_model': 'base.module.record.objects',
163             'views': [(resource_id, 'form')],
164             'type': 'ir.actions.act_window',
165             'target': 'new',
166         }      
167         
168 base_module_save()
169
170 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: