feat: fix bug

This commit is contained in:
MarkusJoe
2022-02-05 16:45:35 +08:00
parent 74db8ed1ac
commit 9860df7375
9 files changed
+208 -81

No files matched your search

+49 -64
View File
@@ -11,9 +11,10 @@ from flask import Flask
from flask import request from flask import request
from flask import Response from flask import Response
from flask import make_response from flask import make_response
from flask import render_template
from flask import send_from_directory from flask import send_from_directory
from bin.render_ import render_temp_
from bin.b64img import re_sort_number_image from bin.b64img import re_sort_number_image
from bin.length import make_html
from wsgiref.simple_server import make_server from wsgiref.simple_server import make_server
from db.db import ( from db.db import (
fetch_data, fetch_data,
@@ -23,68 +24,14 @@ from db.db import (
app = Flask(__name__) app = Flask(__name__)
def is_new_user(name: str): def error_length(length: int) -> Response:
"""
判断名称是否在数据库内
如果在则将该名称的值加1
如果不在数据库内则新加此名称到数据库
:param name:
:return:
"""
"""fetch_data 函数返回的是当前名称下访问次数的数值 为整型"""
count = fetch_data(name)
if count == 0:
return [True, 0]
elif len(str(count)) <= 10:
return [True, count]
else:
update_data(name, 0)
return [False, 0]
def build_page(name: str) -> Response or str:
"""
渲染最终的页面
:param name:
:return:
"""
status, count = is_new_user(name)
if status:
"""
计算出要在数字前加上几个0, 其实就是10减去数字位数
假如当前访问数字是300, 先将整形转换为字符串, 再使用len()计算出长度
使用10 - 去长度, 剩下的结果就是要在前面加上几个0
"""
zero_counter = 10 - len(str(count))
if count != 0: # 不是新名称时直接计算要添加几个零
final_number = '0' * zero_counter + str(count) # 拼接
else: # 是新名称直接设置为10个零
final_number = '0000000000'
sorted_image = re_sort_number_image(final_number)
return render_template('index.html',
title=name,
svg_img_0=f'{sorted_image[0]}',
svg_img_1=f'{sorted_image[1]}',
svg_img_2=f'{sorted_image[2]}',
svg_img_3=f'{sorted_image[3]}',
svg_img_4=f'{sorted_image[4]}',
svg_img_5=f'{sorted_image[5]}',
svg_img_6=f'{sorted_image[6]}',
svg_img_7=f'{sorted_image[7]}',
svg_img_8=f'{sorted_image[8]}',
svg_img_9=f'{sorted_image[9]}'
) # 渲染模板
else:
return 'LONG'
def too_long_to_count() -> Response:
""" """
数值太长显示此页面 数值太长显示此页面
:param length:
:return: :return:
""" """
response = make_response({'code': 200, response = make_response({'code': 200,
'message': '当前数值超过了最大可计数范围(10位)已将此名称的数据重置'}) 'message': f'错误的长度: {length}'})
response.status_code = 200 response.status_code = 200
response.headers['Content-Type'] = 'application/json; charset=utf-8' response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate' response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
@@ -92,14 +39,26 @@ def too_long_to_count() -> Response:
return response return response
def too_lang_to_count() -> Response:
"""
数据过长重置数据
:return:
"""
response = make_response({'code': 200,
'message': f'当前长度超过了预设的值'})
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
response.headers['date'] = time.ctime()
return response
def arg_not_be_full() -> Response: def arg_not_be_full() -> Response:
""" """
参数不完整 参数不完整
:return: :return:
""" """
response = make_response({'code': 200, response = make_response({'code': 200,
'message': '参数填写不完整', 'message': '参数填写不完整或填写错误'})
'timestamp': time.time()})
response.status_code = 200 response.status_code = 200
response.headers['Content-Type'] = 'application/json; charset=utf-8' response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate' response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
@@ -107,24 +66,50 @@ def arg_not_be_full() -> Response:
return response return response
def build_page(name: str, length: int) -> Response | str | bool:
"""
渲染最终的页面
:param name:
:param length:
:return:
"""
count = fetch_data(name)
if len(str(count)) > length:
return too_lang_to_count()
if 7 <= length <= 10:
make_html(length)
zero_count = '0' * (length - len(str(count))) + str(count)
sorted_image = re_sort_number_image(zero_count)
return render_temp_(length, name, sorted_image)
else:
return False
@app.route('/API', methods=['GET', 'POST']) # 允许 GET 和 POST 方法 @app.route('/API', methods=['GET', 'POST']) # 允许 GET 和 POST 方法
def api_page() -> Response or str: def api_page() -> Response | str:
""" """
API 页面函数 API 页面函数
:return: :return:
""" """
if 'length' in request.args:
try:
length = int(request.args['length'])
except ValueError:
return arg_not_be_full()
else:
length = 8
if 'name' in request.args: if 'name' in request.args:
name = request.args['name'] name = request.args['name']
if name != '': # 数据为空返回参数不完整界面 if name != '': # 数据为空返回参数不完整界面
resp = build_page(name) resp = build_page(name, length)
if resp != 'LONG': if resp:
response = make_response(resp) response = make_response(resp)
response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8' response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8'
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate' response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
response.headers['date'] = time.ctime() response.headers['date'] = time.ctime()
return response return response
else: else:
return too_long_to_count() return error_length(length)
else: else:
return arg_not_be_full() return arg_not_be_full()
else: else:
+29
View File
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
# -- coding:utf-8 --
# @Author: markushammered@gmail.com
# @Development Tool: PyCharm
# @Create Time: 2022/2/5
# @File Name: length.py
def make_html(length: int, svg_width: int = 45, svg_height: int = 70) -> None:
"""
生成html
:param svg_height:
:param svg_width:
:param length:
:return:
"""
html_head = """<?xml version="1.0" encoding="UTF-8"?>
<svg width="%(w)s" height="%(h)s" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>{{ title }}</title>
<g>\n"""
svg_tag = """ <image x="%(x)s" y="0" width="%(w)s" height="%(h)s" xlink:href="{{ svg_img_%(i)s }}"></image>\n"""
html_feet = """</g></svg>"""
html_head = html_head % {'w': length * svg_width, 'h': svg_height} # 总体宽度
for i in range(length): # 拼接出完整的html
html_head += svg_tag % {'x': i * svg_width, 'w': svg_width, 'h': svg_height, 'i': i}
html_head += html_feet # 加上结尾标签
with open('./templates/index.html', 'w', encoding='utf-8') as template_fp: # 写入文件
template_fp.write(html_head)
+71
View File
@@ -0,0 +1,71 @@
#!/usr/bin/env python3
# -- coding:utf-8 --
# @Author: markushammered@gmail.com
# @Development Tool: PyCharm
# @Create Time: 2022/2/5
# @File Name: render_.py
from flask import render_template
def render_temp_(length: int, name: str, image_list: list) -> str:
"""
代码很烂写到隐蔽的地方就看不见了
:param length:
:param name:
:param image_list:
:return:
"""
# 自己写的都看不下去了
if length == 7:
return render_template('index.html',
title=name,
svg_img_0=f'{image_list[0]}',
svg_img_1=f'{image_list[1]}',
svg_img_2=f'{image_list[2]}',
svg_img_3=f'{image_list[3]}',
svg_img_4=f'{image_list[4]}',
svg_img_5=f'{image_list[5]}',
svg_img_6=f'{image_list[6]}'
)
elif length == 8:
return render_template('index.html',
title=name,
svg_img_0=f'{image_list[0]}',
svg_img_1=f'{image_list[1]}',
svg_img_2=f'{image_list[2]}',
svg_img_3=f'{image_list[3]}',
svg_img_4=f'{image_list[4]}',
svg_img_5=f'{image_list[5]}',
svg_img_6=f'{image_list[6]}',
svg_img_7=f'{image_list[7]}'
)
elif length == 9:
return render_template('index.html',
title=name,
svg_img_0=f'{image_list[0]}',
svg_img_1=f'{image_list[1]}',
svg_img_2=f'{image_list[2]}',
svg_img_3=f'{image_list[3]}',
svg_img_4=f'{image_list[4]}',
svg_img_5=f'{image_list[5]}',
svg_img_6=f'{image_list[6]}',
svg_img_7=f'{image_list[7]}',
svg_img_8=f'{image_list[8]}',
)
else:
return render_template('index.html',
title=name,
svg_img_0=f'{image_list[0]}',
svg_img_1=f'{image_list[1]}',
svg_img_2=f'{image_list[2]}',
svg_img_3=f'{image_list[3]}',
svg_img_4=f'{image_list[4]}',
svg_img_5=f'{image_list[5]}',
svg_img_6=f'{image_list[6]}',
svg_img_7=f'{image_list[7]}',
svg_img_8=f'{image_list[8]}',
svg_img_9=f'{image_list[9]}'
)
+11
View File
@@ -0,0 +1,11 @@
#!/usr/bin/env python3
# -- coding:utf-8 --
# @Author: markushammered@gmail.com
# @Development Tool: PyCharm
# @Create Time: 2022/2/5
# @File Name: evaltest.py
cmd = """return ('index.html', title=name, svg_img_0=f'{sorted_image[0]}', svg_img_1=f'{sorted_image[1]}', svg_img_2=f'{sorted_image[2]}', svg_img_3=f'{sorted_image[3]}', svg_img_4=f'{sorted_image[4]}', svg_img_5=f'{sorted_image[5]}', svg_img_6=f'{sorted_image[6]}', svg_img_7=f'{sorted_image[7]}', svg_img_8=f'{sorted_image[8]}', svg_img_9=f'{sorted_image[9]}' )"""
print(eval(cmd))
BIN
View File
Binary file not shown.
+3 -2
View File
@@ -22,7 +22,7 @@ def insert_data(name: str):
conn.commit() conn.commit()
def fetch_data(name: str, mode: bool = False) -> int: def fetch_data(name: str) -> int:
""" """
获取数据 获取数据
:param name: :param name:
@@ -43,8 +43,9 @@ def fetch_data(name: str, mode: bool = False) -> int:
if name in temp_dict.keys(): if name in temp_dict.keys():
count = temp_dict[name] # 获取原数字 为 整型 count = temp_dict[name] # 获取原数字 为 整型
update_data(name, count) update_data(name, count)
return temp_dict[name] return count
else: else:
# 新建用户数据
insert_data(name) insert_data(name)
return 0 return 0
+17
View File
@@ -0,0 +1,17 @@
#!/usr/bin/env python3
# -- coding:utf-8 --
# @Author: markushammered@gmail.com
# @Development Tool: PyCharm
# @Create Time: 2022/2/5
# @File Name: t.py
import sqlite3
conn = sqlite3.connect('data.db')
cursor = conn.cursor()
cursor.execute('select * from ReqCount')
print(cursor.fetchall())
cursor.execute('update ReqCount set times=99999998 where name="MarkusJoe"')
conn.commit()
+16
View File
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="450" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>{{ title }}</title>
<g>
<image x="0" y="0" width="45" height="50" xlink:href="{{ svg_img_0 }}"></image>
<image x="45" y="0" width="45" height="50" xlink:href="{{ svg_img_1 }}"></image>
<image x="90" y="0" width="45" height="50" xlink:href="{{ svg_img_2 }}"></image>
<image x="135" y="0" width="45" height="50" xlink:href="{{ svg_img_3 }}"></image>
<image x="180" y="0" width="45" height="50" xlink:href="{{ svg_img_4 }}"></image>
<image x="225" y="0" width="45" height="50" xlink:href="{{ svg_img_5 }}"></image>
<image x="270" y="0" width="45" height="50" xlink:href="{{ svg_img_6 }}"></image>
<image x="315" y="0" width="45" height="50" xlink:href="{{ svg_img_7 }}"></image>
<image x="360" y="0" width="45" height="50" xlink:href="{{ svg_img_8 }}"></image>
<image x="405" y="0" width="45" height="50" xlink:href="{{ svg_img_9 }}"></image>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

+12 -15
View File
@@ -1,16 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<svg width="450" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <svg width="360" height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>{{ title }}</title> <title>{{ title }}</title>
<g> <g>
<image x="0" y="0" width="45" height="50" xlink:href="{{ svg_img_0 }}"></image> <image x="0" y="0" width="45" height="70" xlink:href="{{ svg_img_0 }}"></image>
<image x="45" y="0" width="45" height="50" xlink:href="{{ svg_img_1 }}"></image> <image x="45" y="0" width="45" height="70" xlink:href="{{ svg_img_1 }}"></image>
<image x="90" y="0" width="45" height="50" xlink:href="{{ svg_img_2 }}"></image> <image x="90" y="0" width="45" height="70" xlink:href="{{ svg_img_2 }}"></image>
<image x="135" y="0" width="45" height="50" xlink:href="{{ svg_img_3 }}"></image> <image x="135" y="0" width="45" height="70" xlink:href="{{ svg_img_3 }}"></image>
<image x="180" y="0" width="45" height="50" xlink:href="{{ svg_img_4 }}"></image> <image x="180" y="0" width="45" height="70" xlink:href="{{ svg_img_4 }}"></image>
<image x="225" y="0" width="45" height="50" xlink:href="{{ svg_img_5 }}"></image> <image x="225" y="0" width="45" height="70" xlink:href="{{ svg_img_5 }}"></image>
<image x="270" y="0" width="45" height="50" xlink:href="{{ svg_img_6 }}"></image> <image x="270" y="0" width="45" height="70" xlink:href="{{ svg_img_6 }}"></image>
<image x="315" y="0" width="45" height="50" xlink:href="{{ svg_img_7 }}"></image> <image x="315" y="0" width="45" height="70" xlink:href="{{ svg_img_7 }}"></image>
<image x="360" y="0" width="45" height="50" xlink:href="{{ svg_img_8 }}"></image> </g></svg>
<image x="405" y="0" width="45" height="50" xlink:href="{{ svg_img_9 }}"></image>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 878 B