-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
226 lines (185 loc) · 6.65 KB
/
handlers.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
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
'''
7D2D map markers Tornado Server
by: Adam Dybczak (RaTilicus)
GET /markers
List owned and public markers
POST /markers
Create a new marker
DELETE /markers/<id>
Delete an existing marker
PUT /markers/<id>
Update an existing marker
'''
import tornado.web
from bson import ObjectId
from bson.json_util import loads as json_decode, dumps as json_encode
from schemaforms import SchemaForm
from tornado import gen
class BaseHandler(tornado.web.RequestHandler):
@gen.coroutine
def prepare(self):
''' handler init
- set self.db var
- set self.curent_user (if logged in)
- set self.POST from request body, decode json if request is json
'''
self.db = self.settings['db']
user_id = self.get_secure_cookie("user")
if user_id:
try:
self.current_user = yield self.db.players.find_one({'_id': int(user_id)})
except:
print 'login_failed'
pass
if not self.current_user:
self.clear_cookie("user")
if self.request.method in ['POST', 'PUT'] and self.request.body:
try:
self.POST = json_decode(self.request.body)
except Exception, e:
self.POST = self.request.arguments
@gen.coroutine
def render(self, template, **data):
''' render template override
adds extra common template variables
'''
data['t'] = self.settings['t']
data['user'] = self.current_user
if not 'username' in data:
data['username'] = self.get_argument('username', '')
super(BaseHandler, self).render(template, **data)
@gen.coroutine
def json_response(self, data, success=False, errors=[]):
''' json response helper '''
self.write(json_encode({
'data': data,
'success': success,
'errors': errors
}))
self.finish()
class IndexHandler(BaseHandler):
@gen.coroutine
def get(self):
''' home/index page '''
data=dict(
login_errors='login_errors' in self.request.arguments,
)
print 'show login form'
self.set_header('Cache-Control', 'no-cache, must-revalidate')
self.set_header('Pragma', 'no-cache')
self.set_header('Expires', 'Sun, 11 Aug 2013 11:00:00 GMT')
self.render('templates/index.html', **data)
class RecipesHandler(BaseHandler):
@gen.coroutine
def get(self):
''' recipes page '''
data=dict()
print 'show recipes page', data
self.render('templates/recipes.html', **data)
class AboutHandler(BaseHandler):
@gen.coroutine
def get(self):
''' about page '''
data=dict()
print 'show recipes page', data
self.render('templates/about.html', **data)
class LoginHandler(BaseHandler):
class LoginForm(SchemaForm):
schema = {
'type': 'object',
'properties': {
'username': {'type': 'string', 'minLength': 1},
'password': {'type': 'string', 'minLength': 4},
},
'required': ['username', 'password'],
}
flatten = ['username', 'password']
@gen.coroutine
def post(self):
'login request'
form = self.LoginForm(self.POST)
user = None
if form.is_valid():
user = yield self.db.players.find_one({'username': form.cleaned_data['username']})
if user:
if user['password'] == form.cleaned_data['password']:
self.set_secure_cookie("user", str(user['_id']))
self.redirect("/")
else:
user = None
else:
print form.errors
if not user:
self.redirect(u"/?login_errors=1&username=%s" % form.data.get('username', ''))
class LogoutHandler(BaseHandler):
def get(self):
self.clear_cookie("user")
self.redirect("/")
class MarkerHandler(BaseHandler):
class MarkerForm(SchemaForm):
schema = {
'type': 'object',
'properties': {
'desc': {'type': 'string', 'minLength': 1},
'x': {'type': 'number'},
'y': {'type': 'number', 'default': 0.0},
'z': {'type': 'number'},
'public': {'type': 'boolean', 'default': True}
},
'required': ['desc', 'x', 'z']
}
@gen.coroutine
def get(self):
'get all markers'
markers = []
fields = ['id', 'desc', 'x', 'y', 'z', 'public', 'ts', 'uid', 'username']
if self.current_user:
# get user owned markers, and other public markers
query = {'$or': [
{'username': self.current_user['username']},
{'username': {'$ne': self.current_user['username']}, 'public': True}
]}
else:
# get public markers
query = {'public': True}
cursor = self.db.markers.find(query, fields)
while (yield cursor.fetch_next):
marker = cursor.next_object()
marker['id'] = str(marker.pop('_id'))
marker['o'] = self.current_user and (marker['uid']==self.current_user['_id']) or False
markers.append(marker)
self.json_response(data=markers, success=True)
@gen.coroutine
def post(self):
'create a new marker'
form = self.MarkerForm(self.POST)
if form.is_valid():
data = form.cleaned_data
data['_id'] = ObjectId()
if self.current_user:
# create user owned marker
data['uid'] = self.current_user['_id']
data['username'] = self.current_user['username']
else:
# create anon marker
data['uid'] = 0
data['public'] = True # Anon markers have to be public
self.db.markers.insert(data)
self.json_response(data={'_id': str(data['_id'])}, success=True)
else:
print 'form errors', form.errors
self.json_response(data={}, success=False, errors=form.errors)
@gen.coroutine
def delete(self, id):
'remove a marker'
query = {
'_id': ObjectId(id),
# if user is logged in, the user can remove owned and anon markers
# anon users can only remove anon markers
'uid': ({'$in': [0, self.current_user['_id']]} if self.current_user else 0)
}
self.db.markers.remove(query)
self.json_response(data={}, success=True)
#@gen.coroutine
#def put(self, id):
# 'update a marker'