2017-02-10 06:10:44 +01:00
|
|
|
from functools import wraps
|
2018-01-05 09:52:22 +01:00
|
|
|
from flask import url_for, redirect, session, jsonify, abort, request
|
|
|
|
from titanembeds.database import list_disabled_guilds
|
2017-02-10 06:10:44 +01:00
|
|
|
|
|
|
|
def valid_session_required(api=False):
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if 'unauthenticated' not in session or 'user_id' not in session or 'username' not in session:
|
|
|
|
if api:
|
2017-04-04 07:53:27 +02:00
|
|
|
return jsonify(error=True, message="Unauthenticated session"), 401
|
2017-03-25 08:52:56 +01:00
|
|
|
redirect(url_for('user.logout'))
|
2017-02-10 06:10:44 +01:00
|
|
|
if session['unauthenticated'] and 'user_keys' not in session:
|
|
|
|
session['user_keys'] = {}
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|
|
|
|
return decorator
|
2017-03-25 08:52:56 +01:00
|
|
|
|
|
|
|
def discord_users_only(api=False):
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if 'unauthenticated' not in session or session['unauthenticated']:
|
|
|
|
if api:
|
2017-04-04 07:53:27 +02:00
|
|
|
return jsonify(error=True, message="Not logged in as a discord user"), 401
|
2017-03-25 08:52:56 +01:00
|
|
|
return redirect(url_for("user.login_authenticated"))
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|
|
|
|
return decorator
|
2018-01-05 09:52:22 +01:00
|
|
|
|
2018-08-21 02:43:23 +02:00
|
|
|
def abort_if_guild_disabled(*args):
|
2018-01-05 09:52:22 +01:00
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
guild_id = request.args.get("guild_id", None)
|
2018-08-21 02:43:23 +02:00
|
|
|
if not guild_id and len(args) > 0:
|
|
|
|
guild_id = args[0]
|
2018-01-05 09:52:22 +01:00
|
|
|
if guild_id in list_disabled_guilds():
|
|
|
|
return ('', 423)
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|
2019-01-01 20:24:16 +01:00
|
|
|
return decorator
|
|
|
|
|
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
from config import config
|
|
|
|
|
|
|
|
logger = logging.getLogger('myapp')
|
|
|
|
hdlr = logging.FileHandler(config["app-location"] + "/timeit.log")
|
|
|
|
formatter = logging.Formatter('%(asctime)s %(message)s')
|
|
|
|
hdlr.setFormatter(formatter)
|
|
|
|
logger.addHandler(hdlr)
|
|
|
|
logger.setLevel(logging.CRITICAL)
|
|
|
|
|
|
|
|
def timeit(method):
|
|
|
|
def timed(*args, **kw):
|
|
|
|
ts = time.time()
|
|
|
|
result = method(*args, **kw)
|
|
|
|
te = time.time()
|
|
|
|
if 'log_time' in kw:
|
|
|
|
name = kw.get('log_name', method.__name__.upper())
|
|
|
|
kw['log_time'][name] = int((te - ts) * 1000)
|
|
|
|
else:
|
|
|
|
# print('%r %2.2f ms' % \
|
|
|
|
# (method.__name__, (te - ts) * 1000))
|
|
|
|
logger.critical('%r %2.2f ms' % \
|
|
|
|
(method.__name__, (te - ts) * 1000) + " " + str(session) + " " + str(request))
|
|
|
|
return result
|
|
|
|
return timed
|