-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
178 lines (154 loc) · 5.72 KB
/
data.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
# -*- coding: utf-8 -*-
from google.appengine.ext import ndb
import logging, random, json
API_KEY_MIN=10000
API_KEY_MAX=99999
class Booking(ndb.Model):
user_id = ndb.StringProperty()
start = ndb.DateTimeProperty()
end = ndb.DateTimeProperty()
hours = ndb.TimeProperty()
pause = ndb.TimeProperty()
name = ndb.StringProperty()
class ApiKey(ndb.Model):
'''
Each user has one API-Key.
'''
user_id = ndb.StringProperty()
key = ndb.IntegerProperty()
@classmethod
def getApiKey(cls, user_id):
'''
Return the API-Key for this user - if nothing stored -> generate it.
:param user_id:
:return: the api-key-id
'''
keys = cls.query(ApiKey.user_id == str(user_id)).fetch()
if len(keys) == 0:
#read all keys and choose one unused
usedKeys = set([x.key for x in cls.query().fetch()])
newApiKey = ApiKey(user_id = str(user_id),
key = random.choice(filter(lambda x:x not in usedKeys,
xrange(API_KEY_MIN,API_KEY_MAX))))
newApiKey.put()
return newApiKey.key
else:
return keys[0].key
#getApiKey
@classmethod
def getOwnerId(cls, api_key):
'''
Return the userId for the api_key
:param api_key: the API-KEY id
:return: the user_id
'''
keys = cls.query(ApiKey.key == api_key).fetch(1)
if len(keys)==1:
return keys[0].user_id
#getOwner
#ApiKey
class Location(ndb.Model):
'''Registrierte Orte
'''
user_id = ndb.StringProperty()
name = ndb.StringProperty()
pos = ndb.GeoPtProperty()
radius = ndb.FloatProperty()
home = ndb.BooleanProperty()
work = ndb.BooleanProperty()
@classmethod
def getAllLocations(cls, user_id):
'''
Liefert alle registrierte Orts zurück.
:param user_id:
:return:
'''
locations = cls.query(Location.user_id == str(user_id)).fetch()
return locations
#getAllLocations
def to_dict(self):
'''Overwrite the serialization to not expose the user_id (and some formatting).'''
ret = { "name": self.name.encode('ascii', 'xmlcharrefreplace'),
"pos": { "long": self.pos.lon, "lat": self.pos.lat},
"radius": self.radius,
"home": self.home and 1 or 0, #python 'True' is not JSON 'true'
"work": self.work and 1 or 0
}
return ret
#to_dict
#Location
class MyDevice(ndb.Model):
'''
List of all devices with block-flag and readable name.
'''
user_id = ndb.StringProperty()
device_id = ndb.StringProperty()
name = ndb.StringProperty()
blocked = ndb.BooleanProperty()
@classmethod
def getAllDevices(cls, user_id, deviceIdFromList=set()):
devices = cls.query(MyDevice.user_id == str(user_id)).fetch()
used_devices = Geofency.query(projection=['device'], distinct=True).fetch()
other_devices = list(set([dev.device for dev in used_devices]) -
set([dev.device_id for dev in devices]))
logging.info(other_devices)
#we have no saved devices and only one deviceId from list
if len(devices)==0 and len(other_devices)==1:
#store this one device_id as default smartphone
defaultDevice = MyDevice(user_id=user_id,
device_id=other_devices[0],
name='SmartPhone',
blocked=False)
defaultDevice.put()
devices = [defaultDevice]
else:
devices.extend([MyDevice(name="", device_id=x) for x in other_devices])
return devices
#getAllDevices
#MyDevices
class Geofency(ndb.Model):
user_id = ndb.StringProperty()
name = ndb.StringProperty()
entry = ndb.IntegerProperty()
event = ndb.DateTimeProperty()
pos = ndb.GeoPtProperty()
device = ndb.StringProperty()
def to_dict(self):
'''Overwrite the serialization to not expose the user_id.'''
ret = { "name": self.name.encode('ascii', 'xmlcharrefreplace'),
"entry": self.entry,
"event": self.event.isoformat(),
"pos": { "long": self.pos.lon, "lat": self.pos.lat},
"device": self.device.encode('ascii', 'xmlcharrefreplace')
}
return ret
#to_dict
@classmethod
def getSortedByTime(cls, user_id, max=100):
all_geo = cls.query(Geofency.user_id == str(user_id)).order(cls.event).fetch(max)
device_dict = dict([dev.device_id, dev.name] for dev in MyDevice.getAllDevices(user_id))
for geo in all_geo:
#replace all known geo.device entires with MyDevice.name
geo.device = device_dict.get(geo.device, geo.device)
return all_geo
#getSortedByTime
@classmethod
def deleteByDeviceId(cls, user_id, device_id):
qo = ndb.QueryOptions(keys_only = True)
query = cls.query(Geofency.user_id == str(user_id), Geofency.device == device_id)
keys = query.fetch(100, options = qo)
logging.info('Found %d items to delete' % len(keys))
logging.info(keys)
ndb.delete_multi(keys)
@classmethod
def getAllDevices(cls, user_id, max=100):
return cls.query(Geofency.user_id == str(user_id), projection=['device']).fetch(max)
#getAllDevices
@classmethod
def getSortedByLocation(cls, user_id):
result = []
for log in cls.query(Geofency.user_id == str(user_id)).order(cls.event):
result.append(log)
return result
class TimeProject(ndb.Model):
name = ndb.StringProperty()