diff --git a/app.py b/app.py index a7cd62d..3dcac8a 100644 --- a/app.py +++ b/app.py @@ -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'): diff --git a/tools.py b/tools.py index a90128b..2dce7b5 100644 --- a/tools.py +++ b/tools.py @@ -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 = """ - - - - - - Document - - - """ - html_tail = """ - - """ - file_obj = BytesIO(file_bytes) - result = mammoth.convert_to_html(file_obj) - html = result.value - html = re.sub('', '', 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 + '}', '') - 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 + '}

', '\n

{% endfor %}\n') - if lc[2] == "start": - html = html.replace('

{' + i + '}', - '\n{% for ' + lc[0] + ' in data.' + lc[0] + ' %}\n

') - 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: