-
Notifications
You must be signed in to change notification settings - Fork 1
/
apis.py
194 lines (157 loc) · 5.52 KB
/
apis.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
from google.appengine.api import channel
from google.appengine.api import users
from google.appengine.ext import ndb
import data
import datetime
import json
import webapp2
class GetUser(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
result = data.User.query(data.User.user == user).fetch()
if len(result) == 0:
response = {'result': 'not_found' }
self.response.write(json.dumps(response))
return
currentUser = result[0]
response = {
'result': 'ok',
'user_id': currentUser.user.user_id(),
'name': currentUser.name,
'avatar': currentUser.avatar,
'codes': currentUser.codes }
self.response.write(json.dumps(response));
class SetUser(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
result = data.User.query(data.User.user == user).fetch()
currentUser = None
if len(result) > 0:
currentUser = result[0]
if currentUser is None:
currentUser = data.User()
currentUser.user = user
currentUser.name = self.request.get('name')
currentUser.avatar = self.request.get('avatar')
key = currentUser.put()
response = {'result': 'ok', 'key': key.id() }
self.response.write(json.dumps(response))
class SaveCodes(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
result = data.User.query(data.User.user == user).fetch()
currentUser = None
if len(result) > 0:
currentUser = result[0]
if currentUser is None:
currentUser = data.User()
currentUser.user = user
currentUser.codes = self.request.get_all('codes[]')
key = currentUser.put()
response = {'result': 'ok', 'key': key.id() }
self.response.write(json.dumps(response))
class SaveMap(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
result = data.Map.query(data.Map.owner == user).fetch()
blocks = None
if len(result) > 0:
blocks = result[0]
if blocks is None:
blocks = data.Map()
blocks.created = datetime.datetime.now()
blocks.owner = user
blocks.data = self.request.get('data')
blocks.updated = datetime.datetime.now()
key = blocks.put()
response = {'result': 'ok', 'key': key.id() }
self.response.write(json.dumps(response))
class GetMaps(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
maps = data.Map.query(data.Map.owner == user).fetch()
maps = maps if maps is not None else []
maps = [m.serialize() for m in maps]
response = {
'result': 'ok',
'maps': maps
}
self.response.write(json.dumps(response));
class LoadMapById(webapp2.RequestHandler):
def post(self):
key = self.request.get('key')
result = data.Map.get_by_id(long(key))
if result is None:
self.error(404)
self.response.out.write('not_found.')
return
response = {
'result': 'ok',
'key': result.key.id(),
'data': result.data
}
self.response.write(json.dumps(response));
class SetMapName(webapp2.RequestHandler):
def post(self):
key = self.request.get('key')
result = data.Map.get_by_id(long(key))
if result is None:
self.error(404)
self.response.out.write('not_found.')
return
result.name = self.request.get('name')
key = result.put();
response = {
'result': 'ok',
'key': result.key.id()
}
self.response.write(json.dumps(response));
class CreateMap(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
name = self.request.get('name')
blocks = data.Map()
blocks.name = name;
blocks.created = datetime.datetime.now()
blocks.updated= datetime.datetime.now()
blocks.owner = user
blocks.put()
response = {
'result': 'ok',
'map': blocks.serialize()
}
self.response.write(json.dumps(response));
class DeleteMap(webapp2.RequestHandler):
def post(self):
key = self.request.get('key')
result = data.Map.get_by_id(long(key))
if result is None:
self.error(404)
self.response.out.write('not_found.')
return
ndb.Key(data.Map, long(key)).delete()
response = {
'result': 'ok',
}
self.response.write(json.dumps(response));
class SendMessage(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
token = self.request.get('token')
data = {
'message': self.request.get('message'),
'user_id': user.user_id()
}
channel.send_message(token, json.dumps(data))
application = webapp2.WSGIApplication([
('/_/get_user/', GetUser),
('/_/set_user/', SetUser),
('/_/save_codes/', SaveCodes),
('/_/save_map/', SaveMap),
('/_/get_maps/', GetMaps),
('/_/load_map_by_id/', LoadMapById),
('/_/set_map_name/', SetMapName),
('/_/create_map/', CreateMap),
('/_/delete_map/', DeleteMap),
('/_/send_message/', SendMessage),
], debug=True)