update 1.调整项目文件夹 2.修改生成报告后读取逻辑(生成报告-存入数据库-删除本地报告-数据库读取文件-返回文件)

This commit is contained in:
彭森 2021-11-22 14:53:29 +08:00
parent 62ce4675df
commit 7886ee2135
9 changed files with 74 additions and 418 deletions

View File

@ -1,19 +1,16 @@
import copy
import datetime
from static.report.template import template_report
class ReportModel:
"""
信息技术业报告模板
"""
def __init__(self, report_data):
def __init__(self, req_json, tep_json):
# 接口传入数据
name = report_data['企业名称']
self.myDoc = copy.deepcopy(template_report)
self.myDoc = copy.deepcopy(tep_json)
self.df = dict()
self.df['企业名称'] = report_data['企业名称']
self.df['企业名称'] = req_json['企业名称']
def report_vlaues(self):
report_data = self.myDoc
@ -21,4 +18,3 @@ class ReportModel:
report_data['生成日期'] = datetime.datetime.now().strftime("%Y-%m-%d")
return report_data

View File

@ -1,10 +1,10 @@
import os
from config.project import PROJECT_NAME
from setting import APP_NAME
def get_project_abs_path():
file_abs_path = os.path.abspath(os.path.dirname(__file__))
project_abs_path = file_abs_path[:file_abs_path.find(PROJECT_NAME)+len(PROJECT_NAME)]
project_abs_path = file_abs_path[:file_abs_path.find(APP_NAME)+len(APP_NAME)]
return project_abs_path
@ -18,7 +18,7 @@ def get_font_path(**kwargs):
def gen_pdf_path(**kwargs):
name = kwargs['name']
project_abs_path = get_project_abs_path()
pdf_relative_path = '/result/{}.pdf'.format(name)
pdf_relative_path = '/static/report/{}.pdf'.format(name)
pdf_abs_path = os.path.abspath(project_abs_path + pdf_relative_path)
return pdf_abs_path
@ -33,6 +33,6 @@ def get_pic_path(**kwargs):
def get_gen_report_path(**kwargs):
name = kwargs['name']
project_abs_path = get_project_abs_path()
file_name = '/result/' + '{}.pdf'.format(name)
file_name = '/static/report/' + '{}.pdf'.format(name)
pdf_path = os.path.abspath(project_abs_path + file_name)
return pdf_path

View File

View File

@ -1 +0,0 @@
PROJECT_NAME = "tfse_rating"

View File

@ -1,7 +1,13 @@
"""
天府股交项目数据库
"""
from datetime import datetime
import pymongo
from bson import ObjectId
from gridfs import GridFS
from apps.Report.path_tool import get_gen_report_path
client = pymongo.MongoClient('mongodb://{}:{}@{}:{}'.format('admin', 'fecr1105', '116.63.159.166', 27017))
@ -17,3 +23,34 @@ def find_threshold(param):
collection = client['database']['collection']
data = collection.find({}, {'_id': False})
return list(data)
def upload_pdf(name):
"""
根据名称保存该企业报告
"""
path = get_gen_report_path(name=name)
database = '报告数据库'
collection = 'PDF'
fs = GridFS(client[database], collection)
content = open(path, 'rb').read()
pdf_id = fs.put(content, content_type='application/pdf', filename=name)
return str(pdf_id)
def download_pdf(pdf_id):
"""
根据pdf_id查询pdf文件
"""
database = '报告数据库'
collection = 'PDF'
fs = GridFS(client[database], collection)
data = fs.get(ObjectId(pdf_id)).read()
return data
if __name__ == '__main__':
pid = upload_pdf('远东资信评估有限公司')
file = download_pdf(pid)

File diff suppressed because one or more lines are too long

View File

@ -1,26 +1,39 @@
from flask import Blueprint, request, make_response, send_file
import json
import os
from flask import Blueprint, request, make_response, send_file, Response
from apps.Report.PdfReport import ReportGenerator
from apps.Report.ReportModel import ReportModel
from apps.Report.path_tool import get_gen_report_path
from db.tfse import upload_pdf, download_pdf
report_route = Blueprint('result', __name__)
@report_route.route('/gen_pdf', methods=['POST'])
def gen_pdf():
with open("static/test_data/report_template.json", "r", encoding="utf-8") as f:
data = json.load(f)
req = request.json
name = req['企业名称']
text_model = ReportModel(req).report_vlaues()
text_model = ReportModel(req_json=req, tep_json=data).report_vlaues()
# 生成报告保存到static临时文件夹
pdf_report = ReportGenerator(name=name, text_model=text_model)
pdf_report.gen_report()
file_path = get_gen_report_path(name=name)
res = make_response(send_file(file_path, as_attachment=True))
res.headers["Content-Disposition"] = "attachment; filename={}".format('{}.pdf'.format(name).encode().decode('latin-1'))
# 将报告pdf保存到数据库并删掉本地报告
file_id = upload_pdf(name)
if file_id:
file_path = get_gen_report_path(name=name)
os.remove(file_path)
file = download_pdf(file_id)
response = Response(file, content_type='application/pdf')
return response
return res

View File

@ -1,3 +1,3 @@
# 应用配置
APP_NAME = ''
APP_NAME = 'tfse_rating'
APP_PORT = ''

View File

@ -1,4 +1,4 @@
template_report = {
{
"企业名称": "{company}",
"生成日期": "{rank_date}",
"报告内容": [
@ -19,7 +19,7 @@ template_report = {
"小节内容": [
{"段落": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}
]
},
}
]
},
{
@ -82,7 +82,7 @@ template_report = {
"小节内容": [
{"段落": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}
]
},
}
]
},
{
@ -115,24 +115,24 @@ template_report = {
"小节": "附录1 评级符号说明",
"小节内容": [
{"表格": [
['', '']
]},
["符号示例", "解释示例"]
]}
]
},
{
"小节": "附录2 主要财务指标",
"小节内容": [
{"表格": [
['/', '2021/09/30', '2020/12/31', '2019/12/31', '2018/12/31']
]},
["指标/报告期", "2021/09/30", "2020/12/31", "2019/12/31", "2018/12/31"]
]}
]
},
{
"小节": "附录3 计算公式",
"小节内容": [
{"表格": [
['', '']
]},
["指标", "公式"]
]}
]
}
]