parent
ffb8fe1f4f
commit
bf105f7048
@ -1,5 +1,15 @@ |
|||||||
from flask import Flask |
from flask import Flask |
||||||
|
from config import Config |
||||||
|
from flask_sqlalchemy import SQLAlchemy |
||||||
|
from flask_migrate import Migrate |
||||||
|
from flask_login import LoginManager |
||||||
|
|
||||||
app = Flask(__name__) |
app = Flask(__name__) |
||||||
|
app.config.from_object(Config) |
||||||
|
login = LoginManager(app) |
||||||
|
db = SQLAlchemy(app) |
||||||
|
migrate = Migrate(app, db) |
||||||
|
login = LoginManager(app) |
||||||
|
login.login_view = 'login' |
||||||
|
|
||||||
from app import routes |
from app import routes |
@ -1,9 +1,27 @@ |
|||||||
|
from datetime import datetime |
||||||
|
from app import db |
||||||
from werkzeug.security import generate_password_hash, check_password_hash |
from werkzeug.security import generate_password_hash, check_password_hash |
||||||
|
from flask_login import UserMixin |
||||||
|
from app import login |
||||||
|
|
||||||
|
|
||||||
class User(db.Model): |
class User(UserMixin, db.Model): |
||||||
|
id = db.Column(db.Integer, primary_key=True) |
||||||
|
username = db.Column(db.String(64), index=True, unique=True) |
||||||
|
email = db.Column(db.String(120), index=True, unique=True) |
||||||
|
password_hash = db.Column(db.String(128)) |
||||||
|
# posts = db.relationship('Post', backref='author', lazy='dynamic') |
||||||
|
|
||||||
|
def __repr__(self): |
||||||
|
return '<User {}>'.format(self.username) |
||||||
|
|
||||||
def set_password(self, password): |
def set_password(self, password): |
||||||
self.password_hash = generate_password_hash(password) |
self.password_hash = generate_password_hash(password) |
||||||
|
|
||||||
def check_password(self, password): |
def check_password(self, password): |
||||||
return check_password_hash(self.password_hash, password) |
return check_password_hash(self.password_hash, password) |
||||||
|
|
||||||
|
|
||||||
|
@login.user_loader |
||||||
|
def load_user(id): |
||||||
|
return User.query.get(int(id)) |
@ -0,0 +1,9 @@ |
|||||||
|
import os |
||||||
|
basedir = os.path.abspath(os.path.dirname(__file__)) |
||||||
|
|
||||||
|
|
||||||
|
class Config(object): |
||||||
|
SECRET_KEY = os.environ.get('SECRET_KEY') or 'xxx' |
||||||
|
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \ |
||||||
|
'sqlite:///' + os.path.join(basedir, 'app.db') |
||||||
|
SQLALCHEMY_TRACK_MODIFICATIONS = False |
@ -0,0 +1 @@ |
|||||||
|
from app import app |
Loading…
Reference in new issue