From 57e09be6004b8b6c5b36d98e37ad5200eba854a4 Mon Sep 17 00:00:00 2001 From: James Wang Date: Tue, 22 Dec 2015 22:44:51 -0500 Subject: [PATCH] Add utils.py --- server/api/utils.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 server/api/utils.py diff --git a/server/api/utils.py b/server/api/utils.py new file mode 100644 index 0000000..28127e5 --- /dev/null +++ b/server/api/utils.py @@ -0,0 +1,33 @@ +import datetime +import MySQLdb +import random +import string + +from werkzeug.security import generate_password_hash, check_password_hash + +host = "localhost" +user = "root" + +conn = MySQLdb.connect() + +def get_connection(): + global conn + if not conn: + conn = MySQLdb.connect(host=host, user=user) + return conn + +def hash_password(s): + return generate_password_hash(s) + +def check_password(hashed_password, try_password): + return check_password_hash(hashed_password, try_password) + +def generate_string(length): + return "".join([random.choice(string.letters + string.digits) for x in range(length)]) + +def unix_time_millis(dt): + epoch = datetime.datetime.utcfromtimestamp(0) + return (dt - epoch).total_seconds() * 1000.0 + +def get_time_since_epoch(): + return unix_time_millis(datetime.datetime.now())