1
# -*- encoding: utf-8 -*-
2
#################################################################################
4
# base_json_field for OpenERP #
5
# Copyright (C) 2011 Akretion Sébastien BEAU <sebastien.beau@akretion.com> #
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. #
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. #
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/>. #
20
#################################################################################
22
from osv import fields, osv
26
class json_osv(osv.osv):
28
#TODO : json in product.template and json in product.product
30
def _get_js_fields(self, fields):
31
self_fields = self._columns.keys()
35
if 'x_js_' in field and field in self_fields:
36
store_field = field.split('_x_')[0].replace('x_js_', '')
37
if js_store_fields.get(store_field, False):
38
js_store_fields[store_field] += [field]
40
js_store_fields[store_field] = [field]
42
return js_store_fields, js_fields
44
def get_json_field_to_read(self, fields=None, context=None):
45
if context and context.get('call_from_browse', False):
46
return False, [x for x in fields if 'x_js' not in x]
48
return False, [x for x in self._columns.keys() if not 'x_js' in x]
50
js_store_fields, js_fields = self._get_js_fields(fields)
51
return js_store_fields, list(set(fields) - set(js_fields))
53
def fields_to_json(vals):
54
if 'x_js_' in '/'.join(vals.keys()):
55
js_store_fields = self._get_js_fields(vals.keys())
56
for js_store_field in js_store_fields:
58
for key in js_store_fields[js_store_field]:
59
if self._columns[key]._type == 'many2one':
60
print "the field %s have to be converted"%key
61
print dir(self._columns[key]._symbol_f)
64
vals[js_store_field] = json.dumps(res)
67
def fields_to_json_with_update(object, js_store_fields, vals):
68
#tmp_vals = self.convert_m2o_to_json(cr, uid, tmp_vals, context=context)
69
for js_store_field in js_store_fields:
70
#Read the actual value of json store field
71
if object[js_store_field]:
72
res = json.loads(object[js_store_field])
76
#Update the json store field with the value and remove json field from the tmp_vals
77
for key in js_store_fields[js_store_field]:
78
if self._columns[key]._type == 'many2one':
79
print "the field %s have to be converted"%key
80
print self._columns[key]._symbol_f
82
res[key] = tmp_vals[key]
84
vals[js_store_fields] = json.dumps(res)
89
def convert_json_to_field(self, vals, js_store_fields, fields):
92
#print "\n\n ===== product %s ====\n"%product.get('name', 'rien'), product.get('magento_fields', False)
93
if type(object) == dict:
94
for store_field in js_store_fields:
95
print 'object[store_field]', object[store_field]
96
if object[store_field]:
97
values = json.loads(object[store_field])
100
for field in js_store_fields[store_field]:
101
if ('x_js'+ store_field +'_x_') in field:
102
object[field] = values.get(field, False)
103
for field in js_store_fields:
104
if not field in fields:
105
del object[field_to_remove]
114
for key in js_store_fields[js_store_field]:
115
if self._columns[key]._type == 'many2one':
116
print "the field %s have to be converted"%key
117
print self._columns[key]._symbol_f._name
120
vals[js_store_field] = json.dumps(res)
129
def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
131
js_store_fields, fields_to_read = self.get_json_field_to_read(fields, context)
132
res = super(json_osv, self).read(cr, uid, ids, fields_to_read, context, load)
133
res2 = self.convert_json_to_field(res, js_store_fields, fields)
137
def create(self, cr, uid, vals, context=None):
138
if 'x_js_' in '/'.join(vals.keys()):
139
vals = self.fields_to_json(vals)
140
return super(product_product, self).create(cr, uid, vals, context)
142
def write(self, cr, uid, ids, vals, context=None):
143
if 'x_js_' in '/'.join(vals.keys()):
144
js_store_fields, js_fields = self._get_js_fields(vals.keys())
146
for object in self.read(cr, uid, ids, fields=js_store_fields, context=context):
147
vals = self.fields_to_json_with_update(object, js_store_fields, vals)
148
wrid += [super(json_osv, self).write(cr, uid, object['id'], vals, context=context)]
151
return super(json_osv, self).write(cr, uid, ids, vals, context)
176
def read_with_json(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
177
print 'context', context, fields
178
print 'we are in the read'
180
self_fields = self._columns.keys()
181
#print 'column', self._columns
182
#print "\n\n ===== original field to read ====\n", fields
184
if not fields or context.get('call_from_browse', False):
186
fields_to_read = [x for x in self_fields if not 'x_js' in x]
193
#All json field of the object are remove from the field to read and the json store field is added if necessary
194
if 'x_js_' in field and field in self_fields:
196
store_field = field.split('_x_')[0].replace('x_js_', '')
197
if not store_field in js_store_fields:
198
js_store_fields += [store_field]
200
fields_to_read += [field]
202
js_store_fields_to_add_and_remove = [x for x in js_store_fields if x not in fields_to_read]
203
fields_to_read += js_store_fields_to_add_and_remove
205
#print "\n\n ===== modif field to read ====\n", fields_to_read
206
res = super(json_osv, self).read(cr, uid, ids, fields_to_read, context, load)
210
#print "\n\n ===== product %s ====\n"%product.get('name', 'rien'), product.get('magento_fields', False)
211
if type(object) == dict:
212
for store_field in js_store_fields:
213
values = json.loads(object[store_field])
214
for field in js_fields:
215
if ('x_js'+ store_field +'_x_') in field:
216
object[field] = values.get(field, False)
217
for field_to_remove in js_store_fields_to_add_and_remove:
218
del object[field_to_remove]
219
print "\n\n ===== res ====\n", res
222
def convert_m2o_to_json(self, cr, uid, vals, context=None):
223
attributs_option_ids = []
225
if "x_js" in key and self._columns[key]._type == 'many2one':
226
attributs_option_ids += [vals[key]]
229
for attr in self.pool.get('magerp.product_attribute_options').read(cr, uid, attributs_option_ids, ['label'], context=context):
230
attributs_options.update({attr['id'] : attr['label']})
232
if "x_magerp_js" in key and self._columns[key]._type == 'many2one':
233
vals[key] = [vals[key], attributs_options[vals[key]]]
238
def tttcreate(self, cr, uid, vals, context=None):
239
if 'x_js_' in '/'.join(vals.keys()):
240
js_store_fields = self._get_js_fields(vals.keys())
241
for js_store_field in js_store_fields:
243
for key in js_store_fields[js_store_field]:
244
if self._columns[key]._type == 'many2one':
245
print "the field %s have to be converted"%key
246
print dir(self._columns[key]._symbol_f)
249
vals[js_store_field] = json.dumps(res)
251
return super(json_osv, self).create(cr, uid, vals, context)
254
#The update Json have to be done product per product because the original json can be different
255
def tttwrite(self, cr, uid, ids, vals, context=None):
256
if 'x_js_' in '/'.join(vals.keys()):
257
js_store_fields = self._get_js_fields(vals.key())
259
for object in self.read(cr, uid, ids, fields=js_store_fields, context=context):
260
tmp_vals = vals.copy()
261
#tmp_vals = self.convert_m2o_to_json(cr, uid, tmp_vals, context=context)
262
for js_store_field in js_store_fields:
263
#Read the actual value of json store field
264
if object[js_store_field]:
265
res = json.loads(object[js_store_field])
269
#Update the json store field with the value and remove json field from the tmp_vals
270
for key in js_store_fields[js_store_field]:
271
if self._columns[key]._type == 'many2one':
272
print "the field %s have to be converted"%key
273
print self._columns[key]._symbol_c
274
print self._columns[key]._symbol_f
275
print self._columns[key]._symbol_set
277
res[key] = tmp_vals[key]
279
tmp_vals[js_store_fields] = json.dumps(res)
281
wrid += [super(json_osv, self).write(cr, uid, object['id'], tmp_vals, context=context)]
284
return super(json_osv, self).write(cr, uid, ids, vals, context)