# coding:utf-8
from pymysql import connect
import re
import urllib.parse
URL_FUNC_DICT = dict()
get_func_list = list()
def route(url):
def set_func(func):
# URL_FUNC_DICT['/index.py'] = index
URL_FUNC_DICT[url] = func
get_func_list.append(func)
def get_func(*args, **kwargs):
return get_func(*args, **kwargs)
return get_func
return set_func
# 股票信息
@route('/index.py')
def index(ret):
with open('./templates/index.html', 'r', encoding='UTF-8') as f:
content = f.read()
# 创建conn连接
conn = connect(host='localhost', port=3306, user='root', password='123456', database='scott', charset='utf8')
# 获取Cursor对象
cs = conn.cursor()
cs.execute("select * from info;")
stock_infos = cs.fetchall()
cs.close()
conn.close()
tr_template = '''
<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>
<input type = "button" value="添加" id="toAdd" name="toAdd" systemidvaule="%s">
<td>
</tr>
'''
html = ''
for line_info in stock_infos:
html += tr_template % (line_info[0],line_info[1],line_info[2],line_info[3],line_info[4],line_info[5],line_info[6],line_info[7],line_info[1])
content = re.sub(r'\{%content%\}', html, content)
return content
# 个人中心
@route('/center.py')
def center(ret):
with open('./templates/center.html', 'r', encoding='UTF-8') as f:
content = f.read()
# 创建conn连接
conn = connect(host='localhost', port=3306, user='root', password='123456', database='scott', charset='utf8')
# 获取Cursor对象
cs = conn.cursor()
cs.execute('select distinct i.code,i.short,i.chg,i.turnover,i.price,i.hights,f.note_info from info as i inner join focus as f on i.id=f.id;')
stock_infos = cs.fetchall()
cs.close()
conn.close()
tr_template = '''
<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>
<a type = "button" class= "btn btn-default btn-xs" href="/update/%s.html"> <span class="glyphicon glyphicon-star"
aria-hidden = "true"></span> 修改 </a>
</td>
<td>
<input type="button" value="删除" id="toDel" name="toDel" systemidvalue="%s">
</td>
</tr>
'''
html = ''
for line_info in stock_infos:
html += tr_template % (
line_info[0], line_info[1], line_info[2], line_info[3], line_info[4], line_info[5], line_info[6],line_info[0],line_info[0])
content = re.sub(r'\{%content%\}', html, content)
return content
# 添加股票关注
@route(r'/add/(\d+)\.html')
def add_focus(ret):
# 1.获取股票代码
stock_code = ret.group(1)
# 2.判断是否有这个股票代码
conn = connect(host='localhost', port=3306, user='root', password='123456', database='scott', charset='utf8')
cs = conn.cursor()
sql = '''select * from info where code = %s;'''
cs.execute(sql, (stock_code,))
if not cs.fetchone():
cs.close()
conn.close()
return '没有这支股票,大哥我们是创业公司,请手下留情...'
# 3.判断是否已经关注过
sql = '''select * from info as i inner join focus as f on i.id = f.info_id where i.code = %s;'''
cs.execute(sql, (stock_code,))
if cs.fetchone():
cs.close()
conn.close()
return '已经关注过了,请勿重复关注...'
# 4.添加关注
sql = '''insert into focus (info_id) select id from info where code = %s;'''
cs.execute(sql, (stock_code,))
conn.commit()
return '关注成功(%s)....' % stock_code
# 取消关注
@route(r'/del/(\d+)\.html')
def del_focus(ret):
# 1.获取股票代码
stock_code = ret.group(1)
conn = connect(host='localhost', port=3306, user='root', password='123456', database='scott', charset='utf8')
cs = conn.cursor()
sql = '''select * from info where code = %s;'''
cs.execute(sql, (stock_code,))
# 2.判断是否有这个股票代码
if not cs.fetchone():
cs.close()
conn.close()
return '没有这支股票,大哥我们是创业公司,请手下留情...'
# 3.判断是否已经关注过
sql = '''select * from info as i inner join focus as f on i.id = f.info_id where i.code = %s;'''
cs.execute(sql, (stock_code,))
if not cs.fetchone():
cs.close()
conn.close()
return '之前未关注,取消关注失败...'
# 4.取消关注
sql = '''delete from focus where info_id = (select id from info where code = %s);'''
cs.execute(sql, (stock_code,))
conn.commit()
return '取消成功(%s)....' % stock_code
# 添加备注
@route(r'/update/(\d+)\.html)')
def update_page(ret):
'''显示修改页面'''
# 1.获取股票代码
stock_code = ret.group(1)
# 2.打开模板
with open('./templates/update.html') as f:
content = f.read()
conn = connect(host='localhost', port=3306, user='root', password='123456', database='scott', charset='utf8')
cs = conn.cursor()
sql = '''select f.note_info from focus as f inner join info as i on i.id = f.info_id where i.code = %s;'''
cs.execute(sql, (stock_code,))
stock_infos = cs.fetchone()
note_info = stock_infos[0]
cs.close()
conn.close()
content = re.sub(r'\{%note_info%\}', note_info, content)
content = re.sub(r'\{%code%\}', stock_code, content)
return content
# 保存修改的备注
@route(r'/update/(\d+)/(\.*)\.html)') # w不支持空格
def save_update_page(ret):
'''保存修改的信息'''
# 1.获取股票代码
stock_code = ret.group(1)
comment = ret.group(2)
comment = urllib.parse.unquote(comment) # url解码
conn = connect(host='localhost', port=3306, user='root', password='123456', database='scott', charset='utf8')
cs = conn.cursor()
sql = '''update focus set note_info = %s where info_id = (select id from info where code = %s);'''
cs.execute(sql, (comment, stock_code))
conn.commit()
cs.close()
conn.close()
return '修改成功...'
def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html;charset=utf-8'),('server', 'mini_frame v1.0')])
file_name = env['PATH_INFO']
''' if file_name == '/index.py':
return index()
elif file_name == '/center.py':
return center()
else:
return 'Hello World!(๑′ᴗ‵๑)I Lᵒᵛᵉᵧₒᵤ❤中国'
'''
try:
for url, func in URL_FUNC_DICT.items():
ret = re.match(url, file_name)
if ret:
return func(ret)
else:
return '请求的url(%s)没有对应得函数....'% file_name
except Exception as ret:
return '产生了异常:%s' % str(ret)