def api_user_create(): try: auth_token = request.form["auth_token"] user_first_name = request.form["user_first_name"] user_last_name = request.form["user_last_name"] user_other_names = request.form["user_other_names"] or None user_email = request.form["user_email"] or None user_username = request.form["user_first_name"] user_password = request.form["user_password"] or gen_password(8) user_rank = request.form["rank"] if "" in (user_first_name, user_last_name, user_username, user_password, user_rank): raise KeyError except KeyError as e: raise APIMissingField(e.args[0]) caller = get_user_from_token(auth_token) if caller.rank != "admin": return make_response( jsonify({ "status": "failed", "message": "You do not have a high enough rank to create users." })) conn = engine.connect() query = sql.insert( User, { User.first_name: user_first_name, User.last_name: user_last_name, User.other_names: user_other_names, User.email: user_email, User.username: user_username, User.pass_hash: crypt_hash(user_password), User.rank: user_rank }) res = conn.execute(query) if res.inserted_primary_key: return make_response( jsonify({ "status": "success", "message": "User created", "user_id": res.inserted_primary_key })) else: return make_response( jsonify({ "status": "failed", "message": "Unknown error" }))
def api_user_create(): try: auth_token = request.form["auth_token"] user_first_name = request.form["user_first_name"] user_last_name = request.form["user_last_name"] user_other_names = request.form["user_other_names"] or None user_email = request.form["user_email"] or None user_username = request.form["user_first_name"] user_password = request.form["user_password"] or gen_password(8) user_rank = request.form["rank"] if "" in (user_first_name, user_last_name, user_username, user_password, user_rank): raise KeyError except KeyError as e: raise APIMissingField(e.args[0]) caller = get_user_from_token(auth_token) if caller.rank != "admin": return make_response(jsonify({ "status": "failed", "message": "You do not have a high enough rank to create users." })) conn = engine.connect() query = sql.insert(User, { User.first_name: user_first_name, User.last_name: user_last_name, User.other_names: user_other_names, User.email: user_email, User.username: user_username, User.pass_hash: crypt_hash(user_password), User.rank: user_rank }) res = conn.execute(query) if res.inserted_primary_key: return make_response(jsonify({ "status": "success", "message": "User created", "user_id": res.inserted_primary_key })) else: return make_response(jsonify({ "status": "failed", "message": "Unknown error" }))
def api_token_add(): try: auth_token = request.form["auth_token"] token_value = request.form["token_value"] except KeyError as e: raise APIMissingField(e.args[0]) try: token_code = request.form["token_code"] except KeyError as e: token_code = None try: token_number = request.form["token_number"] except KeyError as e: token_number = 1 caller = get_user_from_token(auth_token) token_number = min(100, int(token_number)) if token_number == 0: raise APIInvalidField("token_number") if caller.rank != "admin": if caller.credits < token_value * token_number: raise APIInvalidUsage( "Not enough credits") # TODO: add better error tokens = [] if token_number == 1 and token_code: if re.match("[^A-Z0-9]", token_code) or len(token_code) != 10: token_code = gen_password(10) tokens.append({ "code": token_code, "value": token_value, "creator": caller.id, "redeemed": False }) else: for token in range(token_number): tokens.append({ "code": gen_password(10), "value": token_value, "creator": caller.id, "redeemed": False }) pprint(tokens) conn = engine.connect() query1 = sql.insert(Token, tokens) res = conn.execute(query1) if res.inserted_primary_key: if caller.rank != "admin": query = sql.update(User).where(User.id == caller.id).values( {User.credits: caller.credits - token_number * token_code}) res = conn.execute(query) return make_response( jsonify({ "status": "success", "message": "%s codes have been generated with a value of %s credits." % (str(token_number), str(token_value)), "tokens": list({ "code": x["code"], "value": x["value"] } for x in tokens) }))
def api_estab_create(): try: full_name = request.form["full_name"] code_name = request.form["code_name"] except KeyError as e: # do something more useful here raise APIMissingField(e.args[0]) try: user = request.form["user"] or "" password = request.form["password"] or "" if "" in (user, password): raise KeyError default_user = False except KeyError as e: user = "******" password = gen_password(8) default_user = True if full_name == "": raise APIMissingField("name") conn = engine.connect() query = sql.Insert(Establishment, { Establishment.full_name: full_name, Establishment.code_name: code_name }) try: res = conn.execute(query) except sqlalchemy.exc.IntegrityError as e: print(e) return make_response( jsonify({ "status": "failed", "message": "Name is in use." }), 400) estab_id = res.inserted_primary_key[0] query = sql.insert( User, { User.first_name: "Admin", User.last_name: "User", User.rank: "admin", User.username: user, User.pass_hash: crypt_hash(password), User.establishment: estab_id }) res = conn.execute(query) if default_user: return make_response( jsonify({ "status": "success", "message": "Establishment was created! (User does not work yet...)", "id": estab_id, "username": user, "password": password })) else: return make_response( jsonify({ "status": "success", "message": "Establishment was created! (User does not work yet...)", "id": estab_id, "username": user, "password": "******" }))
def api_token_add(): try: auth_token = request.form["auth_token"] token_value = request.form["token_value"] except KeyError as e: raise APIMissingField(e.args[0]) try: token_code = request.form["token_code"] except KeyError as e: token_code = None try: token_number = request.form["token_number"] except KeyError as e: token_number = 1 caller = get_user_from_token(auth_token) token_number = min(100, int(token_number)) if token_number == 0: raise APIInvalidField("token_number") if caller.rank != "admin": if caller.credits < token_value * token_number: raise APIInvalidUsage("Not enough credits") # TODO: add better error tokens = [] if token_number == 1 and token_code: if re.match("[^A-Z0-9]", token_code) or len(token_code) != 10: token_code = gen_password(10) tokens.append({ "code": token_code, "value": token_value, "creator": caller.id, "redeemed": False }) else: for token in range(token_number): tokens.append({ "code": gen_password(10), "value": token_value, "creator": caller.id, "redeemed": False }) pprint(tokens) conn = engine.connect() query1 = sql.insert(Token, tokens) res = conn.execute(query1) if res.inserted_primary_key: if caller.rank != "admin": query = sql.update(User).where(User.id == caller.id).values({ User.credits: caller.credits - token_number*token_code }) res = conn.execute(query) return make_response(jsonify({ "status": "success", "message": "%s codes have been generated with a value of %s credits." % ( str(token_number), str(token_value) ), "tokens": list({"code": x["code"], "value": x["value"]} for x in tokens) }))
def api_estab_create(): try: full_name = request.form["full_name"] code_name = request.form["code_name"] except KeyError as e: # do something more useful here raise APIMissingField(e.args[0]) try: user = request.form["user"] or "" password = request.form["password"] or "" if "" in (user, password): raise KeyError default_user = False except KeyError as e: user = "******" password = gen_password(8) default_user = True if full_name == "": raise APIMissingField("name") conn = engine.connect() query = sql.Insert(Establishment, { Establishment.full_name: full_name, Establishment.code_name: code_name }) try: res = conn.execute(query) except sqlalchemy.exc.IntegrityError as e: print(e) return make_response(jsonify({ "status": "failed", "message": "Name is in use." }), 400) estab_id = res.inserted_primary_key[0] query = sql.insert(User, { User.first_name: "Admin", User.last_name: "User", User.rank: "admin", User.username: user, User.pass_hash: crypt_hash(password), User.establishment: estab_id }) res = conn.execute(query) if default_user: return make_response(jsonify({ "status": "success", "message": "Establishment was created! (User does not work yet...)", "id": estab_id, "username": user, "password": password })) else: return make_response(jsonify({ "status": "success", "message": "Establishment was created! (User does not work yet...)", "id": estab_id, "username": user, "password": "******" }))