Blueprint總結
經過一段時間的折騰,藍圖框架遷移終於算是正式完成了,順帶解決了email認證過程中關於激活界面的一個問題,在其中學到了很多,這裏寫下了當作一種分享吧。
關於認證模塊的總結
解決email激活函數無法傳遞toekn參數的問題
from . import auth
from flask import render_template , url_for, session, redirect
from ..models import User , Login_Form , Register_Form , Article
from flask_login import current_user , login_required , login_user , logout_user , user_logged_in
from ..func import *
from .func import *
@auth.route('/')
def index():
return render_template('register.html')
@auth.route('/register',methods=['POST','GET'])
def register():
if current_user.is_authenticated:
dic=make_json_dic(301,user_username='annoyance',date=mcc_time(),info=mcc_info('you have already authenticated.'))
return jsonify(dic)
else:
if request.method=='POST':
form=Register_Form()
if form.mcc_validate():
#if form.validate_on_submit():
name=form.username.data
if form.password.data==form.re_password.data:
password=form.password.data
email=form.email.data
if db_user_auth(name,password)==False:
if mail_auth(email):
user=User()
user.name=name
user.password=password
user.email=email
db.session.add(user)
db.session.commit()
token = user.generate_activate_token()
# 發送激活郵件到註冊郵箱
send_mail(email, '賬戶激活', 'auth\\templates\\Life_Is_Strange_Artwork_5.jpg', token=token,username=name)
# 提示用戶下一步操作
return render_template('login.html')
else:
dic=make_json_dic(301,user_username=name,date=mcc_time(),info=mcc_info('email type erro.'))
return jsonify(dic)
#return render_template('register.html')
else:
dic=make_json_dic(301,user_username=name,date=mcc_time(),info=mcc_info('the user is registered .'))
return jsonify(dic)
#return render_template('register.html')
else:
dic=make_json_dic(301,user_username=name,date=mcc_time(),info=mcc_info('the password is not same.'))
return jsonify(dic)
#return render_template('register.html')
else:
dic=make_json_dic(301,user_username=name,date=mcc_time(),info=mcc_info('the form is not complete. '))
return jsonify(dic)
#return render_template('register.html')
else:
dic=make_json_dic(301,date=mcc_time(),info=mcc_info('the request is not supported.'))
return jsonify(dic)
#return render_template('register.html')
@auth.route('/login',methods=['POST','GET'])
def login():
if current_user.is_authenticated:
mcc_print("you are authenticated")
dic=make_json_dic(200,user_username=current_user.name,date=mcc_time(),info='current user is authenticated.')
return jsonify(dic)
else:
form=Login_Form()
dic=form_analysis(form)
if dic!=None:
if request.method=='POST':
username=dic['username']
password=dic['password']
user=User.query.filter_by(name=username).first()
if user is not None and password==user.password and user.confirmed==True:
session["username"]=username
session["password"]=password
login_user(user,True)
dic=make_json_dic(200,user_username=current_user.name,date=mcc_time(),info=mcc_info('current user is login.'))
return jsonify(dic)
else:
dic=make_json_dic(301,user_username=current_user.name,date=mcc_time(),info=mcc_info('authenticate fail.'))
return jsonify(dic)
else:
dic=make_json_dic(301,user_username=current_user.name,date=mcc_time(),info=mcc_info('authenticate fail.'))
return render_template('login.html')
else:
dic=make_json_dic(404)
return render_template('login.html')
@auth.route('/logout',methods=['POST','GET'])
def logout():
if current_user.is_authenticated:
logout_user()
dic=make_json_dic(200,user_username=current_user.name,date=mcc_time(),info=mcc_info('logout success.'))
return render_template('index.html')
else:
dic=make_json_dic(301,user_username=current_user.name,date=mcc_time(),info=mcc_info('you have login.'))
return render_template('login.html')
@auth.route('/activate/<token>')
def activate(token):
if token !=None:
if User.check_activate_token(self=current_user,token=token):
dic=dict()
dic['info']='activate success'
return jsonify(dic)
else:
dic=dict()
dic['info']='activate fail'
return jsonify(dic)
else:
mcc_print('none')
首先可以看出來,代碼基本上是分離前後端的,只不過我爲了測試加入了一些前端的代碼,這次主要想說的是關於activate這個路由,最初是用的是許多博客中採用的
@auth.route(’/activate/’)
def activate(token):
if token !=None:
if User.check_activate_token(token=token):
dic=dict()
dic[‘info’]=‘activate success’
return jsonify(dic)
else:
dic=dict()
dic[‘info’]=‘activate fail’
return jsonify(dic)
else:
mcc_print(‘none’)
就是這裏的token,我在本地測試是通不過的,但改成 if User.check_activate_token(token=token,self=current_user)之後,token是可以正確傳遞給check_activate_token函數的。
其它一些感受
計算方法前段時間真的是有點放鬆了,但越學到後來越覺得計算方法沒講授的那樣無聊,課程其實整合了特別多的理論知識,而且結合matlab實踐,不算水課,還是要自己投入時間去學習,感覺大學的課其實也沒想象中的那樣,有些課如果自己真的感興趣就認真的去學,並不是爲了刷分,而是爲了提高自己的綜合能力吧。其實有些課不用學,考前大量刷題也可以取得很可觀的分數,但真的就是感覺還不如不學來的直接。