def dashboard(): if not discord.authorized: return redirect(url_for('oauth')) user = discord.get('api/users/@me').json() session['user'] = user['id'] return render_template('dashboard.html', title='Dashboard')
def charge(id): if not discord.authorized: # force oauth to access page return redirect(url_for('discord.login')) user = discord.get('api/users/@me').json() print(request.form) customer = stripe.Customer.create(email=user['email'], source=request.form['stripeToken']) charge = stripe.Subscription.create(customer=customer.id, items=[{ 'plan': id }]) return render_template('charge.html')
def favorites(): user_id = session.get('user') or discord.get('api/users/@me').json().get( 'user') if user_id is None: abort(401) else: if request.method == 'GET': user_favorites = db.session.query(Favorites).join(Sound).filter( Favorites.user_id == user_id) return jsonify({ 'sounds': [ Sound.query.get(fav.sound_id).to_dict() for fav in user_favorites ] }) elif (sound_id := request.json.get('sound_id')) is not None: if request.method == 'DELETE': q = db.session.query(Favorites) \ .filter_by(user_id=int(user_id), sound_id=sound_id) \ .delete(synchronize_session='fetch') db.session.commit() return '', 201 else: # method is POST f = db.session.query(Favorites) \ .filter(Favorites.user_id == user_id) \ .filter(Favorites.sound_id == sound_id) if f.first() is None: f = Favorites(user_id=user_id, sound_id=sound_id) db.session.add(f) db.session.commit() return '', 201 else:
def get_sound(): if (sound_id := request.args.get('sound_id')) is not None: try: user_id = session.get('user') or discord.get( 'api/users/@me').json().get('user') except: user_id = None sound = Sound.query.get(sound_id) if sound is not None: if sound.public or sound.uploader_id == user_id: return jsonify(sound.to_full_dict()) else: abort(403) else: abort(404)
def index(): if not discord.authorized: # force oauth to access page return redirect(url_for('discord.login')) discord_user = discord.get('api/users/@me').json() # grab user if SimpleUser.query.filter_by(email=discord_user['email']).first( ) is None: # check if the user is registered already user = SimpleUser(user_id=discord_user['id'], email=discord_user['email'], username=discord_user['username'] ) # if not make a simple log of their details db.session.add(user) # add to DB db.session.commit() # commit to the DB return render_template('index.html', key=app.config['STRIPE_PUBLIC'], plans=app.config['PLANS'], email=discord_user['email'])
def user_sounds(): user_id = session.get('user') or discord.get('api/users/@me').json().get( 'user') if user_id is None: abort(401) else: if request.method == 'DELETE': if (sound_id := request.args.get('sound_id')) is not None: Sound.query \ .filter(Sound.uploader_id == user_id) \ .filter(Sound.id == sound_id) \ .delete(synchronize_session='fetch') db.session.commit() return '', 201 else: abort(400) else:
def dashboard(): if not discord.authorized: return redirect(url_for('oauth')) if request.method == 'POST': for index in range(len(session.get('reminders'))): try: reminder_rewrite = [x for x in session.get('reminders') if x['index'] == index][0] except IndexError: return '400 Bad Request' if request.form.get('delete{}'.format(index)) is not None: with sqlite3.connect(base_dir + 'DATA/calendar.db') as connection: cursor = connection.cursor() cursor.execute('DELETE FROM reminders WHERE channel = ? AND message = ? AND time = ?', (reminder_rewrite['channel']['id'], reminder_rewrite['message'], reminder_rewrite['time'][0])) elif request.form.get('message{}'.format(index)) != reminder_rewrite['message']: with sqlite3.connect(base_dir + 'DATA/calendar.db') as connection: cursor = connection.cursor() cursor.execute('UPDATE reminders SET message = ? WHERE channel = ? AND message = ? AND time = ?', (request.form.get('message{}'.format(index)), reminder_rewrite['channel']['id'], reminder_rewrite['message'], reminder_rewrite['time'][0])) try: session.pop('reminders') except KeyError: pass return redirect(url_for('dashboard', id=request.args.get('id'))) else: reminders = [] if request.args.get('refresh') == '1': session.pop('guilds') return redirect(url_for('dashboard')) if session.get('guilds') is None: # the code below is time-consuming; only run on first load and if the user wants to refresh the guild list. user = discord.get('api/users/@me').json() guilds = discord.get('api/users/@me/guilds').json() user_id = user['id'] available_guilds = [] with sqlite3.connect(base_dir + '/DATA/calendar.db') as connection: cursor = connection.cursor() cursor.row_factory = sqlite3.Row for guild in guilds: idx = guild['id'] command = 'SELECT restrictions FROM servers WHERE id = ?' cursor.execute(command, (idx,)) restrictions = cursor.fetchone() if restrictions is None: continue elif (guild['permissions'] & 0x00002000) or (guild['permissions'] & 0x00000020) or (guild['permissions'] & 0x00000008): available_guilds.append(guild) continue elif json.loads(dict(restrictions)['restrictions']) == []: continue member = requests.get('https://discordapp.com/api/v6/guilds/{}/members/{}'.format(idx, user_id), headers={'Authorization': 'Bot {}'.format(app.config['BOT_TOKEN'])}).json() for role in member['roles']: if int(role) in json.loads(dict(restrictions)['restrictions']): available_guilds.append(guild) break session['guilds'] = available_guilds if request.args.get('id') is not None: for guild in session['guilds']: if guild['id'] == request.args.get('id'): channels = requests.get('https://discordapp.com/api/v6/guilds/{}/channels'.format(guild['id']), headers={'Authorization': 'Bot {}'.format(app.config['BOT_TOKEN'])}).json() break else: return '403. Don\'t be naughty.' with sqlite3.connect(base_dir + '/DATA/calendar.db') as connection: cursor = connection.cursor() cursor.row_factory = sqlite3.Row command = 'SELECT * FROM reminders WHERE channel IN ({})'.format(','.join(['?'] * len(channels))) cursor.execute(command, [int(x['id']) for x in channels]) reminders = [dict(x) for x in cursor.fetchall()] index = 0 for reminder in reminders: reminder['index'] = index index += 1 channel = [x for x in channels if int(x['id']) == reminder['channel']][0] reminder['channel'] = channel reminder['time'] = [reminder['time'], datetime.fromtimestamp(reminder['time']).strftime('%d/%b/%Y %H:%M:%S')] session['reminders'] = reminders return render_template('dashboard.html', guilds=session['guilds'], reminders=session['reminders'])
def oauth(): if not discord.authorized: return redirect(url_for('discord.login')) resp = discord.get('api/users/@me') return redirect(url_for('index'))
from flask import session, abort from app import app, discord, db from app.models import User, Channel def get_internal_id(): if (internal_id := session.get('internal_id')) is not None: return internal_id else: user_id = session.get('user_id') user_name = session.get('user_name') if user_id is None: user = discord.get('api/users/@me').json() try: user_id = int(user['id']) user_name = '{}#{}'.format(user['username'], user['discriminator']) except KeyError: return abort(401) else: session['user_id'] = user_id session['user_name'] = user_name user_record = User.query.filter(User.user == user_id).first()