-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
151 lines (120 loc) · 3.58 KB
/
models.py
File metadata and controls
151 lines (120 loc) · 3.58 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""SQLAlchemy models for Flash App."""
import os
from datetime import datetime, timedelta
import jwt
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
bcrypt = Bcrypt()
db = SQLAlchemy()
SECRET_KEY = os.environ.get('SECRET_KEY', "it's a secret")
##############################################################################
# Card model
class Card(db.Model):
"""A Card"""
__tablename__ = 'cards'
id = db.Column(
db.Integer,
primary_key=True,
)
word = db.Column(
db.String(25),
nullable=False,
)
defn = db.Column(
db.Text,
nullable=False,
)
bin = db.Column(
db.Integer,
default=0,
nullable=False,
)
wrongs = db.Column(
db.Integer,
default=0,
nullable=False,
)
due = db.Column(
db.DateTime(),
default=datetime.now(),
nullable=False,
)
user_id = db.Column(
db.Integer,
db.ForeignKey('users.id', ondelete='CASCADE'),
nullable=False,
)
def reset_due(self):
"""Reset due date of card by current date and bin"""
current_time = datetime.now()
if self.bin == 1:
return current_time + timedelta(seconds=5)
if self.bin == 2:
return current_time + timedelta(seconds=25)
if self.bin == 3:
return current_time + timedelta(minutes=2)
if self.bin == 4:
return current_time + timedelta(minutes=5)
if self.bin == 5:
return current_time + timedelta(minutes=10)
if self.bin == 6:
return current_time + timedelta(hours=1)
if self.bin == 7:
return current_time + timedelta(hours=5)
if self.bin == 8:
return current_time + timedelta(days=1)
if self.bin == 9:
return current_time + timedelta(days=5)
if self.bin == 10:
return current_time + timedelta(days=25)
if self.bin >= 11:
return current_time + timedelta(days=120)
##############################################################################
# User model
class User(db.Model):
"""A user"""
__tablename__ = 'users'
id = db.Column(
db.Integer,
primary_key=True,
)
username = db.Column(
db.String(25),
nullable=False,
)
password = db.Column(
db.Text,
default=False,
nullable=False,
)
is_admin = db.Column(
db.Boolean,
nullable=False,
)
def generate_token(self, user_id, is_admin):
"""Creates JSON Web Token"""
payload = {'userId': user_id, 'isAdmin': is_admin}
return jwt.encode(payload, SECRET_KEY, algorithm='HS256')
@classmethod
def sign_up(cls, username, password):
"""Signs up user. Hashes password and adds user to database"""
try:
hashed_pwd = bcrypt.generate_password_hash(password).decode('UTF-8')
user = User(username=username, password=hashed_pwd, is_admin=False)
db.session.add(user)
return user
except Exception as e:
return e
@classmethod
def authenticate(cls, username, password):
"""Find user by username and password; otherwise return False."""
user = cls.query.filter_by(username=username).first()
if user:
is_auth = bcrypt.check_password_hash(user.password, password)
if is_auth:
return user
return False
def connect_db(app):
"""Connect database to app."""
db.app = app
db.init_app(app)