routes.py 2.8 KB
Newer Older
梦想橡皮擦's avatar
梦想橡皮擦 已提交
1
from flask import render_template, make_response, request, redirect, url_for, flash
梦想橡皮擦's avatar
梦想橡皮擦 已提交
2
from app import app
梦想橡皮擦's avatar
梦想橡皮擦 已提交
3
import hashlib
梦想橡皮擦's avatar
梦想橡皮擦 已提交
4

梦想橡皮擦's avatar
梦想橡皮擦 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
"""
# 临时处理插入一下数据
from sqlalchemy.orm import sessionmaker
from app import db
"""

# 用户权限认证相关数据
from app.login_form import UserForm
from app.login_model import User

from flask_login import LoginManager,login_user,login_required,logout_user

# use login manager to manage session
login_manager = LoginManager(app)
login_manager.login_view = 'login'  # 设置登录页面


# 回调函数,用来加载用户
@login_manager.user_loader  #
def load_user(id):
    '''用于加载用户'''
    return User.query.get(int(id))


梦想橡皮擦's avatar
梦想橡皮擦 已提交
29 30 31
"""
首页相关路由配置
"""
梦想橡皮擦's avatar
梦想橡皮擦 已提交
32

33

梦想橡皮擦's avatar
梦想橡皮擦 已提交
34 35 36 37 38 39
@app.route('/')
@app.route('/index')
def index():
    item = {
        "msg": "后台传递信息"
    }
40 41
    nav = dict()
    nav['h'] = 'active'
梦想橡皮擦's avatar
梦想橡皮擦 已提交
42
    # 访问首页生成一个 Cookie 值,该值用于访问特定页面
43
    rendered_template = render_template('index.html', title='梦想橡皮擦', nav=nav)
梦想橡皮擦's avatar
梦想橡皮擦 已提交
44 45 46 47 48 49 50
    resp = make_response(rendered_template)
    text = "梦想橡皮擦"

    # 使用 sha256 算法进行加密
    encrypted_text = hashlib.sha256(text.encode()).hexdigest()
    resp.set_cookie('story', encrypted_text)
    return resp
梦想橡皮擦's avatar
梦想橡皮擦 已提交
51 52 53 54


@app.route('/bt')
def bt():
梦想橡皮擦's avatar
梦想橡皮擦 已提交
55 56 57 58 59
    return render_template('bt.html')


@app.route('/mac')
def mac():
梦想橡皮擦's avatar
梦想橡皮擦 已提交
60 61
    return render_template('_form.html')

62

梦想橡皮擦's avatar
梦想橡皮擦 已提交
63
"""博客搭建清单"""
64 65


梦想橡皮擦's avatar
梦想橡皮擦 已提交
66 67
@app.route('/blog')
def blog():
68 69 70 71 72 73 74 75 76 77 78
    nav = dict()
    nav['b'] = 'active'
    return render_template('blog.html', nav=nav)


# 建站时间轴
@app.route('/timeline')
def timeline():
    nav = dict()
    nav['time'] = 'active'
    return render_template('timeline.html', nav=nav)
梦想橡皮擦's avatar
梦想橡皮擦 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118


@app.route("/login", methods=['GET', 'POST'])
def login():
    form = UserForm()
    print(form.validate_on_submit())
    """
    # 创建session类型
    DBSession = sessionmaker(bind=db.engine)

    # 创建session对象
    session = DBSession()
    # 创建新的user对象
    new_user = User()
    new_user.username = 'xiangpica'
    new_user.password = '123456'
    session.add(new_user)
    # 提交即保存到数据库
    session.commit()
    # 关闭session
    session.close()
    """

    if form.validate_on_submit():
        # 查询用户信息
        user = User.query.filter_by(username=form.username.data).first()
        if user is not None:
            if user.verify_password(form.password.data):
                flash('登录成功')
                login_user(user)  # login_user 的参数为要登录的用户
                return redirect(request.args.get('next') or url_for('school.login_list_school'))
        flash('登录失败')
    return render_template('login.html', form=form)

# 退出登录
@app.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('login'))