This repository was archived by the owner on Sep 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrecordBone.py
More file actions
231 lines (169 loc) · 5.22 KB
/
Copy pathrecordBone.py
File metadata and controls
231 lines (169 loc) · 5.22 KB
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
# -*- coding: utf-8 -*-
from server.bones.bone import baseBone, getSystemInitialized
from server.errors import ReadFromClientError
import extjson
class recordBone(baseBone):
type = "record"
def __init__(self, using, format=None, indexed=False, multiple=True, *args, **kwargs):
super(recordBone, self).__init__(indexed=indexed, multiple=multiple, *args, **kwargs)
self.using = using
self.format = format
if not format or indexed or not multiple:
NotImplemented("A recordBone must not be indexed, must be multiple and must have a format set")
if getSystemInitialized():
self._usingSkelCache = using()
else:
self._usingSkelCache = None
def setSystemInitialized(self):
super(recordBone, self).setSystemInitialized()
self._usingSkelCache = self.using()
def _restoreValueFromDatastore(self, val):
"""
Restores one of our values from the serialized data read from the datastore
:param value: Json-Encoded datastore property
:return: Our Value (with restored usingSkel data)
"""
value = extjson.loads(val)
assert isinstance(value, dict), "Read something from the datastore thats not a dict: %s" % str(type(value))
usingSkel = self._usingSkelCache
usingSkel.setValuesCache({})
usingSkel.unserialize(value)
return usingSkel.getValuesCache()
def unserialize(self, valuesCache, name, expando):
if name not in expando:
valuesCache[name] = None
return True
val = expando[name]
if self.multiple:
valuesCache[name] = []
if not val:
return True
if isinstance(val, list):
for res in val:
try:
valuesCache[name].append(self._restoreValueFromDatastore(res))
except:
raise
else:
try:
valuesCache[name].append(self._restoreValueFromDatastore(val))
except:
raise
return True
def serialize(self, valuesCache, name, entity):
if not valuesCache[name]:
entity.set(name, None, False)
else:
usingSkel = self._usingSkelCache
res = []
for val in valuesCache[name]:
usingSkel.setValuesCache(val)
res.append(extjson.dumps(usingSkel.serialize()))
entity.set(name, res, False)
return entity
def fromClient(self, valuesCache, name, data):
valuesCache[name] = []
tmpRes = {}
clientPrefix = "%s." % name
for k, v in data.items():
#print(k, v)
if k.startswith(clientPrefix) or k == name:
if k == name:
k = k.replace(name, "", 1)
else:
k = k.replace(clientPrefix, "", 1)
if "." in k:
try:
idx, bname = k.split(".", 1)
idx = int(idx)
except ValueError:
idx = 0
try:
bname = k.split(".", 1)
except ValueError:
# We got some garbage as input; don't try to parse it
continue
else:
idx = 0
bname = k
if not bname:
continue
if not idx in tmpRes:
tmpRes[idx] = {}
if bname in tmpRes[idx]:
if isinstance(tmpRes[idx][bname], list):
tmpRes[idx][bname].append(v)
else:
tmpRes[idx][bname] = [tmpRes[idx][bname], v]
else:
tmpRes[idx][bname] = v
tmpList = [tmpRes[k] for k in sorted(tmpRes.keys())]
errorDict = {}
forceFail = False
for i, r in enumerate(tmpList[:]):
usingSkel = self._usingSkelCache
usingSkel.setValuesCache({})
if not usingSkel.fromClient(r):
for k, v in usingSkel.errors.items():
errorDict["%s.%d.%s" % (name, i, k)] = v
forceFail = True
tmpList[i] = usingSkel.getValuesCache()
cleanList = []
for item in tmpList:
err = self.isInvalid(item)
if err:
errorDict["%s.%s" % (name, tmpList.index(item))] = err
else:
cleanList.append(item)
valuesCache[name] = tmpList
if not cleanList:
if not (self.required or errorDict):
# Returning a error will only cause a warning if we are not required
return "No value selected!"
errorDict[name] = "No value selected"
if len(errorDict.keys()):
return ReadFromClientError(errorDict, forceFail)
return None
def getSearchTags(self, values, key):
def getValues(res, skel, valuesCache):
for k, bone in skel.items():
if bone.searchable:
for tag in bone.getSearchTags(valuesCache, k):
if tag not in res:
res.append(tag)
return res
value = values.get(key)
res = []
if not value:
return res
for val in value:
res = getValues(res, self._usingSkelCache, val)
return res
def getSearchDocumentFields(self, valuesCache, name, prefix=""):
def getValues(res, skel, valuesCache, searchPrefix):
for key, bone in skel.items():
if bone.searchable:
res.extend(bone.getSearchDocumentFields(valuesCache, key, prefix=searchPrefix))
value = valuesCache.get(name)
res = []
if not value:
return res
for idx, val in enumerate(value):
getValues(res, self._usingSkelCache, val, "%s%s_%s" % (prefix, name, str(idx)))
return res
def getReferencedBlobs(self, valuesCache, name):
def blobsFromSkel(skel, valuesCache):
blobList = set()
for key, _bone in skel.items():
blobList.update(_bone.getReferencedBlobs(valuesCache, key))
return blobList
res = set()
value = valuesCache.get(name)
if not value:
return res
if isinstance(value, list):
for val in value:
res.update(blobsFromSkel(self._usingSkelCache, val))
elif isinstance(value, dict):
res.update(blobsFromSkel(self._usingSkelCache, value))
return res