1 Star 0 Fork 3

看到我请让我滚去学习/VisualizationProject_1

forked from Yiman/VisualizationProject 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
app.py 4.55 KB
一键复制 编辑 原始数据 按行查看 历史
18000155784 提交于 2022-02-09 12:01 . 修复session的bug
import datetime
import redis
from flask import Flask, render_template, request, session, redirect, flash
from views.china import china
from views.checkin import stu
from views.manage import manage
from views.search import search
from views.world import world
from service.user_service import register, login, logout, send_verification
from enums.return_enum import ResType
from enums.role_enum import Roles
from settings import MAX_USERNAME_LENGTH, MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH
from settings import MIN_USERNAME_LENGTH
db = redis.Redis(host='localhost', decode_responses=True)
main = Flask(__name__)
# 设置session过期时间,设置为30分钟
main.permanent_session_lifetime = datetime.timedelta(seconds=60 * 30)
main.config['SECRET_KEY'] = 'jk12la1s2df21h'
main.config.update(DEBUG=True)
# 注册蓝图
main.register_blueprint(world)
main.register_blueprint(china)
main.register_blueprint(stu)
main.register_blueprint(manage)
main.register_blueprint(search)
# 拦截器
@main.before_first_request
def init_session():
session["login_user"] = ""
session["role"] = Roles.NOTHING.value
session["last_view"] = ""
# 主页
@main.route('/homePage')
def home_page():
if session.get("role") in [Roles.NOTHING, None]:
flash("请先登录!")
return redirect('/')
return render_template("homePage.html", login_user=session.get("login_user"), role=session.get("role"))
# 首页
@main.route('/', methods=["Get", "POST"])
def index():
if session.get("role") not in [Roles.NOTHING, None, Roles.TOURIST]:
return redirect('/homePage')
return render_template("index.html")
# 登录
@main.route('/login', methods=["Get", "POST"])
def login_user() -> dict:
username = request.form.get("username")
password = request.form.get("password")
remember = request.form.get("remember")
if not all([username, password]):
return {"status": ResType.PARAM_ERR.value, "msg": "登录信息不完整!"}
result = login(username, password)
if result["status"] == ResType.OK.value:
session["login_user"] = username
session["role"] = result["role"]
session["last_view"] = "中国"
if remember == "true":
session.permanent = True
else:
session.permanent = False
return result
# 游客登录
@main.route("/tourist")
def login_tourist():
session.clear()
session["login_user"] = "tourist"
session["role"] = Roles.TOURIST.value
session["last_view"] = "中国"
return redirect('/homePage')
# 注册
@main.route('/register', methods=["POST"])
def register_user() -> dict:
username = request.form.get("username")
password = request.form.get("password")
email = request.form.get("email")
verification = request.form.get("verification")
# 输入参数合法性验证,判断是否为空
if not all([username, password, email, verification]):
return {"status": ResType.PARAM_ERR.value, "msg": "输入参数不完整!"}
# 用户名长度校验
if len(username) < MIN_USERNAME_LENGTH or len(username) > MAX_USERNAME_LENGTH:
return {"status": ResType.PARAM_ERR.value,
"msg": "用户名长度应在" + str(MIN_USERNAME_LENGTH) + "到" + str(MAX_USERNAME_LENGTH) + "个字符之间"}
# 密码长度校验
if len(password) < MIN_PASSWORD_LENGTH or len(password) > MAX_PASSWORD_LENGTH:
return {"status": ResType.PARAM_ERR.value,
"msg": "密码长度应在" + str(MIN_PASSWORD_LENGTH) + "到" + str(MAX_PASSWORD_LENGTH) + "个字符之间"}
# 验证码校验
code = db.get(email)
# 输入的验证码为空或错误
if not verification or verification != code:
return {"status": ResType.VERIFICATION_ERR.value, "msg": "验证码错误或已失效"}
return register(username, password, email)
# 注销
@main.route('/logout', methods=["Get", "POST"])
def logout_user():
if session.get("role") in [Roles.NOTHING, None]:
flash("请先登录!")
return redirect('/')
return logout()
# 验证码
@main.route('/verify', methods=["POST"])
def verify():
# 获取邮箱地址并校验是否为空
email = request.form.get("email")
if not email:
return {"status": ResType.PARAM_ERR.value, "msg": "请输入完整的参数"}
send_verification(email)
return {"status": ResType.OK.value, "msg": "注册码已发送,注意查收!"}
if __name__ == '__main__':
main.run(host="127.0.0.1", port=8000, debug=True)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/blog52/visualization-project_1.git
git@gitee.com:blog52/visualization-project_1.git
blog52
visualization-project_1
VisualizationProject_1
prediction

搜索帮助

0d507c66 1850385 C8b1a773 1850385