Commit to database on only on request teardown

This commit is contained in:
Jeremy Zhang 2018-01-22 07:52:51 +00:00
parent 2c949d911b
commit 56d25d6ecb
10 changed files with 1 additions and 26 deletions

View File

@ -28,6 +28,7 @@ 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'] = 250 app.config['SQLALCHEMY_POOL_RECYCLE'] = 250
app.config['SQLALCHEMY_POOL_SIZE'] = 100 app.config['SQLALCHEMY_POOL_SIZE'] = 100
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

@ -67,7 +67,6 @@ 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,7 +79,6 @@ 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"])
@ -109,7 +107,6 @@ 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):
@ -202,7 +199,6 @@ 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(
id=db_guild.id, id=db_guild.id,
@ -288,7 +284,6 @@ 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"])
@ -299,7 +294,6 @@ 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,7 +338,6 @@ 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"])
@ -354,7 +347,6 @@ 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"])
@ -381,5 +373,4 @@ def new_custom_css_post():
css = None css = None
css = UserCSS(name, user_id, variables_enabled, variables, css) css = UserCSS(name, user_id, variables_enabled, variables, css)
db.session.add(css) db.session.add(css)
db.session.commit()
return jsonify({"id": css.id}) return jsonify({"id": css.id})

View File

@ -313,7 +313,6 @@ def create_unauthenticated_user():
session['user_id'] = random.randint(0,9999) session['user_id'] = random.randint(0,9999)
user = UnauthenticatedUsers(guild_id, username, session['user_id'], ip_address) user = UnauthenticatedUsers(guild_id, username, session['user_id'], ip_address)
db.session.add(user) db.session.add(user)
db.session.commit()
key = user.user_key key = user.user_key
if 'user_keys' not in session: if 'user_keys' not in session:
session['user_keys'] = {guild_id: key} session['user_keys'] = {guild_id: key}
@ -353,7 +352,6 @@ def change_unauthenticated_username():
session['user_id'] = random.randint(0,9999) session['user_id'] = random.randint(0,9999)
user = UnauthenticatedUsers(guild_id, username, session['user_id'], ip_address) user = UnauthenticatedUsers(guild_id, username, session['user_id'], ip_address)
db.session.add(user) db.session.add(user)
db.session.commit()
key = user.user_key key = user.user_key
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)
@ -428,7 +426,6 @@ 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:

View File

@ -119,7 +119,6 @@ def new_custom_css_post():
css = None css = None
css = UserCSS(name, user_id, variables_enabled, variables, css) css = UserCSS(name, user_id, variables_enabled, variables, css)
db.session.add(css) db.session.add(css)
db.session.commit()
return jsonify({"id": css.id}) return jsonify({"id": css.id})
@user.route("/custom_css/edit/<css_id>", methods=["GET"]) @user.route("/custom_css/edit/<css_id>", methods=["GET"])
@ -165,7 +164,6 @@ 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"])
@ -180,7 +178,6 @@ 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"])
@ -250,7 +247,6 @@ 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(
id=db_guild.id, id=db_guild.id,
@ -337,7 +333,6 @@ 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"])
@ -481,7 +476,6 @@ 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")
@ -561,7 +555,6 @@ def patreon_sync_post():
dbpatreon = Patreon(usr["id"]) dbpatreon = Patreon(usr["id"])
dbpatreon.total_synced = usr["titan"]["total_cents_pledged"] dbpatreon.total_synced = usr["titan"]["total_cents_pledged"]
db.session.add(dbpatreon) db.session.add(dbpatreon)
db.session.commit()
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"])

View File

@ -30,5 +30,4 @@ def set_titan_token(user_id, amt_change, action):
db.session.add(transact) db.session.add(transact)
token_usr.tokens = new_token_count token_usr.tokens = new_token_count
db.session.add(token_usr) db.session.add(token_usr)
db.session.commit()
return True return True

View File

@ -39,7 +39,6 @@ def set_badges(user_id, badges):
usr = Cosmetics(user_id) usr = Cosmetics(user_id)
usr.badges = json.dumps(badges) usr.badges = json.dumps(badges)
db.session.add(usr) db.session.add(usr)
db.session.commit()
def get_badges(user_id): def get_badges(user_id):
usr = db.session.query(Cosmetics).filter(Cosmetics.user_id == user_id).first() usr = db.session.query(Cosmetics).filter(Cosmetics.user_id == user_id).first()

View File

@ -44,5 +44,4 @@ class Guilds(db.Model):
def set_unauthUsersBool(self, value): def set_unauthUsersBool(self, value):
self.unauth_users = value self.unauth_users = value
db.session.commit()
return self.unauth_users return self.unauth_users

View File

@ -26,7 +26,6 @@ class UnauthenticatedBans(db.Model):
def liftBan(self, lifter_id): def liftBan(self, lifter_id):
self.lifter_id = lifter_id self.lifter_id = lifter_id
db.session.commit()
return self.lifter_id return self.lifter_id
def __repr__(self): def __repr__(self):

View File

@ -30,10 +30,8 @@ class UnauthenticatedUsers(db.Model):
def changeUsername(self, username): def changeUsername(self, username):
self.username = username self.username = username
db.session.commit()
return self.username return self.username
def revokeUser(self): def revokeUser(self):
self.revoked = True self.revoked = True
db.session.commit()
return self.revoked return self.revoked

View File

@ -142,7 +142,6 @@ 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,