diff --git a/Implements/Others/TFSEFileImpl.py b/Implements/Others/TFSEFileImpl.py new file mode 100644 index 0000000..e4a5565 --- /dev/null +++ b/Implements/Others/TFSEFileImpl.py @@ -0,0 +1,29 @@ +from flask import Response + +from DBHelper.MongoHelper import MongoHelper +from ObjectsCommon.Others.FECRFile import FECRFile + + +class TFSEFileImpl(FECRFile): + """文件类实现""" + + db = MongoHelper("tfse_v0.21") + + def dict_to_return(self, **kwargs): + return self.file_body + + def get_image(self): + """""" + + def get_pdf(self): + + file_stream = self.db.find_file( + "文件", + self.file_bucket, + self.file_id + ) + + if file_stream: + self.file_body = Response(file_stream, content_type='application/pdf') + else: + self.file_body = None diff --git a/ObjectsCommon/Others/FECRFile.py b/ObjectsCommon/Others/FECRFile.py new file mode 100644 index 0000000..2130600 --- /dev/null +++ b/ObjectsCommon/Others/FECRFile.py @@ -0,0 +1,24 @@ +from flask import Response + +from Utils.ObjUtil import SpecObject +from Utils.ValidateUtil import ValidateAttr + + +class FECRFile(SpecObject): + """文件类""" + + file_id = ValidateAttr(field='file_id', type=str) + file_bucket = ValidateAttr(field='file_bucket', in_list=["综信报告", "综信证书", "ESG报告", "ESG证书"]) + file_body = ValidateAttr(field='file_body', type=Response, default=None) + + fields_map = { + "file_id": "文件ID", + "file_bucket": "文件桶", + "file_body": "文件体" + } + + def get_image(self): + """获取图片""" + + def get_pdf(self): + """获取PDF""" diff --git a/Routes/Other/TFSEFileRoute.py b/Routes/Other/TFSEFileRoute.py new file mode 100644 index 0000000..70527d1 --- /dev/null +++ b/Routes/Other/TFSEFileRoute.py @@ -0,0 +1,64 @@ +from flask import Blueprint, request, Response + +from Implements.Others.TFSEFileImpl import TFSEFileImpl +from Utils.AuthUtil import verify_token, verify_report_view_auth + + +file_route = Blueprint('file', __name__) + + +@file_route.route('/get_cc_rating_report', methods=['GET']) +@verify_token +@verify_report_view_auth +def get_cc_rating_report_route(**kwargs): + """获取综信报告""" + impl = TFSEFileImpl() + impl.file_bucket = '综信报告' + impl.file_id = request.args.get('file_id') + impl.get_pdf() + return impl.dict_to_return() + + +@file_route.route('/get_cc_rating_certificate', methods=['GET']) +@verify_token +@verify_report_view_auth +def get_cc_rating_certificate_route(**kwargs): + """获取综信证书""" + impl = TFSEFileImpl() + impl.file_bucket = '综信证书' + impl.file_id = request.args.get('file_id') + impl.get_pdf() + if impl.file_body: + return impl.dict_to_return() + else: + return {"info": "文件不存在"}, 200 + + +@file_route.route('/get_esg_rating_report', methods=['GET']) +@verify_token +@verify_report_view_auth +def get_esg_rating_report_route(**kwargs): + """获取ESG评价报告""" + impl = TFSEFileImpl() + impl.file_bucket = 'ESG报告' + impl.file_id = request.args.get('file_id') + impl.get_pdf() + if impl.file_body: + return impl.dict_to_return() + else: + return {"info": "文件不存在"}, 200 + + +@file_route.route('/get_esg_rating_certificate', methods=['GET']) +@verify_token +@verify_report_view_auth +def get_esg_rating_certificate_route(**kwargs): + """获取ESG评价证书""" + impl = TFSEFileImpl() + impl.file_bucket = 'ESG证书' + impl.file_id = request.args.get('file_id') + impl.get_pdf() + if impl.file_body: + return impl.dict_to_return() + else: + return {"info": "文件不存在"}, 200 diff --git a/Utils/AuthUtil.py b/Utils/AuthUtil.py index 04e206b..27d2257 100644 --- a/Utils/AuthUtil.py +++ b/Utils/AuthUtil.py @@ -73,9 +73,7 @@ def check_block(func): def verify_report_view_auth(func): - """ - 检查是否具有报告查看权限 - """ + """检查是否具有报告查看权限""" db = MongoHelper("tfse_v0.21") @@ -86,13 +84,13 @@ def verify_report_view_auth(func): records = db.find_all_data( "企业数据", - "评级记录", + "评价记录", {"企业ID": kwargs['cid']}, ["报告fid", "证书fid"] ) if not records: - return {"info": "没有找到文件"}, 200 + return {"info": "没有文件记录"}, 200 file_id_pool = list() for record in records: @@ -102,7 +100,7 @@ def verify_report_view_auth(func): file_id_pool = list(filter(None, file_id_pool)) if file_id not in file_id_pool: - return {"info": "文件不存在或无访问权限"}, 200 + return {"info": "文件ID异常"}, 200 return func(*args, **kwargs) return internal diff --git a/app.py b/app.py index b1ca770..b4d5249 100644 --- a/app.py +++ b/app.py @@ -4,9 +4,10 @@ from flask_cors import * from Routes.Company.Index import company_route from Routes.Other.EmailNoticeRoute import email_route +from Routes.Other.TFSEFileRoute import file_route from Routes.User.TFSECompanyUserRoute import company_user_route -from file.file_routes import file_route + from Rating.Credit.CreditRoutes import credit_route from Rating.Esg.EsgRoutes import esg_route from TestForAdmin.route import test_admin_route diff --git a/file/__init__.py b/file/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/file/file_db.py b/file/file_db.py deleted file mode 100644 index 98536d1..0000000 --- a/file/file_db.py +++ /dev/null @@ -1,30 +0,0 @@ -import gridfs -import pymongo -from bson import ObjectId -from gridfs import GridFS - -DB_HOST = "116.63.130.34" -DB_PASS = "UTlC9cCoglD1cI1*" -DB_USER = "root" -DB_PORT = "27021" -client = pymongo.MongoClient('mongodb://{}:{}@{}:{}'.format(DB_USER, DB_PASS, DB_HOST, DB_PORT)) - - -def FIND_FILE(bucket, file_id): - """ - 读取一个文件 - Parameters: - bucket: 文件存储桶 - file_id: 文件ID - Returns: - data 成功: 文件二进制; 失败: None - """ - # 实例化一个文件存储器 - gfs = GridFS(client['文件'], collection=bucket) - try: - # 二进制读取文件 - data = gfs.get(ObjectId(file_id)).read() - # 返回文件二进制流 - return data - except gridfs.errors.NoFile: - return None diff --git a/file/file_impl.py b/file/file_impl.py deleted file mode 100644 index f745add..0000000 --- a/file/file_impl.py +++ /dev/null @@ -1,16 +0,0 @@ -from file.file_db import FIND_FILE - - -def get_pdf_file_impl(file_type, fid): - """ - 根据文件类型、文件ID,获取pdf文件 - """ - # ftm: file_type_map - ftm = { - "cc_report": "综信报告", - "cc_certificate": "综信证书", - "esg_report": "ESG报告", - "esg_certificate": "ESG证书" - } - filestream = FIND_FILE(ftm[file_type], fid) - return filestream diff --git a/file/file_obj.py b/file/file_obj.py deleted file mode 100644 index b5b1fdf..0000000 --- a/file/file_obj.py +++ /dev/null @@ -1,11 +0,0 @@ -from file.file_impl import get_pdf_file_impl - - -class TfseFile: - - def __init__(self): - self.file_type = None - self.file_id = None - - def get_pdf_file(self): - return get_pdf_file_impl(self.file_type, self.file_id) diff --git a/file/file_routes.py b/file/file_routes.py deleted file mode 100644 index bde090f..0000000 --- a/file/file_routes.py +++ /dev/null @@ -1,24 +0,0 @@ -from flask import Blueprint, request, Response - -from Utils.AuthUtil import verify_token, verify_report_view_auth -from file.file_obj import TfseFile - -file_route = Blueprint('file', __name__) - - -@file_route.route('/pdf', methods=['GET']) -@verify_token -@verify_report_view_auth -def get_file(**kwargs): - """ - 获取pdf文件 - """ - tfse_file = TfseFile() - tfse_file.file_type = request.args.get('file_type') - tfse_file.file_id = request.args.get('file_id') - pdf = tfse_file.get_pdf_file() - if pdf is not None: - response = Response(pdf, content_type='application/pdf') - return response - else: - return {"info": "文件不存在"}, 200