support dumping games to CSV
Haldean Brown
3 years ago
0 | 0 |
import flask
|
|
1 |
import io
|
1 | 2 |
import redis
|
2 | 3 |
|
3 | 4 |
import globals
|
|
60 | 61 |
flask.flash("User \"{}\" added".format(username))
|
61 | 62 |
return flask.redirect(flask.url_for("adduser"))
|
62 | 63 |
|
|
64 |
@app.route("/allgames/csv")
|
|
65 |
def allgames_csv():
|
|
66 |
user = users.session_user()
|
|
67 |
if user is None:
|
|
68 |
return flask.redirect(flask.url_for("index"))
|
|
69 |
csvdat = io.StringIO()
|
|
70 |
data.dump_games_to_csv(user, csvdat)
|
|
71 |
csvdat.seek(0)
|
|
72 |
return csvdat.read(), 200, {"Content-type": "text/plain"}
|
|
73 |
|
63 | 74 |
@app.route("/logout")
|
64 | 75 |
def logout():
|
65 | 76 |
users.set_session_user(None)
|
0 | 0 |
from datetime import datetime, timezone
|
|
1 |
import csv
|
1 | 2 |
import json
|
2 | 3 |
|
3 | 4 |
from globals import *
|
|
17 | 18 |
"teamquality",
|
18 | 19 |
])
|
19 | 20 |
|
|
21 |
dump_fields = ["key", "time"] + sorted(list(valid_fields))
|
|
22 |
|
20 | 23 |
def store(username, gamedata):
|
21 | 24 |
req_fields = set(gamedata.keys())
|
22 | 25 |
missing = valid_fields - req_fields
|
|
31 | 34 |
raise ValueError(
|
32 | 35 |
"game key {} already exists".format(gamekey))
|
33 | 36 |
gamedata["key"] = gamekey
|
34 | |
gamedata["date"] = now.isoformat()
|
|
37 |
gamedata["time"] = now.isoformat()
|
35 | 38 |
gamedata["user"] = username
|
36 | 39 |
r.set(gamekey, json.dumps(gamedata))
|
|
40 |
r.sadd("apex:games:{}".format(username), gamekey)
|
|
41 |
|
|
42 |
def get_user_games(username):
|
|
43 |
return r.smembers("apex:games:{}".format(username))
|
|
44 |
|
|
45 |
def dump_games_to_csv(username, file):
|
|
46 |
gamekeys = list(get_user_games(username))
|
|
47 |
gamewriter = csv.writer(file)
|
|
48 |
gamekeys.sort()
|
|
49 |
gamewriter.writerow(dump_fields)
|
|
50 |
for gamekey in gamekeys:
|
|
51 |
gamedata = json.loads(r.get(gamekey).decode("utf-8"))
|
|
52 |
gamewriter.writerow(gamedata[k] for k in dump_fields)
|
|
53 |
|