import tornado.web
from tornado import gen
import time
import datetime
from tornado.template import Template
from tornado.escape import json_encode

import os
from Send.Send_run import Send_parm,table
from Data.date_treat import *
class show(tornado.web.RequestHandler):
    def get(self):

        self.render("templates/run.html",msg='abd',report_name='uuu.csv')
    def post(self):
        report_name = self.get_argument('report_name')
        print(report_name)
class shownew(tornado.web.RequestHandler):
    def get(self):
        self.render("templates/run.html", msg='测试用例已生成，请下载report.csv文件，查看用例数据，确认无误后，点击运行')

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("templates/index.html")

    def post(self, *args, **kwargs):
        headers = self.get_argument('hearders')
        testurl=self.get_argument('testurl')
        devurl = self.get_argument('devurl')
        parm=self.get_argument('parm')
        methods=self.get_argument('method')
        codes=self.get_argument('codes')
        database= 'xyqb'#self.get_argument('database')
        type='sql'

        url=[testurl,devurl]

        if headers=='':
            headers={"rc_auth_key": "rc_developer", "rc_auth_secret": "rc_secret"}

        if headers and testurl and devurl and parm and methods and codes:
            if type == 'sql':
                if database:
                    df , error_s=sql_date(parm, database)
                    if  df.empty == False:
                        data = date_treat(df, url, headers, methods, codes)
                    else:
                        if error_s == '':
                            data = {"msg": '请求参数为空，请检查sql语句', 'flag':0}
                        else:

                            data = {"msg": 'Sql语句错误:{0}'.format(error_s), 'flag':0}
        else:
            data = {"msg":'有必填参数为空，请检查页面','flag':0}

        data=dict(data)
        if data['flag'] == 1:
            self.render("templates/run.html",msg=data['msg'],report_name=data['report_name'] )
        else:
            self.render("templates/error.html",msg=data['msg'])

settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static")
}#配置静态文件路径

application = tornado.web.Application([
    (r"/index", IndexHandler),
    (r"/run", Send_parm),
    (r"/table", table),
    (r"/shownew", shownew)
],**settings)


def make_app():
    setting = dict(
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
        static_path=os.path.join(os.path.dirname(__file__), "static"),
    )
    return tornado.web.Application([(r'/index', IndexHandler),
                                ],
                               **setting
                               )

if __name__ == "__main__":
    application.listen(9090)

    tornado.ioloop.IOLoop.instance().start()

