-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodule_maker.py
278 lines (241 loc) · 10.8 KB
/
module_maker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#/#############################################################################
#
# BizzAppDev
# Copyright (C) 2014-TODAY bizzappdev(<http://www.bizzappdev.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#/#############################################################################
from template import OpenERPTemplate
from openerp.osv import osv
from openerp.osv import fields
import types
import os
import inspect
import tempfile
import zipfile
import base64
from openerp import addons
replace_list = [' ', ',', '.', '-']
def get_name(name):
for ch in replace_list:
name = name.replace(ch, "_")
return name
def zipdir(path, zip_file, temp_dir):
rootlen = len(path) + 1
for base, dirs, files in os.walk(path):
for file_path in files:
fn = os.path.join(base, file_path)
zip_file.write(fn, fn[rootlen:])
def _get_fields_type(self, cr, uid, context=None):
# Avoid too many nested `if`s below, as RedHat's Python 2.6
# break on it. See bug 939653.
return sorted([(k, k) for k, v in fields.__dict__.iteritems()
if isinstance(type(v), types.TypeType) and
inspect.isclass(v) and
issubclass(v, fields._column) and
v != fields._column and
not v._deprecated and
not issubclass(v, fields.function)]+[('button', 'Button')])
class module_module(osv.osv):
_name = 'module.module'
_description = 'Module '
def _get_file_name(self, cr, uid, ids, name, arg, context={}):
res = {}
for self_obj in self.browse(cr, uid, ids, context=context):
res[self_obj.id] = self_obj.name + '.zip'
return res
_columns = {
'name': fields.char('Module Name', size=64, required=True),
'template_path': fields.char("Tempalte Path", size=256, required=True),
'object_ids': fields.one2many('module.object', 'module_id',
'Module object'),
'module_file': fields.binary('Module File'),
'file_name': fields.function(_get_file_name,
method=True,
string='File Name',
type='char',
store=False, ),
}
def _get_tempalte_path(self, cr, uid, context={}):
return os.path.join(addons.__path__[0], "oe_module_maker",
"template")
_defaults = {
'template_path': _get_tempalte_path,
}
def get_comnpany_data(self, cr, uid, context={}):
company_pool = self.pool.get('res.company')
company_obj = company_pool.browse(
cr, uid, company_pool._company_default_get(
cr, uid, self._name, context=context), context=context)
return {'name': company_obj.name, 'short_name': company_obj.name,
'website': company_obj.website}
def genrate_module(self, cr, uid, ids, context={}):
for self_obj in self.browse(cr, uid, ids, context=context):
sub_folder = []
basic_file = []
xml_file = []
objs = []
openerp_data = {
'sub_folder': sub_folder,
'basic_file': basic_file,
'xml_file_list': xml_file,
'object_datas': []}
for obj in self_obj.object_ids:
sub_folder.append(get_name(obj.name))
objs.append(get_name(obj.name))
xml_file.append(os.path.join(get_name(obj.name), '%s_view.%s' %
(get_name(obj.name), 'xml')))
temp_dict = {
'list': [],
'_rec_name': '',
'inherit': '',
'name': obj.name,
'status': [],
'status_field': obj.status_field_id,
'status_values': obj.status_field_id and dict(
eval(obj.status_field_id.selection)) or {},
'sub_folder': get_name(obj.name)}
for field_obj in obj.field_ids:
temp_dict['list'].append({
'field': field_obj.name,
'filter': field_obj.filter,
'form': field_obj.form,
'help': field_obj.help,
'option': field_obj.selection,
'readonly': field_obj.readonly,
'related_field': field_obj.relation_field,
'relation': field_obj.relation,
'required': field_obj.required,
'search': field_obj.select_level,
'size': field_obj.size or '256',
'string': (field_obj.field_caption or
field_obj.name.capitalize()),
'tree': field_obj.tree,
'type': field_obj.type})
status_len = 0
all_state = [x.status for x in obj.status_line_ids
if x.priority == 0]
while (status_len+1 < len(obj.status_line_ids)):
status_obj = obj.status_line_ids[status_len]
temp_dict['status'].append({
'from_state': status_obj.status,
'to_state': obj.status_line_ids[status_len+1].status,
})
for all_st in all_state:
temp_dict['status'].append({
'from_state': status_obj.status,
'to_state': all_st
})
status_len += 1
openerp_data['object_datas'].append(temp_dict)
openerp_data['objs'] = objs
xml_file.append("security/ir.model.access.csv")
oe = OpenERPTemplate(self_obj.name, openerp_data,
self.get_comnpany_data(cr, uid, context),
self_obj.template_path)
temp_dir = tempfile.gettempdir()
module_path = oe.create_module(temp_dir)
zip_file_name = os.path.join(
temp_dir,
get_name(self_obj.name) + '.zip')
zip_file = zipfile.ZipFile(zip_file_name, 'w',
zipfile.ZIP_DEFLATED)
zipdir(module_path, zip_file, temp_dir)
zip_file.close()
file_obj = open(zip_file_name, "r")
file_data = file_obj.read()
file_data = base64.encodestring(file_data)
self.write(cr, uid, ids,
{'module_file': file_data})
return True
module_module()
class module_object(osv.osv):
_name = 'module.object'
_description = 'Module Object'
_columns = {
'name': fields.char('Object Name', size=64, required=True),
'description': fields.char('Object Description', size=64,
required=True),
'field_ids': fields.one2many('module.object.field', 'object_id',
'Object Fields'),
'module_id': fields.many2one('module.module', 'Module', required=True),
'status_field_id': fields.many2one('module.object.field',
'Status Based on Field'),
'status_line_ids': fields.one2many('module.object.status',
'object_id',
'Status Lines'),
}
def onchange_status_field(self, cr, uid, ids, status_field_id,
context=None):
ret_val = {'value': {'status_line_ids': []}}
if not status_field_id:
return ret_val
field_pool = self.pool.get('module.object.field')
f_obj = field_pool.browse(cr, uid, status_field_id, context=context)
final_lst = []
for prio,state in enumerate(eval(f_obj.selection)):
final_lst.append({'status': state[0], 'priority': prio+1})
ret_val['value']['status_line_ids'] = final_lst
return ret_val
module_object()
class module_object_status(osv.osv):
_name = 'module.object.status'
_order = 'priority'
_rec_name = 'status'
_columns = {
'object_id': fields.many2one('module.object.status'),
'status': fields.char('Status', size=64),
'priority': fields.integer(
'Priority', help='Set the priority based on your flow '
'e.g(Draft --> 1, Open --> 2, Done --> 3, cancel --> 0) '
'For the state which is common set 0')
}
class module_object_field(osv.osv):
_name = 'module.object.field'
_description = 'Object Field'
_columns = {
'name': fields.char('Field', size=64, required=True),
'type': fields.selection(_get_fields_type, 'Field Type',
required=True),
'relation_field': fields.char('Relation Field', size=64, ),
'relation': fields.many2one('module.object', 'Relation Object'),
'field_caption': fields.char('Field Label', size=64),
'selection': fields.char('Selection Options', size=128, ),
'required': fields.boolean('Required ?'),
'readonly': fields.boolean('Read-only ?'),
'select_level': fields.selection(
[('0', 'Not Searchable'), ('1', 'Always Searchable')],
'Searchable ?'),
'translate': fields.boolean('Translatable ?', ),
'size': fields.integer('Size'),
'on_delete': fields.selection(
[('cascade', 'Cascade'), ('set null', 'Set NULL')],
'On Delete', ),
'domain': fields.char('Domain', size=256, ),
'object_id': fields.many2one('module.object', 'Object'),
'help': fields.char('Help', size=256),
'filter': fields.boolean('Filter'),
'form': fields.boolean('Form'),
'tree': fields.boolean('tree'),
}
_defaults = {
'filter': lambda *a: True,
'form': lambda *a: True,
'tree': lambda *a: True,
}
module_object_field()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: