Only db commit on requests that change the database

This commit is contained in:
Jeremy Zhang 2018-06-15 00:13:09 +00:00
parent 4c41a31418
commit d540a4888b
5 changed files with 33 additions and 5 deletions

View File

@ -29,7 +29,6 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # Suppress the warning/no
app.config['RATELIMIT_HEADERS_ENABLED'] = True app.config['RATELIMIT_HEADERS_ENABLED'] = True
app.config['SQLALCHEMY_POOL_RECYCLE'] = 100 app.config['SQLALCHEMY_POOL_RECYCLE'] = 100
app.config['SQLALCHEMY_POOL_SIZE'] = 15 app.config['SQLALCHEMY_POOL_SIZE'] = 15
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['RATELIMIT_STORAGE_URL'] = config["redis-uri"] app.config['RATELIMIT_STORAGE_URL'] = config["redis-uri"]
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=3) app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=3)
app.config['REDIS_URL'] = config["redis-uri"] app.config['REDIS_URL'] = config["redis-uri"]

View File

@ -68,6 +68,7 @@ def cosmetics_post():
badges = [] badges = []
user.badges = json.dumps(badges) user.badges = json.dumps(badges)
db.session.add(user) db.session.add(user)
db.session.commit()
return ('', 204) return ('', 204)
@admin.route("/cosmetics", methods=["DELETE"]) @admin.route("/cosmetics", methods=["DELETE"])
@ -80,6 +81,7 @@ def cosmetics_delete():
if not entry: if not entry:
abort(409) abort(409)
db.session.delete(entry) db.session.delete(entry)
db.session.commit()
return ('', 204) return ('', 204)
@admin.route("/cosmetics", methods=["PATCH"]) @admin.route("/cosmetics", methods=["PATCH"])
@ -108,6 +110,7 @@ def cosmetics_patch():
if badges == [""]: if badges == [""]:
badges = [] badges = []
entry.badges = json.dumps(badges) entry.badges = json.dumps(badges)
db.session.commit()
return ('', 204) return ('', 204)
def prepare_guild_members_list(members, bans): def prepare_guild_members_list(members, bans):
@ -204,6 +207,7 @@ def update_administrate_guild(guild_id):
if guest_icon != None and guest_icon.strip() == "": if guest_icon != None and guest_icon.strip() == "":
guest_icon = None guest_icon = None
db_guild.guest_icon = guest_icon db_guild.guest_icon = guest_icon
db.session.commit()
emit("guest_icon_change", {"guest_icon": guest_icon if guest_icon else url_for('static', filename='img/titanembeds_square.png')}, room="GUILD_"+guild_id, namespace="/gateway") emit("guest_icon_change", {"guest_icon": guest_icon if guest_icon else url_for('static', filename='img/titanembeds_square.png')}, room="GUILD_"+guild_id, namespace="/gateway")
return jsonify( return jsonify(
guild_id=db_guild.guild_id, guild_id=db_guild.guild_id,
@ -262,6 +266,7 @@ def post_titan_tokens():
if get_titan_token(user_id) != -1: if get_titan_token(user_id) != -1:
abort(409) abort(409)
set_titan_token(user_id, amount, "NEW VIA ADMIN [{}]".format(str(reason))) set_titan_token(user_id, amount, "NEW VIA ADMIN [{}]".format(str(reason)))
db.session.commit()
return ('', 204) return ('', 204)
@admin.route("/tokens", methods=["PATCH"]) @admin.route("/tokens", methods=["PATCH"])
@ -275,6 +280,7 @@ def patch_titan_tokens():
if get_titan_token(user_id) == -1: if get_titan_token(user_id) == -1:
abort(409) abort(409)
set_titan_token(user_id, amount, "MODIFY VIA ADMIN [{}]".format(str(reason))) set_titan_token(user_id, amount, "MODIFY VIA ADMIN [{}]".format(str(reason)))
db.session.commit()
return ('', 204) return ('', 204)
@admin.route("/disabled_guilds", methods=["GET"]) @admin.route("/disabled_guilds", methods=["GET"])
@ -290,6 +296,7 @@ def post_disabled_guilds():
abort(409) abort(409)
guild = DisabledGuilds(guild_id) guild = DisabledGuilds(guild_id)
db.session.add(guild) db.session.add(guild)
db.session.commit()
return ('', 204) return ('', 204)
@admin.route("/disabled_guilds", methods=["DELETE"]) @admin.route("/disabled_guilds", methods=["DELETE"])
@ -300,6 +307,7 @@ def delete_disabled_guilds():
abort(409) abort(409)
guild = db.session.query(DisabledGuilds).filter(DisabledGuilds.guild_id == guild_id).first() guild = db.session.query(DisabledGuilds).filter(DisabledGuilds.guild_id == guild_id).first()
db.session.delete(guild) db.session.delete(guild)
db.session.commit()
return ('', 204) return ('', 204)
@admin.route("/custom_css", methods=["GET"]) @admin.route("/custom_css", methods=["GET"])
@ -344,6 +352,7 @@ def edit_custom_css_post(css_id):
dbcss.css = css dbcss.css = css
dbcss.css_variables = variables dbcss.css_variables = variables
dbcss.css_var_bool = variables_enabled dbcss.css_var_bool = variables_enabled
db.session.commit()
return jsonify({"id": dbcss.id}) return jsonify({"id": dbcss.id})
@admin.route("/custom_css/edit/<css_id>", methods=["DELETE"]) @admin.route("/custom_css/edit/<css_id>", methods=["DELETE"])
@ -353,6 +362,7 @@ def edit_custom_css_delete(css_id):
if not dbcss: if not dbcss:
abort(404) abort(404)
db.session.delete(dbcss) db.session.delete(dbcss)
db.session.commit()
return jsonify({}) return jsonify({})
@admin.route("/custom_css/new", methods=["GET"]) @admin.route("/custom_css/new", methods=["GET"])

View File

@ -330,6 +330,7 @@ def post():
else: else:
message = discord_api.create_message(channel_id, content) message = discord_api.create_message(channel_id, content)
status_code = message['code'] status_code = message['code']
db.session.commit()
response = jsonify(message=message.get('content', message), status=status, illegal_reasons=illegal_reasons) response = jsonify(message=message.get('content', message), status=status, illegal_reasons=illegal_reasons)
response.status_code = status_code response.status_code = status_code
return response return response
@ -366,6 +367,7 @@ def create_unauthenticated_user():
captcha_response = request.form['captcha_response'] captcha_response = request.form['captcha_response']
if not verify_captcha_request(captcha_response, request.remote_addr): if not verify_captcha_request(captcha_response, request.remote_addr):
abort(412) abort(412)
final_response = None
if not checkUserBanned(guild_id, ip_address): if not checkUserBanned(guild_id, ip_address):
session['username'] = username session['username'] = username
if 'user_id' not in session or len(str(session["user_id"])) > 4: if 'user_id' not in session or len(str(session["user_id"])) > 4:
@ -379,12 +381,14 @@ def create_unauthenticated_user():
session['user_keys'][guild_id] = key session['user_keys'][guild_id] = key
session.permanent = False session.permanent = False
status = update_user_status(guild_id, username, key) status = update_user_status(guild_id, username, key)
return jsonify(status=status) final_response = jsonify(status=status)
else: else:
status = {'banned': True} status = {'banned': True}
response = jsonify(status=status) response = jsonify(status=status)
response.status_code = 403 response.status_code = 403
return response final_response = response
db.session.commit()
return final_response
@api.route("/change_unauthenticated_username", methods=["POST"]) @api.route("/change_unauthenticated_username", methods=["POST"])
@rate_limiter.limit("1 per 10 minute", key_func=guild_ratelimit_key) @rate_limiter.limit("1 per 10 minute", key_func=guild_ratelimit_key)
@ -402,6 +406,7 @@ def change_unauthenticated_username():
abort(404) abort(404)
if not guild_query_unauth_users_bool(guild_id): if not guild_query_unauth_users_bool(guild_id):
abort(401) abort(401)
final_response = None
if not checkUserBanned(guild_id, ip_address): if not checkUserBanned(guild_id, ip_address):
if 'user_keys' not in session or guild_id not in session['user_keys'] or not session['unauthenticated']: if 'user_keys' not in session or guild_id not in session['user_keys'] or not session['unauthenticated']:
abort(424) abort(424)
@ -415,12 +420,14 @@ def change_unauthenticated_username():
session['user_keys'][guild_id] = key session['user_keys'][guild_id] = key
status = update_user_status(guild_id, username, key) status = update_user_status(guild_id, username, key)
emit("embed_user_disconnect", emitmsg, room="GUILD_"+guild_id, namespace="/gateway") emit("embed_user_disconnect", emitmsg, room="GUILD_"+guild_id, namespace="/gateway")
return jsonify(status=status) final_response = jsonify(status=status)
else: else:
status = {'banned': True} status = {'banned': True}
response = jsonify(status=status) response = jsonify(status=status)
response.status_code = 403 response.status_code = 403
return response final_response = response
db.session.commit()
return final_response
def get_guild_guest_icon(guild_id): def get_guild_guest_icon(guild_id):
guest_icon = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first().guest_icon guest_icon = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first().guest_icon
@ -487,6 +494,7 @@ def create_authenticated_user():
if not db_user: if not db_user:
db_user = AuthenticatedUsers(guild_id, session['user_id']) db_user = AuthenticatedUsers(guild_id, session['user_id'])
db.session.add(db_user) db.session.add(db_user)
db.session.commit()
status = update_user_status(guild_id, session['username']) status = update_user_status(guild_id, session['username'])
return jsonify(status=status) return jsonify(status=status)
else: else:
@ -550,6 +558,7 @@ def webhook_discordbotsorg_vote():
pass pass
DBLTrans = DiscordBotsOrgTransactions(int(user_id), vote_type, referrer) DBLTrans = DiscordBotsOrgTransactions(int(user_id), vote_type, referrer)
db.session.add(DBLTrans) db.session.add(DBLTrans)
db.session.commit()
return ('', 204) return ('', 204)
@api.route("/af/direct_message", methods=["POST"]) @api.route("/af/direct_message", methods=["POST"])

View File

@ -165,6 +165,7 @@ def edit_custom_css_post(css_id):
dbcss.css = css dbcss.css = css
dbcss.css_variables = variables dbcss.css_variables = variables
dbcss.css_var_bool = variables_enabled dbcss.css_var_bool = variables_enabled
db.session.commit()
return jsonify({"id": dbcss.id}) return jsonify({"id": dbcss.id})
@user.route("/custom_css/edit/<css_id>", methods=["DELETE"]) @user.route("/custom_css/edit/<css_id>", methods=["DELETE"])
@ -179,6 +180,7 @@ def edit_custom_css_delete(css_id):
if dbcss.user_id != session['user_id']: if dbcss.user_id != session['user_id']:
abort(403) abort(403)
db.session.delete(dbcss) db.session.delete(dbcss)
db.session.commit()
return jsonify({}) return jsonify({})
@user.route("/administrate_guild/<guild_id>", methods=["GET"]) @user.route("/administrate_guild/<guild_id>", methods=["GET"])
@ -252,6 +254,7 @@ def update_administrate_guild(guild_id):
guest_icon = None guest_icon = None
db_guild.guest_icon = guest_icon db_guild.guest_icon = guest_icon
db.session.commit()
emit("guest_icon_change", {"guest_icon": guest_icon if guest_icon else url_for('static', filename='img/titanembeds_square.png')}, room="GUILD_"+guild_id, namespace="/gateway") emit("guest_icon_change", {"guest_icon": guest_icon if guest_icon else url_for('static', filename='img/titanembeds_square.png')}, room="GUILD_"+guild_id, namespace="/gateway")
return jsonify( return jsonify(
guild_id=db_guild.guild_id, guild_id=db_guild.guild_id,
@ -339,6 +342,7 @@ def ban_unauthenticated_user():
db.session.delete(db_ban) db.session.delete(db_ban)
db_ban = UnauthenticatedBans(guild_id, db_user.ip_address, db_user.username, db_user.discriminator, reason, session["user_id"]) db_ban = UnauthenticatedBans(guild_id, db_user.ip_address, db_user.username, db_user.discriminator, reason, session["user_id"])
db.session.add(db_ban) db.session.add(db_ban)
db.session.commit()
return ('', 204) return ('', 204)
@user.route("/ban", methods=["DELETE"]) @user.route("/ban", methods=["DELETE"])
@ -361,6 +365,7 @@ def unban_unauthenticated_user():
if db_ban.lifter_id is not None: if db_ban.lifter_id is not None:
abort(409) abort(409)
db_ban.liftBan(session["user_id"]) db_ban.liftBan(session["user_id"])
db.session.commit()
return ('', 204) return ('', 204)
@user.route("/revoke", methods=["POST"]) @user.route("/revoke", methods=["POST"])
@ -380,6 +385,7 @@ def revoke_unauthenticated_user():
if db_user.isRevoked(): if db_user.isRevoked():
abort(409) abort(409)
db_user.revokeUser() db_user.revokeUser()
db.session.commit()
return ('', 204) return ('', 204)
@user.route('/donate', methods=["GET"]) @user.route('/donate', methods=["GET"])
@ -440,6 +446,7 @@ def donate_confirm():
set_titan_token(session["user_id"], tokens, action) set_titan_token(session["user_id"], tokens, action)
session["tokens"] = get_titan_token(session["user_id"]) session["tokens"] = get_titan_token(session["user_id"])
add_badge(session["user_id"], "supporter") add_badge(session["user_id"], "supporter")
db.session.commit()
return redirect(url_for('user.donate_thanks', transaction=trans_id)) return redirect(url_for('user.donate_thanks', transaction=trans_id))
else: else:
return redirect(url_for('index')) return redirect(url_for('index'))
@ -482,6 +489,7 @@ def donate_patch():
entry = Cosmetics(session["user_id"]) entry = Cosmetics(session["user_id"])
entry.guest_icon = True entry.guest_icon = True
db.session.add(entry) db.session.add(entry)
db.session.commit()
return ('', 204) return ('', 204)
@user.route("/patreon") @user.route("/patreon")
@ -564,6 +572,7 @@ def patreon_sync_post():
set_titan_token(session["user_id"], usr["titan"]["eligible_tokens"], "PATREON {} [{}]".format(usr["attributes"]["full_name"], usr["id"])) set_titan_token(session["user_id"], usr["titan"]["eligible_tokens"], "PATREON {} [{}]".format(usr["attributes"]["full_name"], usr["id"]))
add_badge(session["user_id"], "supporter") add_badge(session["user_id"], "supporter")
session["tokens"] = get_titan_token(session["user_id"]) session["tokens"] = get_titan_token(session["user_id"])
db.session.commit()
return ('', 204) return ('', 204)
@user.route("/patreon/thanks") @user.route("/patreon/thanks")

View File

@ -145,6 +145,7 @@ def update_user_status(guild_id, username, user_key=None):
if dbUser.username != username or dbUser.ip_address != ip_address: if dbUser.username != username or dbUser.ip_address != ip_address:
dbUser.username = username dbUser.username = username
dbUser.ip_address = ip_address dbUser.ip_address = ip_address
db.session.commit()
else: else:
status = { status = {
'authenticated': True, 'authenticated': True,