flask 开发接口测试平台
2019-05-04 23:01 清风软件测试开发 阅读(1195) 评论(1) 编辑 收藏 举报flask 开发接口测试平台
数据库,forms views 视图,
数据库如下:
# encoding: utf-8 ''' @author: lileilei @file: models.py @time: 2017/5/6 19:28 ''' from app import db import datetime from werkzeug.security import generate_password_hash,check_password_hash class User(db.Model):#用户表 uer_id=db.Column(db.Integer,primary_key=True,autoincrement=True)#用户id user_name=db.Column(db.String(64))#用户名 password=db.Column(db.String(64))#用户密码 status=db.Column(db.Integer) level=db.Column(db.Integer) user_zhuce_date=db.Column(db.DateTime,default=datetime.datetime.now())#注册时间 user_zhuce_email=db.Column(db.String(64))#注册ip user_iphone=db.Column(db.Integer)#手机号 user_qq=db.Column(db.Integer)#qq def __repr__(self): return '<User %r>' % self.user_name def set_password(self, password): self.password = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password, password) class Model(db.Model):#模块 model_id=db.Column(db.Integer,primary_key=True,autoincrement=True) model_name = db.Column(db.String(256)) status = db.Column(db.Integer) class Interface(db.Model):#接口 interface_id=db.Column(db.Integer,primary_key=True,autoincrement=True) model_id=db.Column(db.Integer,db.ForeignKey('model.model_id'))#对应模块的名字 interface_name=db.Column(db.String(64))#接口名字 interface_url=db.Column(db.String(1024))#接口地址 interface_methd=db.Column(db.String(64))#接口请求方式 request_exam=db.Column(db.String(4096))#请求 reposnese_exam=db.Column(db.String(4096))#返回 stasus=db.Column(db.Integer)#状态 def __repr__(self): return '<Interface name is :%r>'%self.interface_name class Parameter(db.Model): parameter_id = db.Column(db.Integer, primary_key=True, autoincrement=True) interface_id = db.Column(db.Integer, db.ForeignKey("interface.interface_id")) parameter_type = db.Column(db.String(64)) parameter_group_id = db.Column(db.Integer) parameter_name = db.Column(db.String(64)) necessary = db.Column(db.String(64)) type = db.Column(db.String(64)) default = db.Column(db.String(64)) remark = db.Column(db.String(64)) level = db.Column(db.String(64))
views 视图函数如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys from flask import render_template, redirect, session, url_for,flash,request from app import app from app import db from .models import Interface, Model, User, Parameter from .forms import LoginForm, ReguistForm,ModelForm, InterfaceForm, ParameterRequestForm, ParameterResponseForm, SubmitForm import datetime # 前台首页 @app.route("/", methods=["GET", "POST"]) @app.route("/index", methods=["GET", "POST"]) def index(): return render_template("index.html") # 前台模块列表 @app.route("/font", methods=["GET", "POST"]) def front(): model_all = Model.query.all() model_forms = [] for i in range(len(model_all)): model_form = {"model_id": model_all[i].model_id, "model_name": model_all[i].model_name} model_forms.append(model_form) return render_template("front.html", model_forms=model_forms) # 前台接口列表 @app.route("/front_model/<model_id>", methods=["GET", "POST"]) def front_model(model_id): model_one = Model.query.filter_by(model_id=model_id).first() interface_model_all = Interface.query.filter_by(model_id=model_id).all() interface_model_forms = [] for i in range(len(interface_model_all)): interface_model_form = {"interface_id": interface_model_all[i].interface_id, "interface_name": interface_model_all[i].interface_name, "interface_url": interface_model_all[i].interface_url} interface_model_forms.append(interface_model_form) return render_template("front_model.html", model_id=model_id, model_one=model_one, interface_model_forms=interface_model_forms) g_parameter_request = [] g_parameter_response = [] # 前台接口详情 @app.route("/front_interface/<model_id>&<interface_id>", methods=["GET", "POST"]) def front_interface(model_id, interface_id): global g_parameter_request, g_parameter_response model_one = Model.query.filter_by(model_id=model_id).first() interface_model_all = Interface.query.filter_by(model_id=model_id).all() interface_model_one = Interface.query.filter_by(interface_id=interface_id).first() interface_model_forms = [] for i in range(len(interface_model_all)): interface_model_form = {"interface_id": interface_model_all[i].interface_id, "interface_name": interface_model_all[i].interface_name, "interface_url": interface_model_all[i].interface_url} interface_model_forms.append(interface_model_form) parameter_request_all = Parameter.query.filter_by(interface_id=interface_id, parameter_type=10).all() parameter_request_forms = [] for i in range(len(parameter_request_all)): parameter_request_form = {"parameter_id": parameter_request_all[i].parameter_id, "parameter_name": parameter_request_all[i].parameter_name, "necessary": parameter_request_all[i].necessary, "type": parameter_request_all[i].type, "default": parameter_request_all[i].default, "remark": parameter_request_all[i].remark, "parameter_group_id": parameter_request_all[i].parameter_group_id, "level": parameter_request_all[i].level} parameter_request_forms.append(parameter_request_form) parameter_response_all = Parameter.query.filter_by(interface_id=interface_id, parameter_type=20).all() parameter_response_forms = [] for i in range(len(parameter_response_all)): parameter_response_form = {"parameter_id": parameter_response_all[i].parameter_id, "parameter_name": parameter_response_all[i].parameter_name, "necessary": parameter_response_all[i].necessary, "type": parameter_response_all[i].type, "default": parameter_response_all[i].default, "remark": parameter_response_all[i].remark, "parameter_group_id": parameter_response_all[i].parameter_group_id, "level": parameter_response_all[i].level} parameter_response_forms.append(parameter_response_form) g_parameter_request = [] # 全局变量、清空列表数据 g_parameter_response = [] # 全局变量、清空列表数据 n_parameter_request(parameter_request_forms, parameter_request_all[0].parameter_group_id) n_parameter_response(parameter_response_forms,parameter_response_all[0].parameter_group_id) return render_template("front_interface.html", model_id=model_id, model_one=model_one, interface_model_forms=interface_model_forms, interface_model_one=interface_model_one, g_parameter_request=g_parameter_request, g_parameter_response=g_parameter_response) # 请求参数排序 def n_parameter_request(request, parameter_group_id): for form in request: if form["parameter_group_id"] == parameter_group_id: new_parameter_group_id = form["parameter_id"] g_parameter_request.append(form) n_parameter_request(request, new_parameter_group_id) return g_parameter_request # 返回参数排序 def n_parameter_response(response, parameter_group_id): for form in response: if form["parameter_group_id"] == parameter_group_id: new_parameter_group_id = form["parameter_id"] g_parameter_response.append(form) n_parameter_response(response, new_parameter_group_id) return g_parameter_response # 后台首页 @app.route("/home", methods=["GET", "POST"]) def home(): if not session.get("logged_in"): return redirect(url_for("login")) return render_template("home.html") # 后台登录 @app.route("/login", methods=["GET", "POST"]) def login(): form = LoginForm() if form.validate_on_submit(): try: user_one = User.query.filter_by(user_name=form.username.data).first() me=user_one.check_password(form.password.data) if me==True and user_one.status == 1: session["logged_in"] = True return redirect(url_for("home")) else: flash('登录密码错误!请检测你的密码') return render_template("login.html", form=form) except AttributeError: flash('登录fail!') return render_template("login.html", form=form) return render_template("login.html", form=form) # 后台登出 @app.route("/logout", methods=["GET", "POST"]) def logout(): session.pop("logged_in", None) return redirect(url_for("index")) # 模块管理 @app.route("/model", methods=["GET", "POST"]) def model(): if not session.get("logged_in"): return redirect(url_for("login")) model_all = Model.query.all() model_forms = [] for i in range(len(model_all)): model_form = {"model_id": model_all[i].model_id, "model_name": model_all[i].model_name} model_forms.append(model_form) return render_template("model.html", model_forms=model_forms) # 新增模块 @app.route("/addmodel", methods=["GET", "POST"]) def addmodel(): if not session.get("logged_in"): return redirect(url_for("login")) add_model = ModelForm() submit = SubmitForm() if submit.validate_on_submit(): add = Model(model_name=add_model.model_name.data) db.session.add(add) db.session.commit() return redirect(url_for("model")) return render_template("addmodel.html", add_model=add_model, submit=submit) # 编辑模块 @app.route("/editmodel/<model_id>", methods=["GET", "POST"]) def editmodel(model_id): if not session.get("logged_in"): return redirect(url_for("login")) model_one = Model.query.filter_by(model_id=model_id).first() edit_model = ModelForm() submit = SubmitForm() if submit.validate_on_submit(): model_one.model_name = edit_model.model_name.data db.session.commit() return redirect(url_for("model")) edit_model.model_name.data = model_one.model_name return render_template("editmodel.html", edit_model=edit_model, submit=submit) # 接口列表 @app.route("/interface/<model_id>") def interface(model_id): if not session.get("logged_in"): return redirect(url_for("login")) interface_model_all = Interface.query.filter_by(model_id=model_id).all() interface_model_forms = [] for i in range(len(interface_model_all)): interface_model_form = {"interface_id": interface_model_all[i].interface_id, "interface_name": interface_model_all[i].interface_name, "interface_url": interface_model_all[i].interface_url} interface_model_forms.append(interface_model_form) return render_template("interface.html", model_id=model_id, interface_model_forms=interface_model_forms) # 新增接口 @app.route("/addinterface/<model_id>", methods=["GET", "POST"]) def addinterface(model_id): if not session.get("logged_in"): return redirect(url_for("login")) add_interface = InterfaceForm() submit = SubmitForm() if submit.validate_on_submit(): add = Interface(interface_name=add_interface.interface_name.data, model_id=model_id, interface_url=add_interface.interface_url.data, interface_methd=add_interface.interface_method.data, request_exam=add_interface.request_exam.data, reposnese_exam=add_interface.response_exam.data) db.session.add(add) db.session.commit() return redirect(url_for("interface", model_id=model_id)) return render_template("addinterface.html", add_interface=add_interface, model_id=model_id, submit=submit) # 编辑接口 @app.route("/editinterface/<model_id>&<interface_id>", methods=["GET", "POST"]) def editinterface(model_id, interface_id): if not session.get("logged_in"): return redirect(url_for("login")) interface_model_one = Interface.query.filter_by(interface_id=interface_id).first() edit_interface_model = InterfaceForm() submit = SubmitForm() if submit.validate_on_submit(): interface_model_one.interface_name = edit_interface_model.interface_name.data interface_model_one.interface_url = edit_interface_model.interface_url.data interface_model_one.interface_methd = edit_interface_model.interface_method.data interface_model_one.request_exam = edit_interface_model.request_exam.data interface_model_one.response_exam = edit_interface_model.response_exam.data db.session.commit() return redirect(url_for("interface", model_id=model_id)) edit_interface_model.interface_name.data = interface_model_one.interface_name edit_interface_model.interface_url.data = interface_model_one.interface_url edit_interface_model.interface_method.data = interface_model_one.interface_methd edit_interface_model.request_exam.data = interface_model_one.request_exam edit_interface_model.response_exam.data = interface_model_one.reposnese_exam return render_template("editinterface.html", model_id=model_id, interface_id=interface_id, edit_interface_model=edit_interface_model, submit=submit) # 请求参数列表 @app.route("/parameter_request/<model_id>&<interface_id>", methods=["GET", "POST"]) def parameter_request(model_id, interface_id): if not session.get("logged_in"): return redirect(url_for("login")) parameter_request_all = Parameter.query.filter_by(interface_id=interface_id, parameter_type=10).all() parameter_request_forms = [] for i in range(len(parameter_request_all)): parameter_request_form = {"parameter_id": parameter_request_all[i].parameter_id, "parameter_name": parameter_request_all[i].parameter_name, "necessary": parameter_request_all[i].necessary, "type": parameter_request_all[i].type, "default": parameter_request_all[i].default, "remark": parameter_request_all[i].remark, "parameter_group_id": parameter_request_all[i].parameter_group_id, "level": parameter_request_all[i].level} parameter_request_forms.append(parameter_request_form) return render_template("parameter_request.html", model_id=model_id, interface_id=interface_id, parameter_request_forms=parameter_request_forms) # 新增请求参数 @app.route("/addparameter_request/<model_id>&<interface_id>", methods=["GET", "POST"]) def addparameter_request(model_id, interface_id): if not session.get("logged_in"): return redirect(url_for("login")) add_parameter_request = ParameterRequestForm() submit = SubmitForm() if submit.validate_on_submit(): add = Parameter(interface_id=interface_id, parameter_type=10, parameter_group_id=add_parameter_request.parameter_group_id.data, parameter_name=add_parameter_request.parameter_name.data, necessary=add_parameter_request.necessary.data, type=add_parameter_request.type.data, default=add_parameter_request.default.data, remark=add_parameter_request.remark.data, level=add_parameter_request.level.data) db.session.add(add) db.session.commit() return redirect(url_for("parameter_request", model_id=model_id, interface_id=interface_id)) return render_template("addparameter_request.html", add_parameter_request=add_parameter_request, model_id=model_id, interface_id=interface_id, submit=submit) # 编辑请求参数 @app.route("/editparameter_request/<model_id>&<interface_id>&<parameter_id>", methods=["GET", "POST"]) def editparameter_request(model_id, interface_id, parameter_id): if not session.get("logged_in"): return redirect(url_for("login")) parameter_request_one = Parameter.query.filter_by(parameter_id=parameter_id).first() edit_parameter_request = ParameterRequestForm() submit = SubmitForm() if submit.validate_on_submit(): parameter_request_one.parameter_name = edit_parameter_request.parameter_name.data parameter_request_one.necessary = edit_parameter_request.necessary.data parameter_request_one.type = edit_parameter_request.type.data parameter_request_one.default = edit_parameter_request.default.data parameter_request_one.remark = edit_parameter_request.remark.data parameter_request_one.parameter_group_id = edit_parameter_request.parameter_group_id.data parameter_request_one.level = edit_parameter_request.level.data db.session.commit() return redirect(url_for("parameter_request", model_id=model_id, interface_id=interface_id)) edit_parameter_request.parameter_name.data = parameter_request_one.parameter_name edit_parameter_request.necessary.data = parameter_request_one.necessary edit_parameter_request.type.data = parameter_request_one.type edit_parameter_request.default.data = parameter_request_one.default edit_parameter_request.remark.data = parameter_request_one.remark edit_parameter_request.parameter_group_id.data = parameter_request_one.parameter_group_id edit_parameter_request.level.data = parameter_request_one.level return render_template("editparameter_request.html", model_id=model_id, interface_id=interface_id, parameter_id=parameter_id, edit_parameter_request=edit_parameter_request, submit=submit) # 删除请求参数 @app.route("/deleteparameter_request/<model_id>&<interface_id>&<parameter_id>", methods=["GET", "POST"]) def deleteparameter_request(model_id, interface_id, parameter_id): if not session.get("logged_in"): return redirect(url_for("login")) deleteparameter_request_one = Parameter.query.filter_by(parameter_id=parameter_id).first() db.session.delete(deleteparameter_request_one) db.session.commit() parameter_request_all = Parameter.query.filter_by(interface_id=interface_id, parameter_type=10).all() parameter_request_forms = [] for i in range(len(parameter_request_all)): parameter_request_form = {"parameter_id": parameter_request_all[i].parameter_id, "parameter_name": parameter_request_all[i].parameter_name, "necessary": parameter_request_all[i].necessary, "type": parameter_request_all[i].type, "default": parameter_request_all[i].default, "remark": parameter_request_all[i].remark, "parameter_group_id": parameter_request_all[i].parameter_group_id, "level": parameter_request_all[i].level} parameter_request_forms.append(parameter_request_form) return render_template("parameter_request.html", model_id=model_id, interface_id=interface_id, parameter_request_forms=parameter_request_forms) # 返回参数列表 @app.route("/parameter_response/<model_id>&<interface_id>", methods=["GET", "POST"]) def parameter_response(model_id, interface_id): if not session.get("logged_in"): return redirect(url_for("login")) parameter_response_all = Parameter.query.filter_by(interface_id=interface_id, parameter_type=20).all() parameter_response_forms = [] for i in range(len(parameter_response_all)): parameter_response_form = {"parameter_id": parameter_response_all[i].parameter_id, "parameter_name": parameter_response_all[i].parameter_name, "necessary": parameter_response_all[i].necessary, "type": parameter_response_all[i].type, "default": parameter_response_all[i].default, "remark": parameter_response_all[i].remark, "parameter_group_id": parameter_response_all[i].parameter_group_id, "level": parameter_response_all[i].level} parameter_response_forms.append(parameter_response_form) return render_template("parameter_response.html", model_id=model_id, interface_id=interface_id, parameter_response_forms=parameter_response_forms) # 新增返回参数 @app.route("/addparameter_response/<model_id>&<interface_id>", methods=["GET", "POST"]) def addparameter_response(model_id, interface_id): if not session.get("logged_in"): return redirect(url_for("login")) add_parameter_response = ParameterResponseForm() submit = SubmitForm() if submit.validate_on_submit(): add = Parameter(interface_id=interface_id, parameter_type=20, parameter_group_id=add_parameter_response.parameter_group_id.data, parameter_name=add_parameter_response.parameter_name.data, necessary=add_parameter_response.necessary.data, type=add_parameter_response.type.data, default=add_parameter_response.default.data, remark=add_parameter_response.remark.data, level=add_parameter_response.level.data) db.session.add(add) db.session.commit() return redirect(url_for("parameter_response", model_id=model_id, interface_id=interface_id)) return render_template("addparameter_response.html", add_parameter_response=add_parameter_response, model_id=model_id, interface_id=interface_id, submit=submit) # 编辑返回参数 @app.route("/editparameter_response/<model_id>&<interface_id>&<parameter_id>", methods=["GET", "POST"]) def editparameter_response(model_id, interface_id, parameter_id): if not session.get("logged_in"): return redirect(url_for("login")) parameter_response_one = Parameter.query.filter_by(parameter_id=parameter_id).first() edit_parameter_response = ParameterResponseForm() submit = SubmitForm() if submit.validate_on_submit(): parameter_response_one.parameter_name = edit_parameter_response.parameter_name.data parameter_response_one.necessary = edit_parameter_response.necessary.data parameter_response_one.type = edit_parameter_response.type.data parameter_response_one.default = edit_parameter_response.default.data parameter_response_one.remark = edit_parameter_response.remark.data parameter_response_one.parameter_group_id = edit_parameter_response.parameter_group_id.data parameter_response_one.level = edit_parameter_response.level.data db.session.commit() return redirect(url_for("parameter_response", model_id=model_id, interface_id=interface_id)) edit_parameter_response.parameter_name.data = parameter_response_one.parameter_name edit_parameter_response.necessary.data = parameter_response_one.necessary edit_parameter_response.type.data = parameter_response_one.type edit_parameter_response.default.data = parameter_response_one.default edit_parameter_response.remark.data = parameter_response_one.remark edit_parameter_response.parameter_group_id.data = parameter_response_one.parameter_group_id edit_parameter_response.level.data = parameter_response_one.level return render_template("editparameter_response.html", model_id=model_id, interface_id=interface_id, parameter_id=parameter_id, edit_parameter_response=edit_parameter_response, submit=submit) # 删除返回参数 @app.route("/deleteparameter_response/<model_id>&<interface_id>&<parameter_id>", methods=["GET", "POST"]) def deleteparameter_response(model_id, interface_id, parameter_id): if not session.get("logged_in"): return redirect(url_for("login")) deleteparameter_response_one = Parameter.query.filter_by(parameter_id=parameter_id).first() db.session.delete(deleteparameter_response_one) db.session.commit() parameter_response_all = Parameter.query.filter_by(interface_id=interface_id, parameter_type=20).all() parameter_response_forms = [] for i in range(len(parameter_response_all)): parameter_response_form = {"parameter_id": parameter_response_all[i].parameter_id, "parameter_name": parameter_response_all[i].parameter_name, "necessary": parameter_response_all[i].necessary, "type": parameter_response_all[i].type, "default": parameter_response_all[i].default, "remark": parameter_response_all[i].remark, "parameter_group_id": parameter_response_all[i].parameter_group_id, "level": parameter_response_all[i].level} parameter_response_forms.append(parameter_response_form) return render_template("parameter_response.html", model_id=model_id, interface_id=interface_id, parameter_response_forms=parameter_response_forms) # 用户管理 @app.route("/users", methods=["GET", "POST"]) def user(): if not session.get("logged_in"): return redirect(url_for("login")) user_all = User.query.filter_by(status=1).all() user_forms = [] for i in range(len(user_all)): user_form = {"user_id": user_all[i].uer_id, "user_name": user_all[i].user_name, "password": user_all[i].password[:30], "status": user_all[i].status, "level": user_all[i].level, "user_zhuce_date": user_all[i].user_zhuce_date, "user_zhuce_email": user_all[i].user_zhuce_email, "user_iphone": user_all[i].user_iphone, "user_qq": user_all[i].user_qq } user_forms.append(user_form) return render_template("user_admin.html", user_forms=user_forms) #注册 @app.route('/register',methods=['GET','POST']) def registser(): form = ReguistForm() if form.validate_on_submit(): user=form.user_name.data me=User.query.filter_by(user_name=user).first() if me: flash('用户名已经存在!') return render_template('regist.html',form=form) if form.que_password.data != form.password.data: flash(' 确认密码是否一致!') return render_template('regist.html',form=form) passw=form.password.data print(passw) add=User( user_name=form.user_name.data, status=1, user_zhuce_email=form.email.data, user_iphone=form.iphone.data, user_qq=form.qq.data, user_zhuce_date=datetime.datetime.now() ) add.set_password(password=passw) db.session.add(add) db.session.commit() return redirect(url_for('login')) return render_template("regist.html", form=form) #删除用户 @app.route('/delete/<user_name>',methods=['GET','POST']) def delete(user_name): if not session.get("logged_in"): return redirect(url_for("login")) delete_user=User.query.filter_by(user_name=user_name).first() db.session.delete(delete_user) db.session.commit() user_all = User.query.filter_by(status=1).all() user_forms = [] for i in range(len(user_all)): user_form = {"user_id": user_all[i].uer_id, "user_name": user_all[i].user_name, "password": user_all[i].password[:30], "status": user_all[i].status, "level": user_all[i].level, "user_zhuce_date": user_all[i].user_zhuce_date, "user_zhuce_email": user_all[i].user_zhuce_email, "user_iphone": user_all[i].user_iphone, "user_qq": user_all[i].user_qq } user_forms.append(user_form) return render_template("user_admin.html", user_forms=user_forms)
forms 表单代码如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- from flask_wtf import Form from wtforms import StringField, TextField, PasswordField, FormField, SubmitField, FieldList, IntegerField from wtforms.validators import Required, DataRequired class LoginForm(Form): username = StringField("登陆名", validators=[Required()]) password = PasswordField("密码", validators=[Required()]) class ModelForm(Form): model_name = StringField("中文名称", validators=[Required()]) class InterfaceForm(Form): interface_name = StringField("接口名称", validators=[Required()]) interface_url = StringField("接口地址", validators=[Required()]) interface_method = StringField("接口方法", validators=[Required()]) request_exam = TextField("请求示例", validators=[Required()]) response_exam = TextField("返回示例", validators=[Required()]) class ParameterRequestForm(Form): parameter_group_id = StringField("从属", validators=[Required()]) parameter_name = StringField("参数名称", validators=[Required()]) necessary = StringField("是否必须", validators=[Required()]) type = StringField("类型", validators=[Required()]) default = StringField("默认值", validators=[Required()]) remark = StringField("备注", validators=[Required()]) level = StringField("层级", validators=[Required()]) class ParameterResponseForm(Form): parameter_group_id = StringField("从属", validators=[Required()]) parameter_name = StringField("参数名称", validators=[Required()]) necessary = StringField("是否必须", validators=[Required()]) type = StringField("类型", validators=[Required()]) default = StringField("示例", validators=[Required()]) remark = StringField("描述", validators=[Required()]) level = StringField("层级", validators=[Required()]) class SubmitForm(Form): submit = SubmitField("保存") class ReguistForm(Form):#注册 user_name=StringField('用户名',validators=[Required()]) password=PasswordField('密码',validators=[Required()]) que_password=PasswordField('密码',validators=[Required()]) iphone=IntegerField('手机号',validators=[Required()]) qq=IntegerField('qq号',validators=[Required()]) email=StringField('邮箱',validators=[Required()])
效果如下图:
原文地址:https://www.cnblogs.com/leiziv5/p/6830256.html
git地址https://github.com/liwanlei/plmock