-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
280 lines (212 loc) · 7.72 KB
/
Copy pathmodel.py
File metadata and controls
280 lines (212 loc) · 7.72 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
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
import uuid
import hashlib
import logging
import collections
import copy
import customdict
from google.appengine.ext import ndb
# generate hashed string
def getHashString(target):
salt = str(uuid.uuid4().get_hex()[0:6])
return hashlib.sha256(target + salt).hexdigest()
class JsonConvertableDateTimeProperty(ndb.DateTimeProperty):
def _get_for_dict(self, entity):
return self._get_value(entity).strftime('%Y-%m-%dT%H:%M:%S')
class User(ndb.Model):
name = ndb.StringProperty(default="Anonymous")
email = ndb.StringProperty()
user_id = ndb.StringProperty(required=True)
shared_apps = ndb.KeyProperty(kind="Application", repeated=True)
created = JsonConvertableDateTimeProperty(auto_now_add=True)
@classmethod
def create(cls, google_user, name=None):
new_user = cls()
new_user.user_id = google_user.user_id()
new_user.email = google_user.email();
if name != None:
new_user.name = name
else:
new_user.name = google_user.nickname()
new_user.put()
return new_user
@classmethod
def getByGoogleUser(cls, google_user):
user = cls.query(cls.user_id==google_user.user_id()).get()
if user != None and user.email != google_user.email():
user.email = google_user.email()
user.put()
return user
@classmethod
def getByEmail(cls, email):
return cls.query(cls.email==email).get()
@classmethod
def getByToken(cls, token_str):
token = AuthToken.getByToken(token_str)
if token is None:
return None
return token.getOwner()
def getToken(self):
current_token = AuthToken.getByUser(self)
if current_token != None:
return current_token
new_token = AuthToken.create(self)
return new_token
def getMyApps(self, limit=20):
return Application.getByUser(self, limit=limit)
def getSharedApps(self, limit=20):
return ndb.get_multi(self.shared_apps[0:limit])
class AuthToken(ndb.Model):
token = ndb.StringProperty(required=True)
enabled = ndb.BooleanProperty(default=True)
created = JsonConvertableDateTimeProperty(auto_now_add=True)
@classmethod
def create(cls, user):
new_token = cls(parent=user.key)
new_token.token = getHashString(str(uuid.uuid4()))
new_token.put()
return new_token
@classmethod
def getByUser(cls, user):
return cls.query(cls.enabled==True, ancestor=user.key).get()
@classmethod
def getByToken(cls, token_str):
return cls.query(cls.token==token_str, cls.enabled==True).get()
def getOwner(self):
return self.key.parent().get()
def disable(self):
self.enabled = False
self.put()
return self
def __str__(self):
return self.token
class Application(ndb.Model):
name = ndb.StringProperty()
kinds = ndb.StringProperty(repeated=True)
accessible_users = ndb.KeyProperty(kind="User", repeated=True)
access_key = ndb.StringProperty()
deleted = ndb.BooleanProperty(default=False)
def getAccessKey(self):
if self.access_key is None:
self.access_key = getHashString(str(uuid.uuid4()))
self.put()
return self.access_key
def allowAccess(self, user):
if self.accessible_users == None:
self.accessible_users = []
if user.key not in self.accessible_users:
self.accessible_users.append(user.key)
self.put()
if user.shared_apps == None:
user.shared_apps = []
if self.key not in user.shared_apps:
user.shared_apps.append(self.key)
user.put()
return self
def revokeAccess(self, user):
self.accessible_users.remove(user.key)
self.put()
user.shared_apps.remove(self.key)
user.put()
def addData(self, kind, value, timestamp):
if kind not in self.kinds:
self.kinds.append(kind)
self.put()
return GraphData.create(self, kind, value, timestamp)
def getOwner(self):
return self.key.parent().get()
def getInfo(self):
info = {"key": self.key.urlsafe(),
"owner": self.getOwner().key.urlsafe(),
"name": self.name,
"kinds": self.kinds}
return info
def getData(self, kind, limit=60):
return GraphData.get(self, kind, limit)
def getFormattedData(self, requestedKind=None, limit=60):
if requestedKind == None:
requestedKind = self.kinds
results = {};
results["cols"] = [{"id": "timestamp", "label": "Timestamp", "type": "datetime"}]
blank_row = [];
dataSet = collections.OrderedDict()
for kind in self.kinds:
if kind not in requestedKind:
continue
results["cols"].append({"id": kind, "label": kind, "type": "number"})
blank_row.append({"v": None})
dataSet[kind] = [p.to_dict() for p in self.getData(kind, limit)]
def row_factory(key):
r = copy.deepcopy(blank_row)
r.insert(0, {"v": key})
logging.info("New row: %s " % r)
return r
rows = customdict.CustomOrderedDefaultdict(row_factory)
col_position = 0
for kind, values in dataSet.items():
col_position += 1
for v in values:
rows[v["timestamp"]][col_position]["v"] = v["value"]
results["rows"] = [{"c": value} for key, value in rows.items()]
return results
def isOwner(self, user):
return (self.key.parent() == user.key)
def isAccessible(self, user):
return self.isOwner(user) or (user.key in self.accessible_users)
def delete(self):
# Delete data later
self.deleted = True
self.put()
return self
def deleteData(self):
logging.info("deleteData")
logging.info(self)
GraphData.deleteAll(self)
return
def getAccessibleUsers(self, limit=20):
return ndb.get_multi(self.accessible_users[0:limit])
@classmethod
def create(cls, user, name):
existing = cls.getByName(user, name)
if existing != None:
return existing
app = cls(parent=user.key, name=name)
app.put()
return app
@classmethod
def getByUser(cls, user, deleted=False, limit=20):
logging.info(deleted)
return cls.query(cls.deleted==deleted, ancestor=user.key).fetch(limit=limit)
@classmethod
def getByName(cls, user, name, deleted=False):
return cls.query(cls.name==name, cls.deleted==deleted, ancestor=user.key).get()
@classmethod
def getByAccessKey(cls, access_key, deleted=False):
return cls.query(cls.access_key==access_key, cls.deleted==deleted).get()
@classmethod
def getDeletedApps(cls):
return cls.query(cls.deleted==True).fetch()
class GraphData(ndb.Model):
kind = ndb.StringProperty()
value = ndb.FloatProperty()
timestamp = JsonConvertableDateTimeProperty()
@classmethod
def create(cls, app, kind, value, timestamp):
data = cls(parent=app.key)
data.kind = kind
data.value = value
data.timestamp = timestamp
data.put()
return data
@classmethod
def get(cls, app, kind, limit=60):
return cls.query(cls.kind==kind, ancestor=app.key).order(-cls.timestamp).fetch(limit=limit)
@classmethod
def deleteAll(cls, app):
logging.info("deleteAll")
while True:
data = cls.query(ancestor=app.key).fetch(limit=1000, keys_only=True)
if len(data) <= 0:
break
logging.info(data)
ndb.delete_multi(data)
return