-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
90 lines (63 loc) · 1.92 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
from flask_bcrypt import Bcrypt
from flask_sqlalchemy import SQLAlchemy
bcrypt = Bcrypt()
db = SQLAlchemy()
class Cocktails(db.Model):
'''Cocktails'''
__tablename__ = 'cocktails'
name = db.Column(db.Text, )
image = db.Column(db.Text, )
instructions = db.Column(db.Text, )
ingredients = db.Column(db.Text, )
measurements = db.Column(db.Text, )
'''' USER CLASS'''
class User(db.Model):
"""User in the system."""
__tablename__ = 'users'
id = db.Column(
db.Integer,
primary_key=True,
)
email = db.Column(
db.Text,
nullable=False,
unique=True,
)
username = db.Column(
db.Text,
nullable=False,
unique=True,
)
password = db.Column(
db.Text,
nullable=False,
)
def __repr__(self):
return f"<User #{self.id}: {self.username}, {self.email}>"
@classmethod
def signup(cls, username, email, password):
"""Sign up user.
Hashes password and adds user to system.
"""
hashed_pwd = bcrypt.generate_password_hash(password).decode('UTF-8')
user = User(
username=username,
email=email,
password=hashed_pwd,
)
db.session.add(user)
return user
@classmethod
def authenticate(cls, username, password):
"""Find user with `username` and `password`.
This is a class method (call it on the class, not an individual user.)
It searches for a user whose password hash matches this password
and, if it finds such a user, returns that user object.
If can't find matching user (or if password is wrong), returns 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