def signup(): name = request.values.get('name',type = str, default = None) password = request.values.get('password',type = str, default = None) email = request.values.get('email',type = str, default = None) user_data = { 'code' : 700, 'data' : { 'msg':'parameter ILLEGAL', 'username':name, 'email': email } } if is_legal_str(name) and is_legal_str(password) and is_legal_str(email): if User.query.filter(User.username == name).all(): user_data['code'] = 400 user_data['data']['msg'] = 'User "' + name + '" already exists' return jsonify(user_data) if User.query.filter(User.email == email).all(): user_data['code'] = 400 user_data['data']['msg'] = 'The mailbox is already occupied' return jsonify(user_data) if MyRedis.get(email + '_checked') != email: user_data['code'] = 400 user_data['data']['msg'] = 'The mailbox was not verified' return jsonify(user_data) MyRedis.delete(email + '_checked') user = User(name, password, email) try: db.session.add(user) db.session.flush() db.session.commit() except: user_data['code'] = 300 user_data['data']['msg'] = 'Database error' return jsonify(user_data) user_data['code'] = 200 user_data['data']['id'] = user.id user_data['data']['msg'] = 'Signup Success' return jsonify(user_data) else: return jsonify(user_data)
def get_mail_verify(): return_json = {'data':{}} email = request.values.get('email',type = str, default = None) if not is_legal_str(email): return_json['code'] = 900 return_json['data']['msg'] = "Email can't use or Network congestion" return jsonify(return_json) if MyRedis.get(email) != None: verify_code = MyRedis.get(email) else: verify_code = get_verify_code() MyRedis.set(email, verify_code, REDIS_STAY_TIME) if send_email(email, verify_code) == -1: #发送失败,可能是网络问题或者email有误 return_json['code'] = 900 return_json['data']['msg'] = "Email can't use or Network congestion" return jsonify(return_json) else: return_json['code'] = 200 return_json['data']['msg'] = "Get verify code successfully" return jsonify(return_json)
def login_by_email(): user_data = {'data':{}} email = request.values.get('email',type = str, default = None) if MyRedis.get(email+'_checked') == email: MyRedis.delete(email + '_checked') user_search = User.query.filter(User.email == email).all() if user_search: user = user_search[0] login_user(user) user_data['code'] = 200 user_data['data'] = user.todict() user_data['data']['msg'] = 'User "' + user.username + '" login success' return jsonify(user_data) else: user_data['code'] = 400 user_data['data']['msg'] = 'User doesn\'t exist' return jsonify(user_data) else: user_data['code'] = 400 user_data['data']['msg'] = 'The mailbox was not verified' return jsonify(user_data)
def modify_info(): user_name = request.cookies.get("user_name") current_user = User.query.filter(User.username == user_name).all()[0] return_json = {'data': {}} newname = request.values.get('newname', type=str, default=None) if is_legal_str(newname): if User.query.filter(User.username == newname).all(): return_json['code'] = 400 return_json['data'][ 'msg'] = "User \"" + newname + "\" already exists" return jsonify(return_json) else: current_user.username = newname db.session.commit() return_json['code'] = 200 return_json['data']['msg'] = "Username modify success" return jsonify(return_json) newpassword = request.values.get('newpassword', type=str, default=None) if is_legal_str(newpassword): current_user.password = generate_password_hash(newpassword) db.session.commit() return_json['code'] = 200 return_json['data']['msg'] = "Password modify success" return jsonify(return_json) newmotto = request.values.get('newmotto', type=str, default=None) if newmotto: if len(newmotto) > 0 and len(newmotto) <= MAXMOTTO: current_user.motto = newmotto db.session.commit() return_json['code'] = 200 return_json['data']['msg'] = "Motto modify success" return jsonify(return_json) newemail = request.values.get('newemail', type=str, default=None) if is_legal_str(newemail): if MyRedis.get(newemail + "_checked") == newemail: current_user.email = newemail db.session.commit() return_json['code'] = 200 return_json['data']['msg'] = "Email modify success" return jsonify(return_json) else: return_json['code'] = 400 return_json['data']['msg'] = 'The mailbox was not verified' return jsonify(return_json) #所有的都不满足,就一定是参数错误 return_json['code'] = 900 return_json['data']['msg'] = "parameter ILLEGAL" return jsonify(return_json)
def check_mail_verify(): email = request.values.get('email',type = str, default = None) verify_code = request.values.get('verify_code',type = str, default = None) return_json = {'data':{}} if verify_code == MyRedis.get(email): MyRedis.set(email+"_checked", email, REDIS_STAY_TIME) #把email本身存在Redis里,确认后赋予权限 MyRedis.delete(email) return_json['code'] = 200 return_json['data']['msg'] = "Check verify code successfully" return jsonify(return_json) else: if MyRedis.get(email) != None: return_json['code'] = 900 return_json['data']['msg'] = "Verify code error" return jsonify(return_json) else: return_json['code'] = 900 return_json['data']['msg'] = "The verification code does not exist or has expired" return jsonify(return_json)
#用来做一些闲杂测试,忽略即可 from werkzeug.security import generate_password_hash, check_password_hash from Model import User, db, MyRedis from configs import IMAGEPATH from uuid import uuid1 import base64 import redis import time from Mail import send_email MyRedis.set("[email protected]_checked", "*****@*****.**")