def put(self, guild_id): guild = self._get_object(guild_id) guild.update(**request.json) db.update(guild) current_app.logger.debug( "The guild {0} has been updated successfully.".format(guild_id)) return {}, 204
def refresh_token_if_needed(token): if int(token.access_token_expiry_date) >= int(time.time()): return if token.token_type == "user": data = { "client_id": constants.CLIENT_ID, "client_secret": constants.CLIENT_SECRET, "grant_type": "refresh_token", "refresh_token": token.refresh_token, "redirect_uri": constants.DISCORD_REDIRECT_URI, "scope": token.scope, } headers = {"Content-Type": "application/x-www-form-urlencoded"} try: r = requests.post(endpoints.DISCORD_TOKEN + "/token", data=data, headers=headers) r.raise_for_status() except requests.exceptions.HTTPError: raise exceptions.DiscordTokenError() token.access_token = data["access_token"] token.access_token_expiry_date = str( int(time.time()) + data["expires_in"]) token.refresh_token = data["refresh_token"] token.scope = data["scope"] db.update(token)
def put(self, tournament_id): tournament = self._get_object(tournament_id) tournament.update(**request.json) db.update(tournament) current_app.logger.debug( "The tournament {0} has been updated successfully.".format( tournament_id)) return {}, 204
def put(self, tournament_id, allowed_reschedule_id): allowed_reschedule = self._get_object(tournament_id, allowed_reschedule_id) allowed_reschedule.update(**request.json) db.update(allowed_reschedule) current_app.logger.debug( "The allowed reschedule {0} for match id {1} has been updated successfully." .format(allowed_reschedule_id, allowed_reschedule.match_id)) return {}, 204
def put(self, tournament_id, bracket_id, spreadsheet_id): spreadsheet = self._get_object(tournament_id, bracket_id, spreadsheet_id) body = request.json if "spreadsheet_id" in body: body["spreadsheet_id"] = spreadsheet_api.extract_spreadsheet_id( body["spreadsheet_id"]) spreadsheet.update(**body) db.update(spreadsheet) current_app.logger.debug( "The qualifiers spreadsheet {0} has been updated successfully.". format(spreadsheet_id)) return {}, 204
def put(self, user_id): user = self._get_object(user_id) body = request.json if "discord_id" in body: del body["discord_id"] if "discord_id_snowflake" in body: del body["discord_id_snowflake"] if "osu_name_hash" in body: body["osu_name_hash"] = body["osu_name_hash"].casefold() user.update(**body) db.update(user) current_app.logger.debug( "The user {0} has been updated successfully.".format(user_id)) return {}, 204
def post(self): body = request.json if (not body or not body["guild_id"] or not body["guild_id_snowflake"] or not body["name"] or not body["acronym"]): raise exceptions.MissingRequiredInformation() body["guild_id"] = str(body["guild_id"]) tournament = Tournament(**body) db.add(tournament) bracket = Bracket(tournament_id=tournament.id, name=tournament.name) db.add(bracket) tournament.current_bracket_id = bracket.id db.update(tournament) current_app.logger.debug( "The tournament {0} has been created successfully.".format( tournament.id)) return get_tournament_data(tournament, True), 201
def post(self, tournament_id, bracket_id): bracket = (db.query(Bracket).where( Bracket.tournament_id == tournament_id).where( Bracket.id == bracket_id).first()) if not bracket: raise exceptions.NotFound() body = request.json if not body or not body["spreadsheet_id"]: raise exceptions.MissingRequiredInformation() body["spreadsheet_id"] = spreadsheet_api.extract_spreadsheet_id( body["spreadsheet_id"]) spreadsheet = QualifiersSpreadsheet(**body) db.add(spreadsheet) bracket.qualifiers_spreadsheet_id = spreadsheet.id db.update(bracket) current_app.logger.debug( "The qualifiers spreadsheet {0} for the bracket {1} has been created successfully." .format(spreadsheet.id, bracket_id)) return spreadsheet.get_api_dict(), 201
def post(self): body = request.json if "tosurnament_code" not in body or "osu_code" not in body: raise exceptions.BadRequest() user = db.query(User).where( User.code == body["tosurnament_code"]).first() if not user: raise exceptions.InternalServerError() if user.verified: raise exceptions.UserAlreadyVerified() parameters = { "client_id": constants.OSU_CLIENT_ID, "client_secret": constants.OSU_CLIENT_SECRET, "code": body["osu_code"], "grant_type": "authorization_code", "redirect_uri": constants.OSU_REDIRECT_URI, } token_request = requests.post(endpoints.OSU_TOKEN, data=parameters) if not token_request.ok: raise exceptions.OsuTokenError() token_results = token_request.json() me_headers = { "Authorization": "Bearer " + token_results["access_token"] } me_request = requests.get(endpoints.OSU_ME, headers=me_headers) if not me_request.ok: raise exceptions.OsuMeError() me_results = me_request.json() user.osu_id = str(me_results["id"]) user.osu_name = me_results["username"] user.osu_name_hash = me_results["username"].casefold() if me_results["previous_usernames"]: user.osu_previous_name = me_results["previous_usernames"][-1] user.verified = True db.update(user) current_app.logger.debug( "The user with the osu id {0} has been authentificated successfully." .format(user.osu_id)) return {}, 204
def store_token(data): token = None session_token = request.headers.get("Authorization") if session_token: token = db.query(Token).where( Token.session_token == session_token).first() if not token: token = Token() token.discord_user_id = get_user_id(data["access_token"]) session_token = str(uuid.uuid4()) token.session_token = session_token token.expiry_date = str(int(time.time()) + 2592000) db.add(token) token.access_token = data["access_token"] token.token_type = "user" token.access_token_expiry_date = str(int(time.time()) + data["expires_in"]) token.refresh_token = data["refresh_token"] token.scope = data["scope"] db.update(token) current_app.logger.debug( "A token has successfully been created/updated for the user {0}". format(token.discord_user_id)) return session_token