Move db to a submodule, fix register issue
This commit is contained in:
parent
1b0578adf4
commit
72c632bb93
2 changed files with 32 additions and 30 deletions
36
server.py
36
server.py
|
@ -9,6 +9,8 @@ import etcd
|
|||
import json
|
||||
import datetime
|
||||
|
||||
from ungleich_game_db import *
|
||||
|
||||
from flask import Flask, abort, request, Response
|
||||
from flask_restful import reqparse
|
||||
|
||||
|
@ -25,31 +27,6 @@ def require_args(*args):
|
|||
parser.add_argument(arg, required=True)
|
||||
return parser.parse_args()
|
||||
|
||||
class etcdWrapper(object):
|
||||
""" Generalises some etcd actions """
|
||||
|
||||
def __init__(self, client, base):
|
||||
self.client = client
|
||||
self.base = base
|
||||
|
||||
def read_key_or_none(self, path, recursive=False):
|
||||
try:
|
||||
data = self.client.read(path, recursive=recursive)
|
||||
except etcd.EtcdKeyNotFound:
|
||||
return None
|
||||
except Exception:
|
||||
abort(Response(status=400, response="Error connecting to etcd"))
|
||||
|
||||
return data
|
||||
|
||||
def get_user_key_or_none(self, user, key):
|
||||
path = "{}/user/{}/{}".format(self.base, user, key)
|
||||
return self.read_key_or_none(path)
|
||||
|
||||
def set_user_key(self, user, key, value):
|
||||
path = "{}/user/{}/{}".format(self.base, user, key)
|
||||
self.client.write(path, value)
|
||||
|
||||
|
||||
class Challenge(object):
|
||||
""" A sample challenge -- inherit this and overwrite accordingly """
|
||||
|
@ -111,7 +88,7 @@ Submit your network with the "network" parameter.
|
|||
self.db.set_user_key(user, "network", network)
|
||||
self.save_points(user)
|
||||
|
||||
return "Network {} registered, have fun with the next challenge!".format(network)
|
||||
return "Network {} registered, have fun with the next challenge!\n".format(network)
|
||||
|
||||
class IPv6Address(Challenge):
|
||||
points = 20
|
||||
|
@ -280,15 +257,14 @@ Point list (aka high score)
|
|||
|
||||
def register(self):
|
||||
args = require_args("user")
|
||||
path = "{}/{}/registered_at".format(self.userbase, args['user'])
|
||||
value = str(datetime.datetime.now())
|
||||
|
||||
cur = self.read_etcd(path)
|
||||
cur = self.wrapper.get_user_key_or_none(args['user'], "registered_at")
|
||||
value = str(datetime.datetime.now())
|
||||
|
||||
if cur:
|
||||
value = cur.value
|
||||
else:
|
||||
self.client.write(path, value)
|
||||
self.wrapper.set_user_key(args['user'], "registered_at", value)
|
||||
|
||||
return "Registered at: {}\n".format(value)
|
||||
|
||||
|
|
26
ungleich_game_db.py
Normal file
26
ungleich_game_db.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
import etcd
|
||||
|
||||
class etcdWrapper(object):
|
||||
""" Generalises some etcd actions """
|
||||
|
||||
def __init__(self, client, base):
|
||||
self.client = client
|
||||
self.base = base
|
||||
|
||||
def read_key_or_none(self, path, recursive=False):
|
||||
try:
|
||||
data = self.client.read(path, recursive=recursive)
|
||||
except etcd.EtcdKeyNotFound:
|
||||
return None
|
||||
except Exception:
|
||||
abort(Response(status=400, response="Error connecting to etcd"))
|
||||
|
||||
return data
|
||||
|
||||
def get_user_key_or_none(self, user, key):
|
||||
path = "{}/user/{}/{}".format(self.base, user, key)
|
||||
return self.read_key_or_none(path)
|
||||
|
||||
def set_user_key(self, user, key, value):
|
||||
path = "{}/user/{}/{}".format(self.base, user, key)
|
||||
self.client.write(path, value)
|
Loading…
Reference in a new issue