Replace discordrest from user and utils

This commit is contained in:
Jeremy Zhang 2017-05-06 18:36:45 -07:00
parent 4fc0af2d80
commit 5b3bdaac38
2 changed files with 6 additions and 15 deletions

View File

@ -1,7 +1,6 @@
from flask import Blueprint, request, redirect, jsonify, abort, session, url_for, render_template from flask import Blueprint, request, redirect, jsonify, abort, session, url_for, render_template
from config import config from config import config
from titanembeds.decorators import discord_users_only from titanembeds.decorators import discord_users_only
from titanembeds.utils import discord_api
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans
from titanembeds.oauth import authorize_url, token_url, make_authenticated_session, get_current_authenticated_user, get_user_managed_servers, check_user_can_administrate_guild, check_user_permission, generate_avatar_url, generate_guild_icon_url, generate_bot_invite_url from titanembeds.oauth import authorize_url, token_url, make_authenticated_session, get_current_authenticated_user, get_user_managed_servers, check_user_can_administrate_guild, check_user_permission, generate_avatar_url, generate_guild_icon_url, generate_bot_invite_url
import time import time
@ -74,16 +73,11 @@ def dashboard():
def administrate_guild(guild_id): def administrate_guild(guild_id):
if not check_user_can_administrate_guild(guild_id): if not check_user_can_administrate_guild(guild_id):
return redirect(url_for("user.dashboard")) return redirect(url_for("user.dashboard"))
guild = discord_api.get_guild(guild_id) db_guild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
if guild['code'] != 200: if not db_guild:
session["redirect"] = url_for("user.administrate_guild", guild_id=guild_id, _external=True) session["redirect"] = url_for("user.administrate_guild", guild_id=guild_id, _external=True)
return redirect(generate_bot_invite_url(guild_id)) return redirect(generate_bot_invite_url(guild_id))
session["redirect"] = None session["redirect"] = None
db_guild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
if not db_guild:
db_guild = Guilds(guild_id)
db.session.add(db_guild)
db.session.commit()
permissions=[] permissions=[]
if check_user_permission(guild_id, 5): if check_user_permission(guild_id, 5):
permissions.append("Manage Embed Settings") permissions.append("Manage Embed Settings")
@ -95,18 +89,15 @@ def administrate_guild(guild_id):
all_bans = db.session.query(UnauthenticatedBans).filter(UnauthenticatedBans.guild_id == guild_id).all() all_bans = db.session.query(UnauthenticatedBans).filter(UnauthenticatedBans.guild_id == guild_id).all()
users = prepare_guild_members_list(all_members, all_bans) users = prepare_guild_members_list(all_members, all_bans)
dbguild_dict = {"unauth_users": db_guild.unauth_users} dbguild_dict = {"unauth_users": db_guild.unauth_users}
return render_template("administrate_guild.html.j2", guild=guild['content'], dbguild=dbguild_dict, members=users, permissions=permissions) return render_template("administrate_guild.html.j2", guild=db_guild, dbguild=dbguild_dict, members=users, permissions=permissions)
@user.route("/administrate_guild/<guild_id>", methods=["POST"]) @user.route("/administrate_guild/<guild_id>", methods=["POST"])
@discord_users_only() @discord_users_only()
def update_administrate_guild(guild_id): def update_administrate_guild(guild_id):
if not check_user_can_administrate_guild(guild_id): if not check_user_can_administrate_guild(guild_id):
abort(403) abort(403)
guild = discord_api.get_guild(guild_id)
if guild['code'] != 200:
abort(guild['code'])
db_guild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() db_guild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
if db_guild is None: if not db_guild:
abort(400) abort(400)
db_guild.unauth_users = request.form.get("unauth_users", db_guild.unauth_users) in ["true", True] db_guild.unauth_users = request.form.get("unauth_users", db_guild.unauth_users) in ["true", True]
db.session.commit() db.session.commit()

View File

@ -67,8 +67,8 @@ def check_guild_existance(guild_id):
dbGuild = Guilds.query.filter_by(guild_id=guild_id).first() dbGuild = Guilds.query.filter_by(guild_id=guild_id).first()
if not dbGuild: if not dbGuild:
return False return False
guild = discord_api.get_guild(guild_id) else:
return guild['code'] == 200 return True
def guild_query_unauth_users_bool(guild_id): def guild_query_unauth_users_bool(guild_id):
dbGuild = db.session.query(Guilds).filter(Guilds.guild_id==guild_id).first() dbGuild = db.session.query(Guilds).filter(Guilds.guild_id==guild_id).first()