From 5477d33dd49bbb1f63cffcc0d9664891b4ac0e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=9D=E5=B7=9D?= Date: Wed, 27 Apr 2022 17:15:36 +0800 Subject: [PATCH 1/4] tmp --- Implements/Others/TFSEFileImpl.py | 36 +++++++++++++++++++++++++++++++ ObjectsCommon/Others/FECRFile.py | 24 +++++++++++++++++++++ Routes/Other/TFSEFileRoute.py | 22 +++++++++++++++++++ Utils/AuthUtil.py | 4 ++-- app.py | 3 ++- file/__init__.py | 0 file/file_db.py | 30 -------------------------- file/file_impl.py | 16 -------------- file/file_obj.py | 11 ---------- file/file_routes.py | 24 --------------------- 10 files changed, 86 insertions(+), 84 deletions(-) create mode 100644 Implements/Others/TFSEFileImpl.py create mode 100644 ObjectsCommon/Others/FECRFile.py create mode 100644 Routes/Other/TFSEFileRoute.py delete mode 100644 file/__init__.py delete mode 100644 file/file_db.py delete mode 100644 file/file_impl.py delete mode 100644 file/file_obj.py delete mode 100644 file/file_routes.py diff --git a/Implements/Others/TFSEFileImpl.py b/Implements/Others/TFSEFileImpl.py new file mode 100644 index 0000000..bad987e --- /dev/null +++ b/Implements/Others/TFSEFileImpl.py @@ -0,0 +1,36 @@ +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_type_map = { + "cc_report": "综信报告", + "cc_certificate": "综信证书", + "esg_report": "ESG报告", + "esg_certificate": "ESG证书" + } + + try: + file_stream = self.db.find_file( + "文件", + file_type_map[self.file_type], + self.file_id + ) + + self.file_body = Response(file_stream, content_type='application/pdf') + except Exception: + self.file_body = None diff --git a/ObjectsCommon/Others/FECRFile.py b/ObjectsCommon/Others/FECRFile.py new file mode 100644 index 0000000..f4a37fd --- /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_type = ValidateAttr(field='file_type', type=str) + file_body = ValidateAttr(field='file_body', type=Response, default=None) + + fields_map = { + "file_id": "文件ID", + "file_type": "文件类型", + "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..60216c2 --- /dev/null +++ b/Routes/Other/TFSEFileRoute.py @@ -0,0 +1,22 @@ +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('/pdf', methods=['GET']) +@verify_token +@verify_report_view_auth +def get_file(**kwargs): + """获取pdf文件""" + impl = TFSEFileImpl() + impl.file_type = request.args.get('file_type') + 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..267a041 100644 --- a/Utils/AuthUtil.py +++ b/Utils/AuthUtil.py @@ -92,7 +92,7 @@ def verify_report_view_auth(func): ) if not records: - return {"info": "没有找到文件"}, 200 + return {"info": "文件不存在"}, 200 file_id_pool = list() for record in records: @@ -102,7 +102,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": "无访问权限"}, 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 From d33a43089e260e0391e790bd33760f810e4c16bf Mon Sep 17 00:00:00 2001 From: P3ngSaM <61768364+P3ngSaM@users.noreply.github.com> Date: Thu, 28 Apr 2022 10:11:14 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Implements/Others/TFSEFileImpl.py | 5 ++++- Routes/Other/TFSEFileRoute.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Implements/Others/TFSEFileImpl.py b/Implements/Others/TFSEFileImpl.py index bad987e..ca81b13 100644 --- a/Implements/Others/TFSEFileImpl.py +++ b/Implements/Others/TFSEFileImpl.py @@ -31,6 +31,9 @@ class TFSEFileImpl(FECRFile): self.file_id ) - self.file_body = Response(file_stream, content_type='application/pdf') + if file_stream: + self.file_body = Response(file_stream, content_type='application/pdf') + else: + self.file_body = None except Exception: self.file_body = None diff --git a/Routes/Other/TFSEFileRoute.py b/Routes/Other/TFSEFileRoute.py index 60216c2..3ac685e 100644 --- a/Routes/Other/TFSEFileRoute.py +++ b/Routes/Other/TFSEFileRoute.py @@ -9,7 +9,7 @@ file_route = Blueprint('file', __name__) @file_route.route('/pdf', methods=['GET']) @verify_token -@verify_report_view_auth +# @verify_report_view_auth def get_file(**kwargs): """获取pdf文件""" impl = TFSEFileImpl() From 6c6cd03e91e0552c16f5a205d920f7d24fc2786a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=9D=E5=B7=9D?= Date: Fri, 29 Apr 2022 11:27:24 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Implements/Others/TFSEFileImpl.py | 26 +++++---------- ObjectsCommon/Others/FECRFile.py | 4 +-- Routes/Other/TFSEFileRoute.py | 53 ++++++++++++++++++++++++++++--- Utils/AuthUtil.py | 10 +++--- 4 files changed, 63 insertions(+), 30 deletions(-) diff --git a/Implements/Others/TFSEFileImpl.py b/Implements/Others/TFSEFileImpl.py index ca81b13..e4a5565 100644 --- a/Implements/Others/TFSEFileImpl.py +++ b/Implements/Others/TFSEFileImpl.py @@ -17,23 +17,13 @@ class TFSEFileImpl(FECRFile): def get_pdf(self): - file_type_map = { - "cc_report": "综信报告", - "cc_certificate": "综信证书", - "esg_report": "ESG报告", - "esg_certificate": "ESG证书" - } + file_stream = self.db.find_file( + "文件", + self.file_bucket, + self.file_id + ) - try: - file_stream = self.db.find_file( - "文件", - file_type_map[self.file_type], - self.file_id - ) - - if file_stream: - self.file_body = Response(file_stream, content_type='application/pdf') - else: - self.file_body = None - except Exception: + 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 index f4a37fd..2130600 100644 --- a/ObjectsCommon/Others/FECRFile.py +++ b/ObjectsCommon/Others/FECRFile.py @@ -8,12 +8,12 @@ class FECRFile(SpecObject): """文件类""" file_id = ValidateAttr(field='file_id', type=str) - file_type = ValidateAttr(field='file_type', 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_type": "文件类型", + "file_bucket": "文件桶", "file_body": "文件体" } diff --git a/Routes/Other/TFSEFileRoute.py b/Routes/Other/TFSEFileRoute.py index 3ac685e..15f2daf 100644 --- a/Routes/Other/TFSEFileRoute.py +++ b/Routes/Other/TFSEFileRoute.py @@ -7,13 +7,58 @@ from Utils.AuthUtil import verify_token, verify_report_view_auth file_route = Blueprint('file', __name__) -@file_route.route('/pdf', methods=['GET']) +@file_route.route('/get_cc_rating_report', methods=['GET']) @verify_token -# @verify_report_view_auth +@verify_report_view_auth def get_file(**kwargs): - """获取pdf文件""" + """获取综信报告""" impl = TFSEFileImpl() - impl.file_type = request.args.get('file_type') + 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_cc_rating_certificate', methods=['GET']) +@verify_token +@verify_report_view_auth +def get_file(**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_file(**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_file(**kwargs): + """获取ESG评价证书""" + impl = TFSEFileImpl() + impl.file_bucket = 'ESG证书' impl.file_id = request.args.get('file_id') impl.get_pdf() if impl.file_body: diff --git a/Utils/AuthUtil.py b/Utils/AuthUtil.py index 267a041..7a2c22f 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": "没有访问权限"}, 200 return func(*args, **kwargs) return internal From dff7f59352ec9191b0cfabd53a93848b6c19cf98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=9D=E5=B7=9D?= Date: Fri, 29 Apr 2022 11:34:29 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Routes/Other/TFSEFileRoute.py | 13 +++++-------- Utils/AuthUtil.py | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Routes/Other/TFSEFileRoute.py b/Routes/Other/TFSEFileRoute.py index 15f2daf..70527d1 100644 --- a/Routes/Other/TFSEFileRoute.py +++ b/Routes/Other/TFSEFileRoute.py @@ -10,22 +10,19 @@ file_route = Blueprint('file', __name__) @file_route.route('/get_cc_rating_report', methods=['GET']) @verify_token @verify_report_view_auth -def get_file(**kwargs): +def get_cc_rating_report_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 + return impl.dict_to_return() @file_route.route('/get_cc_rating_certificate', methods=['GET']) @verify_token @verify_report_view_auth -def get_file(**kwargs): +def get_cc_rating_certificate_route(**kwargs): """获取综信证书""" impl = TFSEFileImpl() impl.file_bucket = '综信证书' @@ -40,7 +37,7 @@ def get_file(**kwargs): @file_route.route('/get_esg_rating_report', methods=['GET']) @verify_token @verify_report_view_auth -def get_file(**kwargs): +def get_esg_rating_report_route(**kwargs): """获取ESG评价报告""" impl = TFSEFileImpl() impl.file_bucket = 'ESG报告' @@ -55,7 +52,7 @@ def get_file(**kwargs): @file_route.route('/get_esg_rating_certificate', methods=['GET']) @verify_token @verify_report_view_auth -def get_file(**kwargs): +def get_esg_rating_certificate_route(**kwargs): """获取ESG评价证书""" impl = TFSEFileImpl() impl.file_bucket = 'ESG证书' diff --git a/Utils/AuthUtil.py b/Utils/AuthUtil.py index 7a2c22f..27d2257 100644 --- a/Utils/AuthUtil.py +++ b/Utils/AuthUtil.py @@ -100,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