更新数据截止日2025.12.07

This commit is contained in:
lychang
2025-12-08 16:24:17 +08:00
parent 2c8b096c8f
commit 40f2aabf3a
2 changed files with 0 additions and 117 deletions

55
app.py
View File

@@ -1176,61 +1176,6 @@ def generate_weekly_report():
return api_response(success=False, error=e, status_code=500)
@app.route('/api/render/template', methods=['POST'])
def report_render_template():
data = request.get_json()
template = data.get('template', '')
origin_path = data.get('path', '')
file = ofm.get_file(origin_path)
with open("a.docx", 'wb') as f:
f.write(file)
if file:
template_content = transform_template(file)
response = ofm.upload_file(f"tpldev/render/{template}.template", template_content.encode('utf-8'))
print(response)
if response:
return api_response(success=True,
data={"template_name": template, "template_path": f"tpldev/render/{template}.template"},
status_code=200)
return api_response(success=False, error="上传模板失败", status_code=500)
@app.route('/api/render/insight', methods=['GET', 'POST'])
def report_render():
"""获取统计数据API"""
try:
if request.method == 'GET':
security_code = request.args.get('security_code', '')
else:
data = request.get_json()
security_code = data.get('security_code', '')
url = "http://10.0.0.39/cloud/in/api/online/run/cache"
body = {
"pid": "1988527857586081794",
"fid": "1988527857586081794",
"secCode": security_code
}
resp = requests.post(url=url, json=body)
if resp.status_code != 200:
return api_response(success=False, error=resp.text, status_code=500)
else:
data_info = resp.json().get('data', {}).get('data', '')
data_info = data_info.replace("data:application/json,", "")
data_info = json.loads(data_info)
data_path = data_info.get('url', '')
data = requests.get(data_path)
template_content = ofm.get_file("tpldev/render/insight.template")
html = report_template_render(template_content.decode("utf-8"), data=data.json())
if request.method == 'GET':
return html
else:
return api_response(success=False, content=html, status_code=200)
except Exception as e:
return api_response(success=False, error=e, status_code=500)
if __name__ == '__main__':
# 检查数据库文件是否存在
if not os.path.exists('db/data'):

View File

@@ -5,68 +5,6 @@ from typing import Union, Optional
import sqlite3
from file import ObjectFileManager, LocalFileManager
import re
from io import BytesIO
import jinja2
import mammoth
def transform_template(file_bytes: bytes):
html_head = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
table, th, td {
border: 1px solid #000000;
border-collapse: collapse;
}
</style>
</head>
<body>"""
html_tail = """
</body>
</html>"""
file_obj = BytesIO(file_bytes)
result = mammoth.convert_to_html(file_obj)
html = result.value
html = re.sub('<img.*?>', '', html)
c = re.compile(r'{(.*?)}')
ll = c.findall(html)
ta_list = []
for i in ll:
lc = i.split('+')
item_type = lc[1]
if item_type in ["TS", "TN", "TD"]:
if lc[0].split(".")[0] not in ta_list:
html = html.replace('{' + i + '}', '{{data["' + lc[0] + '"]}}')
else:
html = html.replace('{' + i + '}', '{{' + lc[0] + '}}')
elif item_type == "TP":
html = html.replace('{' + i + '}', '<img width="70%" src="data:image/jpeg;base64,{{data.' + lc[0] + '}}">')
elif item_type == "TC":
if lc[4] == "end":
html = html.replace('{' + i + '}', '\n{% endif %}\n')
if lc[4] == "start":
html = html.replace('{' + i + '}', '\n{% if data.' + lc[0] + '=="' + lc[3] + '" %}\n')
elif item_type == "TA":
ta_list.append(lc[0])
if lc[2] == "end":
html = html.replace('{' + i + '}</p></td></tr>', '\n</p></td></tr>{% endfor %}\n')
if lc[2] == "start":
html = html.replace('<tr><td><p>{' + i + '}',
'\n{% for ' + lc[0] + ' in data.' + lc[0] + ' %}\n<tr><td><p>')
else:
print(lc[0], lc)
return html_head + html + html_tail
def report_template_render(template: str, data: dict):
html = jinja2.Template(template).render(data=data)
return html
class DatabaseManager: