/
app.py
executable file
·316 lines (258 loc) · 9.43 KB
/
app.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python
# encoding: utf-8
"""
This wonderfull app permits to display boardgame scores and stats
"""
from __future__ import print_function
import argparse
import json
import os
import sys
import flask
import flask_login
from flask import Flask, jsonify, render_template, request
import scores.scores as scores
import scores.database as database
import scores.stats.stats_overall as s_overall
from mongokit.document import StructureError
THIS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__)))
class Config(object):
"""A configuration class"""
DEBUG = False
TESTING = False
FILE_URI = os.path.join(THIS_DIR, 'scores.yml')
# ex: mongodb://<dbuser>:<dbpassword>@ds019664.mlab.com:19664/zesk06
DATABASE_URI = 'mongodb://<dbuser>:<dbpassword>@<instance>.mlab.com:<port>/<dbname>'
if 'DATABASE_URI' in os.environ:
DATABASE_URI = os.environ['DATABASE_URI']
class DevelopmentConfig(Config):
"""The dev configuration"""
DEBUG = True
class TestConfig(Config):
"""The test config"""
TESTING = True
FILE_URI = os.path.join(THIS_DIR, 'target/scores.yml')
DATABASE_URI = 'mongodb://<dbuser>:<dbpassword>@<instance>.mlab.com:<port>/<dbname>_test'
if 'TEST_DATABASE_URI' in os.environ:
DATABASE_URI = os.environ['TEST_DATABASE_URI']
app = Flask(__name__)
app.config.from_object(Config)
# used by flask-login
app.secret_key = 'idontwanttobeinthepubliccode'
login_manager = flask_login.LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
app.database = None
def get_db():
"""Return the Database
:rtype: scores.database.Database"""
if app.database is None:
print('connecting to %s' % app.config['DATABASE_URI'])
app.database = database.Database(app.config['DATABASE_URI'])
return app.database
@login_manager.user_loader
def user_loader(login_s):
# retrieve user from database
print('user_loader')
db = get_db()
user = db.get_user(login_s)
return user
@login_manager.request_loader
def request_loader(mrequest):
print('request loader')
mlogin = mrequest.form.get('username')
passwd = mrequest.form.get('pw')
if mlogin and passwd:
db = get_db()
user = db.get_user(mlogin)
if user:
if not user.authenticate(passwd):
print('wrong password for %s' % mlogin)
else:
print('user %s not found' % mlogin)
return user
@app.route('/login', methods=['GET', 'POST'])
def login():
if flask.request.method == 'GET':
return '''
<form action='login' method='POST'>
<input type='text' name='username' id='username' placeholder='username'></input>
<input type='password' name='pw' id='pw' placeholder='password'></input>
<input type='submit' name='submit'></input>
</form>
'''
login = request.form.get('username')
password = request.form['pw']
db = get_db()
user = db.get_user(login)
if user and user.authenticate(password):
flask_login.login_user(user)
flask.flash('Logged in successfully.')
# what is the next page?
next_url = flask.request.args.get('next')
if not next_is_valid(next_url, authenticated=True):
return flask.abort(400)
return flask.redirect(next_url or flask.url_for('index'))
return 'Bad login'
def next_is_valid(url, authenticated):
"""Return True if next page is valid
"""
protected_urls = ('protected', )
if url in protected_urls and not authenticated:
return False
return True
@app.route('/logout')
def logout():
"""To logout from the web app"""
flask_login.logout_user()
flask.flash('Logged out successfully.')
# what is the next page?
next_url = flask.request.args.get('next')
if not next_is_valid(next_url, False):
return flask.abort(400)
return flask.redirect(next_url or flask.url_for('index'))
@app.route('/protected')
@flask_login.login_required
def protected():
return 'Logged in as: ' + flask_login.current_user.id
@app.route('/')
def index():
print('index')
mscores = get_mscores()
stats = s_overall.OverallWinnerStat()
stats.parse(mscores)
print('rendering')
return render_template('base.html', title=u'GAME STATS', stats=stats)
def get_mscores():
"returns the mscores"
return scores.Scores(database=get_db())
@app.route('/api/v1/is_logged', methods=["GET"])
def is_logged():
"""
:return: {"logged": "true"} if user is logged
"""
return jsonify({"logged": True, "user_id": flask_login.current_user.get_id()})
@app.route('/api/v1/plays', methods=["POST"])
@flask_login.login_required
def add_play():
"""Adds a new play"""
if 'Logged in as: ' + flask_login.current_user.get_id():
play_json = request.json
if request.json is None and request.data:
play_json = request.data
if play_json:
try:
# insert the created by information in play
print('play_json is %s' % type(play_json))
play_obj = play_json
if isinstance(play_json, basestring):
play_obj = json.loads(play_json)
play_obj['created_by'] = '%s' % flask_login.current_user.get_id()
play_json = json.dumps(play_obj)
#
new_play = get_db().add_play_from_json(play_json)
return jsonify(msg='added play %s' % new_play.id,
id=new_play.id,
data=new_play.to_json()), 201
except StructureError, error:
return jsonify('BAD JSON %s: %s' % (error, play_json)), 400
else:
return jsonify('Failed to find JSON data in your POST'), 404
return jsonify('You must be logged in to add a play'), 401
@app.route('/api/v1/plays', methods=["GET"])
def get_plays():
"""
: return: the plays
"""
mscores = get_mscores()
return jsonify([json.loads(play.to_json()) for play in mscores.plays])
@app.route('/api/v1/plays/<play_id>', methods=["GET"])
def get_play(play_id):
"""
: return: the play with given ID
"""
mscores = get_mscores()
play = mscores.get_play(play_id)
if play:
return jsonify(json.loads(play.to_json()))
return jsonify('Failed to find play with id %s' % play_id), 404
@app.route('/api/v1/plays/<play_id>/elos', methods=["GET"])
def get_play_elos(play_id):
"""
: return: the elos of the play with given ID
"""
stats = s_overall.OverallWinnerStat()
stats.parse(get_mscores())
elos_per_player = stats.elo_stats.get_elos_per_player(play_id)
if elos_per_player:
json_object = []
for player_login, elos in elos_per_player.iteritems():
json_object.append({
"login": player_login,
"elo_before": elos[0],
"elo_after": elos[1],
"elos": elos[1] - elos[0],
})
return jsonify(json_object)
return jsonify('Failed to find elos of play with id %s' % play_id), 404
@app.route('/api/v1/plays/<play_id>', methods=["DELETE"])
@flask_login.login_required
def delete_play(play_id):
"""
Delete the play with the given id
:param play_id: The id of the play to delete
"""
mscores = get_mscores()
play = mscores.get_play(play_id)
if play:
if play.created_by == flask_login.current_user.get_id():
play.delete()
return jsonify(msg='play deleted id %s' % play_id, data=play.to_json()), 200
else:
msg = 'You can\'t delete play you did not create'
return jsonify(msg=msg, data=play.to_json()), 403
return jsonify('Failed to find play with id %s' % play_id), 404
@app.route('/api/v1/games', methods=["GET"])
def get_games():
"""Return the list of games"""
stats = s_overall.OverallWinnerStat()
stats.parse(get_mscores())
return jsonify(sorted(list(stats.game_stats)))
@app.route('/api/v1/games/<string:game_id>', methods=["GET"])
def get_game(game_id):
"""
: return: the game with given ID
"""
stats = s_overall.OverallWinnerStat()
stats.parse(get_mscores())
if game_id in stats.game_stats:
return jsonify(stats.game_stats[game_id].to_json())
return jsonify('Failed to find game %s' % game_id), 404
@app.route('/api/v1/players', methods=["GET"])
def get_players():
"""
: return: Get all player stats
"""
stats = s_overall.OverallWinnerStat()
stats.parse(get_mscores())
return jsonify([player_stat.to_json()
for player_stat in stats.player_stats.values()])
@app.route('/api/v1/players/<string:player_id>', methods=["GET"])
def get_player(player_id):
"""
: return: the player with given ID
"""
stats = s_overall.OverallWinnerStat()
stats.parse(get_mscores())
if player_id in stats.player_stats:
return jsonify(stats.player_stats[player_id].to_json())
return jsonify('Failed to find player with id %s' % player_id), 404
if __name__ == '__main__':
parser = argparse.ArgumentParser('app.py')
parser.add_argument(
'-d', '-r', help='goes in flask debug mode', action="store_true")
parser.add_argument('port', help='The port, 5000 by default', nargs='?',
type=int, default=5000)
args = parser.parse_args()
print('launching server with args [%s]' % ', '.join(sys.argv))
app.run(debug=args.d, port=args.port)