-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
90 lines (66 loc) · 2.22 KB
/
classes.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
import json as JSON
class BaseObject(object):
def __init__(self,json ,session):
self.parseJSON(json,session)
def parseJSON(self,json,session ):
#session = db.getSession()
myAttributes = [a for a in dir(self) if not a.startswith('__') and not callable(getattr(self,a)) and not a.startswith('_') and not a == ('id')]
for key in json :
value = json[key]
#check if value is a dict
if type(value) is dict:
# value is a complex object
valueClass = value['class']
if value['reference'] == 'False':
# we create new objects to connect to self.key
valueClass = value['class']
valueList = []
# value is iterable
for jsonObj in value['objects']:
valueList.append(eval(valueClass)(jsonObj,session))
setattr(self, key,valueList)
elif (value['reference'] == 'True'):
# we query for existing objects to connect to self.key
filterStr = value['filter']
#giantBats = session.query(cls.Monster).filter(cls.Monster.race.like("Giant%")).all()
queryObjects = eval('session.query('+valueClass+').filter('+ filterStr+ ').all()')
for obj in queryObjects:
eval('self.'+key).append(obj)
else :
pass #TBD: raise error - reference value ...
else:
# value is a simple field value
# TBD: if needed - raise no attribute error ...
setattr(self, key,value)
def __str__(self):
myAttributes = [a for a in dir(self) if not a.startswith('__') and not callable(getattr(self,a)) and not a.startswith('_') and not a == ('id')]
string = self.__class__.__name__ + ':'
for key in myAttributes :
myAtribute = getattr(self, key)
string = string + '\n'+ key+':'+str(myAtribute)
return string
def __repr__(self):
return self.__str__()
class User(BaseObject):
pass
class Bid(BaseObject):
pass
class Contribution(object):
pass
class Organization(BaseObject):
pass
class UserOrganization(BaseObject):
pass
class ContributionContributor(object):
pass
class Milestone(object):
pass
class MilestoneBid(BaseObject):
pass
class MilestoneContributor(object):
pass
class MilestoneContribution(object):
pass
class ContributionValue(object):
pass
# checkout serlizing : http://www.diveintopython3.net/serializing.html