[Python] 纯文本查看 复制代码
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from werkzeug.security import check_password_hash
import pymysql, os
from flask import jsonify
app = Flask(__name__)
# 数据库配置
HOSTNAME = 'localhost'
PORT = '3306'
DATABASE = 'movic'
USERNAME = 'root'
PASSWORD = 'root'
app.config["SQLALCHEMY_DATABASE_URI"] = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8'.format(USERNAME, PASSWORD, HOSTNAME, PORT, DATABASE)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
app.config["SECRET_KEY"] = os.urandom(24)
db = SQLAlchemy(app)
# 会员
class User(db.Model):
global pwd
__tablename__ = "user"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(100), unique=True)
pwd = db.Column(db.String(510))
addtime = db.Column(db.DateTime, index=True, default=datetime.now)
userlogs = db.relationship("UserLog", backref="user") # 会员日志外键关联
def __repr__(self):
return "<User %r>" % self.name
def check_pwd(self, pwd):
return check_password_hash(self.pwd, pwd)
class UserLog(db.Model):
"""
会员登录日志表
"""
__tablename__ = "userlog" # 表名
id = db.Column(db.Integer, primary_key=True, autoincrement=True) # 编号
user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # 所属会员
ip = db.Column(db.String(100)) # 登录IP
addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 登录时间;
def __repr__(self):
return "<UserLog %r>" % self.id
if __name__ == '__main__':
db.drop_all()
db.create_all()
[mw_shl_code=asm,true]from . import home
from flask import render_template, redirect, url_for, flash, session, request
from app.home.forms import RegistForm, LoginForm
from app.models import User, db, UserLog
from datetime import datetime
from werkzeug.security import generate_password_hash
from functools import wraps
from werkzeug.security import check_password_hash
# 登录装饰器
def user_login_req(func):
@wraps(func)
def decorated_function(*args, **kwargs):
if "user" not in session:
return redirect(url_for("home.login"))
return func(*args, **kwargs)
return decorated_function
# 前端首页
@home.route("/")
def index():
return render_template("home/index.html")
# 用户中心
@home.route("/user")
@user_login_req
def user():
return render_template("home/user.html")
# 用户登录
@home.route("/login", methods=["GET", "POST"])
def login():
# global pwd
form = LoginForm()
if form.validate_on_submit():
data = form.data
user = User.query.filter_by(name = data["name"]).first()
if user.check_pwd(data['pwd']):
userlog = UserLog(
user_id=user.id,
ip=request.remote_addr
)
db.session.add(userlog)
db.session.commit()
session["name"] = user.name
return redirect(url_for("home.user"))
return render_template("home/login.html", form=form)
# 退出登录
@home.route("/logout",methods=["GET", "POST"])
def logout():
form = LoginForm()
if 'name' in session:
session.clear()
return redirect(url_for("home.login"))
return render_template("home/login.html", form=form)
# 会员注册
@home.route("/register", methods=["GET", "POST"])
def register():
form = RegistForm()
if form.validate_on_submit():
data = form.data
user = User(
name=data["name"],
pwd=generate_password_hash(data["pwd"]),
addtime=datetime.now()
)
db.session.add(user)
db.session.commit()
flash("注册成功", "ok")
return render_template("home/register.html", form=form)
[mw_shl_code=python,true]
[/mw_shl_code][/mw_shl_code]