def build_sample_db(): db.create_all() db.drop_all() db.create_all() user = User() user.first_name = 'admin' user.last_name = 'admin' user.login = '******' user.email = user.login + "@example.com" user.password = generate_password_hash('admin') db.session.add(user) db.session.add(RequestType(type='User')) db.session.add(RequestType(type='Bot')) db.session.add(Technique(name='chi_square')) db.session.add(Technique(name='Kolmogorov-Smirnov')) db.session.commit()
def register(): user_data = request.get_json() # 判断获取结果 if not user_data: return jsonify(errno=RET.PARAMERR, errmsg='参数错误') name = user_data.get('name') title = user_data.get('title') organization = user_data.get('organization') email = user_data.get('email') password = user_data.get('password') if not re.match( r"^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$", email): return jsonify(errno=RET.PARAMERR, errmsg="邮箱格式不正确") # 检查参数的完整性 if not all([name, title, organization, email, password]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') # 判断用户是否已经注册 try: user = User.query.filter_by(email=email).first() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='查询数据库异常') else: # 判断查询结果 if user: return jsonify(errno=RET.DATAEXIST, errmsg='邮箱号已注册') user = User() user.name = name user.title = title user.organization = organization user.email = email user.password = password try: db.session.add(user) db.session.commit() except Exception as e: current_app.logger.error(e) # 如果提交数据发生异常,需要进行回滚 db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='保存用户信息失败') return jsonify(errno=RET.OK, errmsg='OK', data=user.to_dict()) # 登录成功,重定向到登录页