flask 动手写的接口平台

Posted 北漂的雷子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask 动手写的接口平台相关的知识,希望对你有一定的参考价值。

   笔者做的是测试,在群里经常有人讨论,怎么和开发对接怎么难,怎么测接口比较难,开发不愿因写文档等等,是啊,我感觉也是这样,沟通,还有我们应该怎样去学习,去扩充自己,让自己不再受开发所左右,

    笔者就像试图用flask去做这么一个东西吧,所有的数据什么保存到数据库,按照一定的规矩存放,那么我去获取数据库,展示过来就好,那么研发也不用给我写文档啥的,我直接去读取你数据库的接口东西就可以。

  说就说,干就干,人生就这样。

  选择Python+flask做,数据库sqlite3。

  下面来看下我的目录,

  

 

  数据库,forms  views  视图,

  数据库如下:

  

 1 # encoding: utf-8
 2 \'\'\'
 3 @author: lileilei
 4 @file: models.py
 5 @time: 2017/5/6 19:28
 6 \'\'\'
 7 from  app import  db
 8 import datetime
 9 from werkzeug.security import generate_password_hash,check_password_hash
10 class User(db.Model):#用户表
11     uer_id=db.Column(db.Integer,primary_key=True,autoincrement=True)#用户id
12     user_name=db.Column(db.String(64))#用户名
13     password=db.Column(db.String(64))#用户密码
14     status=db.Column(db.Integer)
15     level=db.Column(db.Integer)
16     user_zhuce_date=db.Column(db.DateTime,default=datetime.datetime.now())#注册时间
17     user_zhuce_email=db.Column(db.String(64))#注册ip
18     user_iphone=db.Column(db.Integer)#手机号
19     user_qq=db.Column(db.Integer)#qq
20     def __repr__(self):
21         return \'<User %r>\' % self.user_name
22     def set_password(self, password):
23         self.password = generate_password_hash(password)
24     def check_password(self, password):
25         return check_password_hash(self.password, password)
26 class Model(db.Model):#模块
27     model_id=db.Column(db.Integer,primary_key=True,autoincrement=True)
28     model_name = db.Column(db.String(256))
29     status = db.Column(db.Integer)
30 class Interface(db.Model):#接口
31     interface_id=db.Column(db.Integer,primary_key=True,autoincrement=True)
32     model_id=db.Column(db.Integer,db.ForeignKey(\'model.model_id\'))#对应模块的名字
33     interface_name=db.Column(db.String(64))#接口名字
34     interface_url=db.Column(db.String(1024))#接口地址
35     interface_methd=db.Column(db.String(64))#接口请求方式
36     request_exam=db.Column(db.String(4096))#请求
37     reposnese_exam=db.Column(db.String(4096))#返回
38     stasus=db.Column(db.Integer)#状态
39     def __repr__(self):
40         return \'<Interface name is :%r>\'%self.interface_name
41 class Parameter(db.Model):
42     parameter_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
43     interface_id = db.Column(db.Integer, db.ForeignKey("interface.interface_id"))
44     parameter_type = db.Column(db.String(64))
45     parameter_group_id = db.Column(db.Integer)
46     parameter_name = db.Column(db.String(64))
47     necessary = db.Column(db.String(64))
48     type = db.Column(db.String(64))
49     default = db.Column(db.String(64))
50     remark = db.Column(db.String(64))
51     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()])

  效果图

github传送门

 

以上是关于flask 动手写的接口平台的主要内容,如果未能解决你的问题,请参考以下文章

Flask 编写http接口api及接口自动化测试

接口测试

Flask测试小工具平台

Web前端性能测试平台开发(Flask)

Flask框架学习笔记(API接口管理平台 V2.0)

开源接口测试平台