2019-10-06 05:40:02 +00:00
|
|
|
from database import db, ma
|
2019-10-13 17:15:03 +00:00
|
|
|
from marshmallow import fields, Schema
|
2019-10-06 05:40:02 +00:00
|
|
|
from app import bcrypt
|
|
|
|
from configuration import config
|
2019-10-04 18:24:11 +00:00
|
|
|
import datetime
|
2019-10-02 20:55:09 +00:00
|
|
|
import enum
|
2019-10-06 20:44:38 +00:00
|
|
|
import json
|
2019-10-05 06:25:01 +00:00
|
|
|
import jwt
|
2019-10-06 22:39:22 +00:00
|
|
|
import os
|
2019-10-02 20:55:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class User(db.Model):
|
|
|
|
__tablename__ = "users"
|
|
|
|
|
2019-10-10 21:57:29 +00:00
|
|
|
class Ranks(enum.Enum): # with minimal Elo rating
|
|
|
|
D7 = "Seven Dan" # Elo 2700+
|
|
|
|
D6 = "Six Dan"
|
|
|
|
D5 = "Five Dan" # Elo 2500
|
|
|
|
D4 = "Four Dan"
|
|
|
|
D3 = "Three Dan"
|
|
|
|
D2 = "Two Dan"
|
|
|
|
D1 = "One Dan"
|
|
|
|
K1 = "One Kyu" # Elo 2000
|
|
|
|
K2 = "Two Kyu"
|
|
|
|
K3 = "Three Kyu"
|
|
|
|
K4 = "Four Kyu"
|
|
|
|
K5 = "Five Kyu"
|
|
|
|
K6 = "Six Kyu" # Elo 1500
|
|
|
|
K7 = "Seven Kyu"
|
|
|
|
K8 = "Eight Kyu"
|
|
|
|
K9 = "Nine Kyu"
|
|
|
|
K10 = "Ten Kyu"
|
|
|
|
K11 = "Eleven Kyu" # ELo 1000
|
|
|
|
K12 = "Twelve Kyu"
|
|
|
|
K13 = "Thirteen Kyu"
|
|
|
|
K14 = "Fourteen Kyu"
|
|
|
|
K15 = "Fifteen Kyu"
|
|
|
|
K16 = "Sixteen Kyu" # Elo 500
|
|
|
|
K17 = "Seventeen Kyu"
|
|
|
|
K18 = "Eighteen Kyu"
|
|
|
|
K19 = "Nineteen Kyu"
|
|
|
|
K20 = "Twenty Kyu"
|
|
|
|
K21 = "Twenty-One Kyu" # Elo 0
|
|
|
|
K22 = "Twenty-Two Kyu"
|
|
|
|
K23 = "Twenty-Three Kyu"
|
|
|
|
K24 = "Twenty-Four Kyu"
|
|
|
|
K25 = "Twenty-Five Kyu"
|
|
|
|
K26 = "Twenty-Six Kyu" # Elo -500
|
|
|
|
K27 = "Twenty-Seven Kyu"
|
|
|
|
K28 = "Twenty-Eight Kyu"
|
|
|
|
K29 = "Twenty-Nine Kyu"
|
|
|
|
K30 = "Thirty Kyu" # Elo -900
|
|
|
|
UR = "Unknown Rank"
|
|
|
|
|
2019-10-02 20:55:09 +00:00
|
|
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
2019-10-06 06:57:36 +00:00
|
|
|
username = db.Column(db.String(255), unique=True, nullable=False, autoincrement=True)
|
2019-10-02 20:55:09 +00:00
|
|
|
email = db.Column(db.String(255), unique=True, nullable=False)
|
2019-10-06 05:40:02 +00:00
|
|
|
password = db.Column(db.String(255), nullable=False)
|
2019-10-02 20:55:09 +00:00
|
|
|
registered_on = db.Column(db.DateTime, nullable=False)
|
|
|
|
admin = db.Column(db.Boolean, nullable=False, default=False)
|
|
|
|
rank = db.Column(db.Enum(Ranks))
|
|
|
|
elo = db.Column(db.Integer)
|
|
|
|
rank_certainty = db.Column(db.Boolean, nullable=False, default=False)
|
|
|
|
|
2019-10-11 00:41:49 +00:00
|
|
|
|
2019-10-13 17:15:03 +00:00
|
|
|
def __init__(self, username, email, password, rank=Ranks.UR, admin=False):
|
2019-10-06 06:57:36 +00:00
|
|
|
self.username = username
|
2019-10-04 18:24:11 +00:00
|
|
|
self.email = email
|
2019-10-02 20:55:09 +00:00
|
|
|
self.password = bcrypt.generate_password_hash(
|
2019-10-06 05:40:02 +00:00
|
|
|
password, 13
|
2019-10-02 20:55:09 +00:00
|
|
|
).decode()
|
2019-10-06 06:57:36 +00:00
|
|
|
self.rank = rank
|
2019-10-04 18:24:11 +00:00
|
|
|
self.registered_on = datetime.datetime.now()
|
|
|
|
self.admin = admin
|
2019-10-05 06:25:01 +00:00
|
|
|
|
|
|
|
def encode_auth_token(self, user_id):
|
2019-10-05 06:29:43 +00:00
|
|
|
"""
|
|
|
|
Generates the Auth Token
|
|
|
|
:return: string
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
payload = {
|
2019-10-07 05:51:36 +00:00
|
|
|
'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=4),
|
2019-10-05 06:29:43 +00:00
|
|
|
'iat': datetime.datetime.utcnow(),
|
2019-10-07 05:51:36 +00:00
|
|
|
'user': user_schema.dumps(self)
|
2019-10-05 06:29:43 +00:00
|
|
|
}
|
|
|
|
return jwt.encode(
|
|
|
|
payload,
|
2019-10-06 22:39:22 +00:00
|
|
|
os.environ.get('SECRET_KEY'),
|
2019-10-05 06:29:43 +00:00
|
|
|
algorithm='HS256'
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
return e
|
2019-10-05 06:25:01 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def decode_auth_token(auth_token):
|
|
|
|
"""
|
|
|
|
Decodes the auth token
|
|
|
|
:param auth_token:
|
|
|
|
:return: integer|string
|
|
|
|
"""
|
|
|
|
try:
|
2019-10-06 22:39:22 +00:00
|
|
|
payload = jwt.decode(auth_token, os.environ.get('SECRET_KEY'))
|
2019-10-07 05:51:36 +00:00
|
|
|
return payload['user']
|
2019-10-05 06:25:01 +00:00
|
|
|
except jwt.ExpiredSignatureError:
|
|
|
|
return 'Signature expired. Please log in again.'
|
|
|
|
except jwt.InvalidTokenError:
|
|
|
|
return 'Invalid token. Please log in again.'
|
2019-10-06 05:40:02 +00:00
|
|
|
|
2019-10-13 17:15:03 +00:00
|
|
|
class UserSchema(Schema):
|
2019-10-06 20:44:38 +00:00
|
|
|
id = fields.Int()
|
|
|
|
username = fields.Str()
|
|
|
|
email = fields.Str()
|
|
|
|
registered_on = fields.Date()
|
|
|
|
rank = fields.Str()
|
|
|
|
rank_certainty = fields.Bool()
|
|
|
|
elo = fields.Int()
|
|
|
|
|
2019-10-13 17:15:03 +00:00
|
|
|
class Meta:
|
|
|
|
register=True
|
2019-10-06 05:40:02 +00:00
|
|
|
|
|
|
|
user_schema = UserSchema()
|
|
|
|
users_schema = UserSchema(many=True)
|