-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
59 lines (40 loc) · 1.67 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
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
db = SQLAlchemy()
bcrypt = Bcrypt()
def connect_db(app):
db.app = app
db.init_app(app)
class User(db.Model):
__tablename__ = 'users'
def __repr__(self):
return f'<{self.username}, id: {self.id}>'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(20), nullable=False, unique=True)
password = db.Column(db.Text, nullable=False)
recipes = db.relationship('SavedRecipe', cascade='all, delete', backref='users')
@classmethod
def register(cls, username, password):
"""Register user w/hashed password & return user."""
hashed = bcrypt.generate_password_hash(password)
# turn bytestring into normal (unicode utf8) string
hashed_utf8 = hashed.decode("utf8")
# return instance of user w/username and hashed password
return cls(username=username, password=hashed_utf8)
@classmethod
def authenticate(cls, username, password):
"""Validate that user exists & password is correct.
Return user if valid; else return False.
"""
user = User.query.filter_by(username=username).first()
if user and bcrypt.check_password_hash(user.password, password):
return user
else:
return False
class SavedRecipe(db.Model):
__tablename__ = 'saved_recipes'
def __repr__(self):
return f'<SavedRecipe user_id: {self.user_id}, recipe_id: {self.recipe_id}>'
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
recipe_id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text)