git.haldean.org apex / aa92ad4
support dumping games to CSV Haldean Brown 3 years ago
2 changed file(s) with 29 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
00 import flask
1 import io
12 import redis
23
34 import globals
6061 flask.flash("User \"{}\" added".format(username))
6162 return flask.redirect(flask.url_for("adduser"))
6263
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
6374 @app.route("/logout")
6475 def logout():
6576 users.set_session_user(None)
00 from datetime import datetime, timezone
1 import csv
12 import json
23
34 from globals import *
1718 "teamquality",
1819 ])
1920
21 dump_fields = ["key", "time"] + sorted(list(valid_fields))
22
2023 def store(username, gamedata):
2124 req_fields = set(gamedata.keys())
2225 missing = valid_fields - req_fields
3134 raise ValueError(
3235 "game key {} already exists".format(gamekey))
3336 gamedata["key"] = gamekey
34 gamedata["date"] = now.isoformat()
37 gamedata["time"] = now.isoformat()
3538 gamedata["user"] = username
3639 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