-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
109 lines (87 loc) · 3.52 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from datetime import datetime
from Crypto.Protocol.KDF import scrypt
from Crypto.Random import get_random_bytes
from flask_login import UserMixin
from app import db
from werkzeug.security import generate_password_hash
import base64
from cryptography.fernet import Fernet
# ENCRYPTION
def encrypt(data, draw_key):
return Fernet(draw_key).encrypt(bytes(data, 'utf-8'))
def decrypt(data, draw_key):
return Fernet(draw_key).decrypt(data).decode("utf-8")
class User(db.Model, UserMixin):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
# User authentication information.
email = db.Column(db.String(100), nullable=False, unique=True)
password = db.Column(db.String(100), nullable=False)
pin_key = db.Column(db.String(100), nullable=False)
# User activity information
registered_on = db.Column(db.DateTime, nullable=True)
last_logged_in = db.Column(db.DateTime, nullable=True)
current_logged_in = db.Column(db.DateTime, nullable=True)
# User information
firstname = db.Column(db.String(100), nullable=False)
lastname = db.Column(db.String(100), nullable=False)
phone = db.Column(db.String(100), nullable=False)
role = db.Column(db.String(100), nullable=False, default='user')
# crypto key for user's lottery draws
draw_key = db.Column(db.BLOB)
# Define the relationship to Draw
draws = db.relationship('Draw')
def __init__(self, email, firstname, lastname, phone, password, pin_key, role):
self.email = email
self.firstname = firstname
self.lastname = lastname
self.phone = phone
self.password = generate_password_hash(password)
self.pin_key = pin_key
self.draw_key = base64.urlsafe_b64encode(scrypt(password, str(get_random_bytes(32)), 32, N=2 ** 14, r=8, p=1))
self.role = role
self.registered_on = datetime.now()
self.last_logged_in = None
self.current_logged_in = None
class Draw(db.Model):
__tablename__ = 'draws'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False)
draw = db.Column(db.String(100), nullable=False)
played = db.Column(db.BOOLEAN, nullable=False, default=False)
match = db.Column(db.BOOLEAN, nullable=False, default=False)
win = db.Column(db.BOOLEAN, nullable=False)
round = db.Column(db.Integer, nullable=False, default=0)
def __init__(self, user_id, draw, win, round, draw_key):
self.user_id = user_id
self.draw = encrypt(draw, draw_key)
self.played = False
self.match = False
self.win = win
self.round = round
def update_draw(self, draw, draw_key):
self.draw = encrypt(draw, draw_key)
db.session.commit()
def view_draw(self, draw_key):
return decrypt(self.draw, draw_key)
def init_db():
db.drop_all()
db.create_all()
# Testing users
admin = User(email='[email protected]',
password='Admin1!',
pin_key='BFB5S34STBLZCOB22K6PPYDCMZMH46OJ',
firstname='Alice',
lastname='Jones',
phone='0191-123-4567',
role='admin')
user = User(email='[email protected]',
password='User1!',
pin_key='BFB5S34STBLZCOB22K6PPYDCMZMH46OJ',
firstname='Bob',
lastname='Billy',
phone='0191-123-4567',
role='user')
db.session.add(admin)
db.session.add(user)
db.session.commit()