-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
48 lines (34 loc) · 1.22 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
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
email = db.Column(db.String)
password = db.Column(db.String)
def __repr__(self):
return f"<User {self.id} | {self.name}>"
def save(self):
db.session.add(self)
db.session.commit()
print(f"User {self.id} saved")
def create_password(self, password):
self.password = generate_password_hash(password)
def is_valid_password(self, password):
return check_password_hash(self.password, password)
def change_password(self, password):
if self.is_valid_password(password):
self.create_password(password)
self.save()
def connect_to_db(app):
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://localhost/flask_auth"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
db.init_app(app)
if __name__ == '__main__':
# Used only to connect to app database.
app = Flask(__name__)
connect_to_db(app)
db.create_all()