-
Notifications
You must be signed in to change notification settings - Fork 1
/
importscores.py
79 lines (65 loc) · 2.03 KB
/
importscores.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
import logging
import urllib
from google.appengine.api import urlfetch
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import login_required
import qserv
def setup(root):
"""Returns the handlers for the score importation. root must have a
trailing slash."""
_BaseHandler.root = root
return [(root, FormHandler),
(root + 'submit', ImportHandler),
(root + 'status', StatusHandler)]
class _BaseHandler(webapp.RequestHandler):
pass
class FormHandler(_BaseHandler):
@login_required
def get(self):
if not users.is_current_user_admin():
self.redirect('status?msg=notadmin')
return
self.response.out.write("""<html>
<body>
<form action="submit" method="post" enctype="multipart/form-data">
<p>Import from: <input name="highscores" type="file"></p>
<p><input value="Upload highscores" type="submit"></p>
</form>
</body>
</html>""")
class ImportHandler(_BaseHandler):
def post(self):
if not users.is_current_user_admin():
self.redirect('status?msg=notadmin')
return
highscores_data = self.request.get('highscores', '')
if highscores_data == '':
self.redirect('status?msg=missingdata')
return
lines = highscores_data.splitlines()
if lines and lines[0] == 'Ok':
lines.pop(0)
params = {}
for line in lines:
(key, value) = line.split(None, 1)
qserv.set_param(params, key, value)
for score in params.itervalues():
qserv.put_score(score)
self.redirect('status?msg=success')
class StatusHandler(_BaseHandler):
@login_required
def get(self):
msg = self.request.get('msg', 'unknown')
if not users.is_current_user_admin():
msg = 'notadmin'
msgstrs = {'unknown': 'status unknown',
'notadmin': 'you do not have sufficient rights',
'missingdata': 'highscore data is missing',
'success': 'success'}
self.response.out.write("""<html>
<body>
<p>%s</p>
<p><a href="%s">logout</a></p>
</body>
</html>""" % (msgstrs[msg], users.create_logout_url(self.root)))