Files
pythonstock--stock/web/dataEditorHandler.py
T

138 lines
4.6 KiB
Python
Executable File

#!/usr/bin/python3
# -*- coding: utf-8 -*-
from tornado import gen
# import sys
# import os
# sys.path.append(os.path.abspath('/data/stock/libs'))
import libs.stock_web_dic as stock_web_dic
import web.base as webBase
import logging
import tornado.web
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import io
import re
def GenImage(freq):
t = np.linspace(0, 10, 500)
y = np.sin(t * freq * 2 * 3.141)
fig1 = plt.figure()
plt.plot(t, y)
plt.xlabel('Time [s]')
memdata = io.BytesIO()
plt.grid(True)
plt.savefig(memdata, format='png')
image = memdata.getvalue()
return image
class ImageHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
image = GenImage(0.5)
self.set_header('Content-type', 'image/png')
self.set_header('Content-length', len(image))
self.write(image)
# 获得页面数据。
class GetEditorHtmlHandler(webBase.BaseHandler):
@gen.coroutine
def get(self):
name = self.get_argument("table_name", default=None, strip=False)
stockWeb = stock_web_dic.STOCK_WEB_DATA_MAP[name]
# self.uri_ = ("self.request.url:", self.request.uri)
# print self.uri_
self.render("data_editor.html", stockWeb=stockWeb, leftMenu=webBase.GetLeftMenu(self.request.uri))
# 拼接sql,将value的key 和 value 放到一起。
def genSql(primary_key, param_map, join_string):
tmp_sql = ""
idx = 0
for tmp_key in primary_key:
tmp_val = param_map[tmp_key]
if idx == 0:
tmp_sql = " `%s` = '%s' " % (tmp_key, tmp_val)
else:
tmp_sql += join_string + (" `%s` = '%s' " % (tmp_key, tmp_val))
idx += 1
return tmp_sql
# 获得页面数据。
class SaveEditorHandler(webBase.BaseHandler):
@gen.coroutine
def post(self):
action = self.get_argument("action", default=None, strip=False)
logging.info(action)
table_name = self.get_argument("table_name", default=None, strip=False)
stockWeb = stock_web_dic.STOCK_WEB_DATA_MAP[table_name]
# 临时map数组。
param_map = {}
# 支持多排序。使用shift+鼠标左键。
for item, val in self.request.arguments.items():
# 正则查找 data[1112][code] 里面的code字段
item_key = re.search(r"\]\[(.*?)\]", item)
if item_key:
tmp_1 = item_key.group()
if tmp_1:
tmp_1 = tmp_1.replace("][", "").replace("]", "")
param_map[tmp_1] = val[0].decode("utf-8")
#logging.info(param_map)
if action == "create":
logging.info("###########################create")
# 拼接where 和 update 语句。
tmp_columns = "`, `".join(stockWeb.columns)
tmp_values = []
for tmp_key in stockWeb.columns:
tmp_values.append(param_map[tmp_key])
# 更新sql。
tmp_values2 = "', '".join(tmp_values)
insert_sql = " INSERT INTO %s (`%s`) VALUES('%s'); " % (stockWeb.table_name, tmp_columns, tmp_values2)
logging.info(insert_sql)
try:
self.db.execute(insert_sql)
except Exception as e:
err = {"error": str(e)}
logging.info(err)
self.write(err)
return
elif action == "edit":
logging.info("###########################edit")
# 拼接where 和 update 语句。
tmp_update = genSql(stockWeb.columns, param_map, ",")
tmp_where = genSql(stockWeb.primary_key, param_map, "and")
# 更新sql。
update_sql = " UPDATE %s SET %s WHERE %s " % (stockWeb.table_name, tmp_update, tmp_where)
logging.info(update_sql)
try:
self.db.execute(update_sql)
except Exception as e:
err = {"error": str(e)}
logging.info(err)
self.write(err)
return
elif action == "remove":
logging.info("###########################remove")
# 拼接where 语句。
tmp_where = genSql(stockWeb.primary_key, param_map, "and")
# 更新sql。
delete_sql = " DELETE FROM %s WHERE %s " % (stockWeb.table_name, tmp_where)
logging.info(delete_sql)
try:
self.db.execute(delete_sql)
except Exception as e:
err = {"error": str(e)}
logging.info(err)
self.write(err)
return
self.write("{\"data\":[{}]}")