九十一:CMS系统之cms用户模型定义
数据库信息
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:123456@127.0.0.1:3306/test'
SQLALCHEMY_TRACK_MODIFICATIONS = False
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
from datetime import datetime
from exts import db
class CMSUser(db.Model):
__tablename__ = 'cms_user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(50), nullable=False, comment='用户名')
password = db.Column(db.String(50), nullable=False, comment='密码')
email = db.Column(db.String(50), nullable=False, unique=True, comment='邮箱,唯一')
join_time = db.Column(db.DateTime, default=datetime.now, comment='加入时间')
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from cms import app
from exts import db
from apps.cms import models
manager = Manager(app)
Migrate(app, db)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
python manager.py db init
python manager.py db migrate
python manager.py db upgrade
from flask import Flask
from apps.cms import bp as cms_bp
from apps.common import bp as common_bp
from apps.front import bp as front_bp
import config
from exts import db
def create_app():
app = Flask(__name__)
app.config.from_object(config)
app.register_blueprint(cms_bp)
app.register_blueprint(common_bp)
app.register_blueprint(front_bp)
db.init_app(app)
return app
if __name__ == '__main__':
app = create_app()
app.run(host='0.0.0.0', port=8888)
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from cms import create_app
from exts import db
from apps.cms import models as cms_models
CMSUser = cms_models.CMSUser
app = create_app()
manager = Manager(app)
Migrate(app, db)
manager.add_command('db', MigrateCommand)
# 命令行添加用户
@manager.option('-u', '--username', dest='username')
@manager.option('-p', '--password', dest='password')
@manager.option('-e', '--email', dest='email')
def create_cms_user(username, password, email):
user = CMSUser(username=username, password=password, email=email)
db.session.add(user)
db.session.commit()
if __name__ == '__main__':
manager.run()
插入用户:python manager.py create_cms_user -u user1 -p 123456 -e 123@qq.com
密码加密
from datetime import datetime
from exts import db
from werkzeug.security import generate_password_hash, check_password_hash
class CMSUser(db.Model):
__tablename__ = 'cms_user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(50), nullable=False, comment='用户名')
_password = db.Column(db.String(100), nullable=False, comment='密码')
email = db.Column(db.String(50), nullable=False, unique=True, comment='邮箱,唯一')
join_time = db.Column(db.DateTime, default=datetime.now, comment='加入时间')
def __init__(self, username, password, email):
self.username = username
self.password = password
self.email = email
# 密码:对外的字段名:password、对内的字段名:__password
@property
def password(self):
""" 获取密码 """
return self._password
@password.setter
def password(self, row):
""" 设置加密密码 """
self._password = generate_password_hash(row)
def check_password(self, row):
""" 检查密码 """
return check_password_hash(self.password, row)
由于修改了字段名,这里执行数据库迁移
添加用户:python manager.py create_cms_user -u user1 -p 123456 -e 122@qq.com
讨论群:249728408