forked from lasarux/ooop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ooop.py
722 lines (630 loc) · 26.8 KB
/
ooop.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# -*- encoding: utf-8 -*-
########################################################################
#
# OOOP, OpenObject On Python
# Copyright (C) 2010 Pedro A. Gracia Fajardo. All Rights Reserved.
# $Id$
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
########################################################################
import xmlrpclib
import time
import base64
import types
from datetime import datetime, date
# check if pydot is installed
try:
import pydot
except:
pydot = False
__author__ = "Pedro Gracia <[email protected]>"
__license__ = "GPLv3+"
__version__ = "0.2.3"
OOOPMODELS = 'ir.model'
OOOPFIELDS = '%s.fields' % OOOPMODELS
OPERATORS = {
'lt': '<',
'lte': '<=',
'gt': '>',
'gte': '>=',
'ne': '!=',
'ne': '<>',
'like': 'like',
'ilike': 'ilike',
'eq_like': '=like',
'not_like': 'not like',
'not_ilike':'not ilike',
'in': 'in',
'not_in': 'not in',
'child_of': 'child of',
}
def remove(object):
del object
# http://stackoverflow.com/questions/1305532/convert-python-dict-to-object (last one)
class dict2obj(dict):
def __init__(self, dict_):
super(dict2obj, self).__init__(dict_)
for key in self:
item = self[key]
if isinstance(item, list):
for idx, it in enumerate(item):
if isinstance(it, dict):
item[idx] = dict2obj(it)
elif isinstance(item, dict):
self[key] = dict2obj(item)
def __getattr__(self, key):
return self[key]
class objectsock_mock():
"""mock for objectsock to be able to use the OOOP as a module inside of openerp"""
def __init__(self, parent, cr):
self.parent = parent
self.cr = cr
def execute(self, *args, **kargs):
"""mocking execute function"""
if len(args) == 7:
(dbname, uid, pwd, model, action, vals, fields) = args
elif len(args) == 6:
(dbname, uid, pwd, model, action, vals) = args
elif len(args) == 5:
(dbname, uid, pwd, model, action) = args
_model = self.parent.pool.get(model)
if action == 'create':
return _model.create(self.cr, uid, vals)
elif action == 'unlink':
return _model.unlink(self.cr, uid, vals)
elif action == 'write':
return _model.write(self.cr, uid, vals, fields)
elif action == 'read' and len(args) == 7:
return _model.read(self.cr, uid, vals, fields)
elif action == 'read':
return _model.read(self.cr, uid, vals)
elif action == 'search':
return _model.search(self.cr, uid, vals)
else:
return getattr(_model, action)(self.cr, uid) # is callable
class OOOP:
""" Main class to manage xml-rpc comunitacion with openerp-server """
def __init__(self, user='admin', pwd='admin', dbname='openerp',
uri='http://localhost', port=8069, debug=False,
exe=False, active=True, **kwargs):
self.user = user # default: 'admin'
self.pwd = pwd # default: 'admin'
self.dbname = dbname # default: 'openerp'
self.uri = uri
self.port = port
self.debug = debug
self.exe = exe
self.active = active
self.commonsock = None
self.objectsock = None
self.reportsock = None
self.uid = None
self.models = {}
self.fields = {}
#has to be uid, cr, parent (the openerp model to get the pool)
if len(kwargs) == 3:
self.uid = kwargs['uid']
self.objectsock = objectsock_mock(kwargs['parent'], kwargs['cr'])
else:
self.connect()
self.load_models()
def connect(self):
"""login and sockets to xmlrpc services: common, object and report"""
self.uid = self.login(self.dbname, self.user, self.pwd)
self.objectsock = xmlrpclib.ServerProxy('%s:%i/xmlrpc/object' % (self.uri, self.port))
self.reportsock = xmlrpclib.ServerProxy('%s:%i/xmlrpc/report' % (self.uri, self.port))
def login(self, dbname, user, pwd):
self.commonsock = xmlrpclib.ServerProxy('%s:%i/xmlrpc/common' % (self.uri, self.port))
return self.commonsock.login(dbname, user, pwd)
def execute(self, model, *args):
if self.debug:
print "DEBUG [execute]:", model, args
return self.objectsock.execute(self.dbname, self.uid, self.pwd, model, *args)
def create(self, model, data):
""" create a new register """
if self.debug:
print "DEBUG [create]:", model, data
return self.objectsock.execute(self.dbname, self.uid, self.pwd, model, 'create', data)
def unlink(self, model, ids):
""" remove register """
if self.debug:
print "DEBUG [unlink]:", model, ids
return self.objectsock.execute(self.dbname, self.uid, self.pwd, model, 'unlink', ids)
def write(self, model, ids, value):
""" update register """
if self.debug:
print "DEBUG [write]:", model, ids, value
return self.objectsock.execute(self.dbname, self.uid, self.pwd, model, 'write', ids, value)
def read(self, model, ids, fields=[]):
""" update register """
if self.debug:
print "DEBUG [read]:", model, ids, fields
return self.objectsock.execute(self.dbname, self.uid, self.pwd, model, 'read', ids, fields)
def read_all(self, model, fields=[]):
""" update register """
if self.debug:
print "DEBUG [read_all]:", model, fields
return self.objectsock.execute(self.dbname, self.uid, self.pwd, model, 'read', self.all(model), fields)
def search(self, model, query):
""" return ids that match with 'query' """
if self.debug:
print "DEBUG [search]:", model, query
return self.objectsock.execute(self.dbname, self.uid, self.pwd, model, 'search', query)
# TODO: verify if remove this
def custom_execute(self, model, ids, remote_method, data):
if self.debug:
print "DEBUG [custom_execute]:", self.dbname, self.uid, self.pwd, model, args
return self.objectsock.execute(self.dbname, self.uid, self.pwd, model, ids, remote_method, data)
def all(self, model, query=[]):
""" return all ids """
if self.debug:
print "DEBUG [all]:", model, query
return self.search(model, query)
def insert_items(self, model, data):
""" insert items into database """
for item in data:
d = {'name': item}
query = [('name', '=', item)]
if not self.search(model, query): # check if it's already present
self.create(model, d)
else:
print 'Warning: %s already in %s model: %s' % (item, model)
def load_models(self):
models = self.read_all(OOOPMODELS)
for model in models:
self.models[model['model']] = model
self.__dict__[self.normalize_model_name(model['model'])] = Manager(model['model'], self)
def set_model(self, model, r={}, deep=None):
"""docstring for set_model"""
if not model in r.keys():
r[model] = {
'name': self.normalize_model_name(model),
'links': {},
'fields': [] #TODO: storage i?
}
flds = self.read(OOOPFIELDS, self.models[model]['field_id'])
for fld in flds:
r[model]['fields'].append((fld["name"], fld["ttype"]))
if fld["ttype"] in ('one2many', 'many2one', 'many2many'):
if deep > 0 and not r[model]['links'].has_key(fld['relation']):
r[model]['links'][fld['relation']] = fld['ttype']
r = self.set_model(fld["relation"], r, deep-1)
return r
def export(self, filename, filetype, showfields=True, model=None, deep=-1):
"""Export the model to dot file"""
#o2m 0..* m2m *..* m2o *..0
if not pydot:
raise ImportError('no pydot package found')
if filename == "":
raise TypeError('no filename')
if not filetype in ('dot', 'png', 'jpg', 'svg'):
raise TypeError('filetype only accept dot, png, jpg or svg types')
graph = pydot.Dot(graph_type='digraph')
graph.set_ratio('compress')
HEADER = """<
<TABLE BGCOLOR="palegoldenrod" BORDER="0" CELLBORDER="0" CELLSPACING="0">
<TR>
<TD COLSPAN="2" CELLPADDING="4" ALIGN="CENTER" BGCOLOR="olivedrab4">
<FONT FACE="Helvetica Bold" COLOR="white">%s</FONT>
</TD>
</TR>
"""
FIELD = """
<TR><TD ALIGN="LEFT" BORDER="0"
><FONT FACE="Helvetica Bold">%s</FONT
></TD>
<TD ALIGN="LEFT"
><FONT FACE="Helvetica Bold">%s</FONT
></TD></TR>
"""
TAIL = """
</TABLE>
>"""
r = {}
if not model:
models = self.read_all(OOOPMODELS)
else:
model_id = self.search(OOOPMODELS, [('model','=', model)])
models = self.read(OOOPMODELS, model_id)
for model in models:
r = self.set_model(model['model'], r, deep)
lines = ""
for key, value in r.items():
if showfields:
fields = ''.join([FIELD % k for k in value['fields']])
else:
fields = ''
label = HEADER % key + fields + TAIL
node = pydot.Node(value['name'], label=label)
node.set_shape('none')
graph.add_node(node)
for i in value['links'].keys():
edge = pydot.Edge(value['name'], r[i]['name'])
if value['links'][i] == 'one2many':
edge.set_headlabel('0..*')
edge.set_taillabel('*..0')
elif value['links'][i] == 'many2one':
edge.set_headlabel('*..0')
edge.set_taillabel('0..*')
elif value['links'][i] == 'many2many':
edge.set_headlabel("*..*")
edge.set_taillabel("*..*")
graph.add_edge(edge)
graph.set_fontsize('8.0')
filename = '%s.%s' % (filename, filetype)
if filetype == 'dot':
graph.write_dot(filename)
elif filetype == 'png':
graph.write_png(filename)
elif filetype == 'jpg':
graph.write_jpg(filename)
elif filetype == 'svg':
graph.write_svg(filename)
def normalize_model_name(self, name):
return "".join(["%s" % k.capitalize() for k in name.split('.')])
def report(self, model, ref, report_type='pdf'):
""" return a report """
# TODO: test this function
data = {'model': model, 'id': ref[0], 'report_type': report_type}
id_report = self.reportsock.report(self.dbname, self.uid, self.pwd, model, ref, data)
time.sleep(5)
state = False
attempt = 0
while not state:
report = self.reportsock.report_get(self.dbname, self.uid, self.pwd, id_report)
state = report['state']
if not state:
time.sleep(1)
attempt += 1
if attempt > 200:
print 'Printing aborted, too long delay!'
break
if report_type == 'pdf':
string_pdf = base64.decodestring(report['result'])
return string_pdf
else:
return report['result']
# reference: http://www.java2s.com/Code/Python/Class/MyListinitaddmulgetitemlengetslice.htm
class List:
""" An 'smart' list """
#FIXME: reorder args
#TODO: cache
def __init__(self, manager, objects=None, parent=None,
low=None, high=None, data=None, model=None):
self.manager = manager # List or Manager instance
if model:
self.model = model
else:
self.model = self.manager._model
if not objects:
self.objects = []
else:
self.objects = objects
self.parent = parent # List instance
self.low = low
self.high = high
self.data = data
self.index = -1
def __iter__(self):
return self
def next(self):
self.index += 1
if self.index == len(self.objects):
self.index = -1
raise StopIteration
return self.__getitem__(self.index)
def delete(self):
if self.parent:
objects = self.parent.objects
self.parent.objects = objects[:self.low] + objects[self.high:]
return self.manager._ooop.unlink(self.model, self.objects)
def append(self, value):
if self.data:
self.data.INSTANCES['%s:%s' % (self.model, value._ref)] = value
self.objects.append(value)
def __getslice__(self, low, high):
return List(self.manager, self.objects[low:high], self, low, high, data=self.data, model=self.model)
def __getitem__(self, offset):
if type(self.objects[offset]) != types.IntType:
return self.objects[offset]
else:
#if type(data) != Types.StringType: # data != model
# self.data.INSTANCES['%s:%s' % (self.data.model, self.objects[offset])] = instance # TODO: method to add instances
self.objects[offset] = Data(self.manager, self.objects[offset], model=self.model)
return self.objects[offset]
def __len__(self):
return len(self.objects)
def __repr__(self):
return '<Objects from %s> %i elements' % (self.model, len(self.objects))
class Manager:
def __init__(self, model, ooop):
self._model = model
self._ooop = ooop
def __getattr__(self, name):
return lambda *a: self._ooop.execute(self._model, name, *a)
def get(self, ref): # TODO: only ids?
return Data(self, ref)
def new(self, *args, **kargs):
return Data(self, *args, **kargs)
def copy(self, ref):
return Data(self, ref, copy=True)
def read(self, ids, fields=[]):
data = self._ooop.read(self._model, ids, fields)
res = []
for item in data:
row = {'id': item['id']}
for i in fields:
row[i] = item[i]
res.append(dict2obj(row))
return res
def all(self, fields=[], offset=0, limit=999999, as_list=False):
ids = self._ooop.all(self._model)
if not ids: # CHECKTHIS
return []
data = self._ooop.read(self._model, ids[offset:limit], fields)
if as_list:
res = []
for item in data:
row = {'id': item['id']}
for i in fields:
row[i] = item[i]
res.append(dict2obj(row))
return res
return List(self, ids) #manager, object ids
def filter(self, fields=[], as_list=False, **kargs):
q = [] # query dict
for key, value in kargs.items():
if not '__' in key:
op = '='
else:
i = key.find('__')
op = OPERATORS[key[i+2:]]
key = key[:i]
q.append(('%s' % key, op, value))
ids = self._ooop.search(self._model, q)
if as_list:
return self.read(ids, fields)
return List(self, ids)
def exclude(self, *args, **kargs):
pass # TODO
def export(self, filename=None, filetype="jpg", deep=0):
"""docstring for export"""
if not filename:
filename = self._model
self._ooop.export(filename=filename, filetype=filetype, model=self._model, deep=deep)
def __repr__(self):
return '<%s> manager instance' % self._model
class Data(object):
def __init__(self, manager, ref=None, model=None, copy=False, data=None, fields=[], *args, **kargs):
if not model:
self._model = manager._model # model name # FIXME!
else:
self._model = model
self._manager = manager
self._ooop = self._manager._ooop
self._copy = copy
self._data = data
self._fields = fields
self.INSTANCES = {}
if ref:
self._ref = ref
else:
self._ref = -id(self)
self.INSTANCES['%s:%s' % (self._model, self._ref)] = self
if self._model in self._ooop.fields.keys():
self.fields = self._ooop.fields[self._model]
else:
fields = self._manager.fields_get()
self.fields = {}
for field_name, field in fields.items():
field['name'] = field_name
field['relation'] = field.get('relation', False)
field['ttype'] = field['type']
del field['type']
self.fields[field_name] = field
self._ooop.fields[self._model] = self.fields
# get current data for this object
if ref:
self.get_values()
else:
# get default values
default_values = {}
field_names = self.fields.keys()
default_values = self._manager.default_get(field_names)
# active by default ?
if self._ooop.active:
default_values['active'] = True
default_values.update(**kargs) # initial values from caller
self.init_values(**default_values)
def init_values(self, *args, **kargs):
""" initial values for object """
keys = kargs.keys()
for i in self.fields.values():
name, ttype, relation = i['name'], i['ttype'], i['relation']
if ttype in ('one2many', 'many2many'): # these types use a list of objects
if name in keys:
self.__dict__[name] = List(Manager(relation, self._ooop), kargs[name], data=self, model=relation)
else:
self.__dict__[name] = List(Manager(relation, self._ooop), data=self, model=relation)
elif ttype == 'many2one':
if name in keys:
# manager, ref=None, model=None, copy=False
instance = Data(Manager(relation, self._ooop), kargs[name], relation)
self.INSTANCES['%s:%s' % (relation, kargs[name])] = instance
self.__dict__[name] = instance
else:
self.__dict__[name] = None
else:
if name in keys:
self.__dict__[name] = kargs[name]
else:
self.__dict__[name] = False # TODO: I prefer None here...
def get_values(self):
""" get values of fields with no relations """
if not self._data:
data = self._ooop.read(self._model, self._ref, self._fields)
if not data:
raise AttributeError('Object %s(%i) doesn\'t exist.' % (self._model, self._ref))
else:
self._data = data
for i in self.fields.values():
name, ttype, relation = i['name'], i['ttype'], i['relation']
if not ttype in ('one2many', 'many2one', 'many2many'):
hasattr(self,name) # use __getattr__ to trigger load
else:
pass # TODO: to load related fields as proxies to objects
def __print__(self, sort=True):
if sort:
fields = sorted(self.fields)
else:
fields = self.fields
for i in fields:
print "%s: %s" % (i, self.__getattr__(i))
def __setattr__(self, field, value):
if 'fields' in self.__dict__:
if field in self.fields.keys():
ttype = self.fields[field]['ttype']
if value:
if ttype =='many2one':
self.INSTANCES['%s:%s' % (self._model, value._ref)] = value
self.__dict__[field] = value
def __getattr__(self, field):
""" put values into object dynamically """
if field in self.__dict__.keys():
return self.__dict__[field]
try:
data = {field: self._data[field]}
except:
if field in self.fields.keys():
data = self._ooop.read(self._model, self._ref, [field])
# Try a custom function
if self._ooop.exe:
return lambda *a: self._ooop.execute(
self._model, field, [self._ref], *a)
try:
name = self.fields[field]['name']
except:
raise NameError('field \'%s\' is not defined' % field)
ttype = self.fields[field]['ttype']
relation = self.fields[field]['relation']
if ttype == 'many2one':
if data[name]: # TODO: review this
self.__dict__['__%s' % name] = data[name]
key = '%s:%i' % (relation, data[name][0])
if key in self.INSTANCES.keys():
self.__dict__[name] = self.INSTANCES[key]
else:
# TODO: use a Manager instance, not Data
instance = Data(Manager(relation, self._ooop),
data[name][0], relation, data=self)
self.__dict__[name] = instance
self.INSTANCES[key] = instance
else:
self.__dict__[name] = None # TODO: empty openerp data object
elif ttype in ('one2many', 'many2many'):
if data[name]:
self.__dict__['__%s' % name] = data[name]
self.__dict__[name] = List(Manager(relation, self._ooop),
data=self, model=relation)
for i in xrange(len(data[name])):
key = '%s:%i' % (relation, data[name][i])
if key in self.INSTANCES.keys():
self.__dict__[name].append(self.INSTANCES['%s:%s' % (relation, data[name][i])])
else:
# TODO: use a Manager instance, not Data
instance = Data(Manager(relation, self._ooop),
data[name][i], data=self,
model=relation)
self.__dict__[name].append(instance)
#self.INSTANCES['%s:%s' % (relation, data[name][i])] = instance
else:
self.__dict__[name] = List(Manager(relation, self._ooop),
data=self, model=relation)
elif ttype == "datetime" and data[name]:
self.__dict__[name] = datetime.strptime(data[name], "%Y-%m-%d %H:%M:%S")
elif ttype == "date" and data[name]:
self.__dict__[name] = date.fromordinal(datetime.strptime(data[name], "%Y-%m-%d").toordinal())
else:
# axelor conector workaround
if type(data) == types.ListType:
data = data[0]
self.__dict__[name] = data[name]
if self.__dict__[name]:
return self.__dict__[name]
def save(self):
""" save attributes object data into openerp
return: object id """
data = {}
for i in self.fields.values():
name, ttype, relation = i['name'], i['ttype'], i['relation']
if name in self.__dict__.keys(): # else keep values in original object
if not '2' in ttype: # not one2many, many2one nor many2many
if ttype == 'date' and self.__dict__[name]:
data[name] = date.strftime(self.__dict__[name], '%Y-%m-%d')
elif ttype == 'datetime' and self.__dict__[name]:
data[name] = datetime.strftime(self.__dict__[name], '%Y-%m-%d %H:%M:%S')
elif ttype in ('boolean', 'float', 'integer') or self.__dict__[name]:
data[name] = self.__dict__[name]
elif ttype in ('one2many', 'many2many'):
if len(self.__dict__[name]) > 0:
data[name] = [(6, 0, [i.save() for i in self.__dict__[name]])]
# update __name and INSTANCES (cache)
self.__dict__['__%s' % name] = [i._ref for i in self.__dict__[name]] # REVIEW: two loops?
for i in self.__dict__[name]:
self.INSTANCES['%s:%s' % (relation, i._ref)] = i
elif ttype == 'many2one':
if self.__dict__[name]:
data[name] = self.__dict__[name]._ref
# update __name and INSTANCES (cache)
self.__dict__['__%s' % name] = [self.__dict__[name]._ref, self.__dict__[name].name]
self.INSTANCES['%s:%s' % (relation, self.__dict__[name]._ref)] = self.__dict__[name]
if self._ooop.debug:
print ">>> model:", self._model
print ">>> data:", data
# create or write the object
if self._ref > 0 and not self._copy: # same object
self._ooop.write(self._model, self._ref, data)
else:
self._ref = self._ooop.create(self._model, data)
# update cache
self.INSTANCES['%s:%s' % (self._model, self._ref)] = self
return self._ref
def delete(self):
if self._ref > 0:
self._ooop.unlink(self._model, self._ref)
#else:
# pass # TODO
remove(self)
# TODO: to develop a more clever save function
def save_all(self):
""" save related instances """
for key, instance in self.INSTANCES.items():
if instance != self: # save self at the end
instance.save()
# updating INSTANCES
self.INSTANCES.pop(key)
self.INSTANCES['%s:%s' % (instance._model, instance._ref)] = instance
self.save()
@property
def id(self):
return self._ref
def __repr__(self):
""" default representation: <model:id> """
if self._ref < 0:
return u'<%s> new data instance' % self._model
try:
return u'<%s:%s %r> data instance' % (self._model, self._ref, self.name)
except:
return u'<%s:%s> data instance' % (self._model, self._ref)