def witness_match(): try: content = get_request_json(witness_schema) except ValidationError as error: logging.error(error) return make_response(error, 400) try: content["season"], content["challenger"] = pop_challenge(content["winner"], content["loser"]) except ValueError as error: logging.error(error) return make_response(error, 400) season = seasons.get_season(content["season"]) winner = scores.get_score(username=content['winner'], season=season.id) loser = scores.get_score(username=content['loser'], season=season.id) witness = users.get_user(username=content['witness']) winner_wins = content['winner_wins'] loser_wins = content['loser_wins'] if winner is None: try: winner = create_score(content["winner"], season.id) except ValueError as err: logging.error("Error: Player score could not be generated: ", err) return make_response("The winner could not be added to current season", 500) except SQLAlchemyError as err: logging.error(err) return make_response("Something went wrong :(", 500) if loser is None: try: loser = create_score(content["loser"], season.id) except ValueError as err: logging.error("Error: Player score could not be generated: ", err) return make_response("The loser could not be added to current season", 500) except SQLAlchemyError as err: logging.error(err) return make_response("Something went wrong :(", 500) if winner is None or loser is None: logging.error("Error: Player not found. Winner: {0}, Loser: {1}" .format(content['winner'], content['loser'])) return make_response("Could not find the match contestants", 404) if winner.username == loser.username: return make_response("A match cannot be conducted by the same player", 400) if not witness.witness: return make_response("That user is not a valid witness", 400) try: validate_rounds(winner_wins, loser_wins, season.game.max_rounds, season.game.min_rounds) except ValueError as error: make_response(error, 400) try: create_match(winner, loser, witness, season, winner_wins, loser_wins, content["challenger"]) except SQLAlchemyError as error: logging.error("Could not insert match into database: ", error) return make_response("Sorry, something went wrong!", 500) return make_response("Your match has been accepted!", 200)
def wrapped_function(*args, **kwargs): if not request.headers.environ.get('HTTP_AUTHORIZATION'): return make_response('No bearer token given', 401) bearer = request.headers.environ.get("HTTP_AUTHORIZATION") username = Oidc.user_by_token(bearer) if not username: return make_response("Unable to authenticate authorization token", 401) user = users.get_user(username=username) if not user: return abort('Unable to authenticate user details') g.user = user return func(*args, **kwargs)
def cancel_match(): try: content = get_request_json(cancel_schema) except ValidationError as error: logging.error('Error: Cancel request failed: ', error) return make_response("Sorry, that's not a valid cancellation statement", 400) challenger = users.get_user(username=content["challenger"]) challenged = users.get_user(username=content["challenged"]) if g.user.username not in [challenger.username, challenged.username]: return make_response('You cannot cancel a challenge for someone else', 403) if challenger == challenged: return make_response("You cannot even challenge yourself, wtf are you doing??", 400) try: del_challenge(challenger.username, challenged.username) except ValueError as err: logging.error("Error: Could not cancel challenge: ", err) return make_response("Hmm.. Looks like you're not challenging each other", 400) return make_response("You have cancelled {0}'s challenge".format(content["challenger"]), 200)
def accept_match(): try: content = get_request_json(accept_schema) except ValidationError as error: logging.error(error) return make_response(error, 400) challenger = users.get_user(username=content["challenger"]) challenged = users.get_user(username=content["challenged"]) if not g.user.username == challenged.username: return make_response('You cannot accept a challenge for someone else', 403) if challenger.username == challenged.username: return make_response("You cannot accept a challenge with yourself!", 400) try: accept_challenge(challenger.username, challenged.username) except ValueError as err: logging.error("Error: Could not accept challenge: ", err) return make_response("Hmm.. Looks like you are not being challenged", 400) return make_response("You have accepted {0}'s challenge".format(challenger.username), 200)
def create_user(): """ Silently create user if first login """ try: content = Oidc.get_user_info() if not users.get_user(username=content["username"]): _create_user(content["username"], content["first_name"], content["last_name"], content["profile_img"]) except ValidationError as error: logging.error(error) return make_response('', 400) except SQLAlchemyError as error: logging.error(error) return make_response('', 500) return make_response('', 201)
def new_match(): try: content = get_request_json(challenge_schema) except ValidationError as error: logging.error(error) return make_response(error, 400) challenger = users.get_user(username=content["challenger"]) challenged = users.get_user(username=content["challenged"]) season = seasons.get_season(_id=content["season"]) if not g.user.username == challenger.username: return make_response("You cannot issue a challenge for someone else", 403) if challenger.username == challenged.username: return make_response("You cannot challenge yourself!", 400) try: put_challenge(challenger.username, challenged.username, season.id) except ValueError as error: logging.error(error) return make_response("Hmm.. Seems like you're already challenging each other", 400) return make_response("You have challenged {0}".format(challenged.username), 200)
def validate_challenged(self, value): if not users.get_user(username=value): raise ValidationError('Your opponent is not a ranker user')
def validate_challenger(self, value): if not users.get_user(username=value): raise ValidationError('You are not a ranker user')
def get_slack_user(_id): user = users.get_user(slack_id=_id) if not user: return jsonify return user_schema.dumps(user)
def get_user_is_admin(username): user = users.get_user(username=username) if not user: return jsonify(admin=False) return jsonify(admin=user.admin)
def get_user_is_witness(username): user = users.get_user(username=username) if not user: return jsonify(witness=False) return jsonify(witness=user.witness)
def get_user(username): user = users.get_user(username=username) return user_schema.dumps(user)
def get_users(): _users = users.get_user() return users_schema.dumps(_users)