4de905f7 by 周伟奇

add ecm

1 parent 50b15762
...@@ -19,7 +19,8 @@ from common.mixins import LoggerMixin ...@@ -19,7 +19,8 @@ from common.mixins import LoggerMixin
19 from common.tools.file_tools import write_zip_file 19 from common.tools.file_tools import write_zip_file
20 from common.tools.pdf_to_img import PDFHandler 20 from common.tools.pdf_to_img import PDFHandler
21 from apps.doc import consts 21 from apps.doc import consts
22 from apps.doc.ocr.edms import EDMS, rh 22 # from apps.doc.ocr.edms import EDMS, rh
23 from apps.doc.ocr.ecm import ECM, rh
23 from apps.doc.named_enum import KeywordsType, FailureReason, WorkflowName, ProcessName, RequestTeam, RequestTrigger 24 from apps.doc.named_enum import KeywordsType, FailureReason, WorkflowName, ProcessName, RequestTeam, RequestTrigger
24 from apps.doc.exceptions import EDMSException, OCR1Exception, OCR2Exception, OCR4Exception 25 from apps.doc.exceptions import EDMSException, OCR1Exception, OCR2Exception, OCR4Exception
25 from apps.doc.ocr.wb import BSWorkbook 26 from apps.doc.ocr.wb import BSWorkbook
...@@ -67,7 +68,8 @@ class Command(BaseCommand, LoggerMixin): ...@@ -67,7 +68,8 @@ class Command(BaseCommand, LoggerMixin):
67 self.ocr_url_3 = conf.BC_URL 68 self.ocr_url_3 = conf.BC_URL
68 self.ocr_url_4 = conf.IC_URL 69 self.ocr_url_4 = conf.IC_URL
69 # EDMS web_service_api 70 # EDMS web_service_api
70 self.edms = EDMS() 71 # self.edms = EDMS()
72 self.edms = ECM()
71 # 优雅退出信号:15 73 # 优雅退出信号:15
72 signal.signal(signal.SIGTERM, self.signal_handler) 74 signal.signal(signal.SIGTERM, self.signal_handler)
73 75
...@@ -717,7 +719,8 @@ class Command(BaseCommand, LoggerMixin): ...@@ -717,7 +719,8 @@ class Command(BaseCommand, LoggerMixin):
717 for times in range(consts.RETRY_TIMES): 719 for times in range(consts.RETRY_TIMES):
718 try: 720 try:
719 if not doc.application_id.startswith(consts.FIXED_APPLICATION_ID_PREFIX): 721 if not doc.application_id.startswith(consts.FIXED_APPLICATION_ID_PREFIX):
720 self.edms.download(pdf_path, doc.metadata_version_id) 722 # self.edms.download(pdf_path, doc.metadata_version_id)
723 self.edms.download(pdf_path, doc.metadata_version_id, doc.document_scheme, business_type)
721 self.online_log.info('{0} [edms download success] [task={1}] [times={2}] ' 724 self.online_log.info('{0} [edms download success] [task={1}] [times={2}] '
722 '[pdf_path={3}]'.format(self.log_base, task_str, times, pdf_path)) 725 '[pdf_path={3}]'.format(self.log_base, task_str, times, pdf_path))
723 726
......
...@@ -13,6 +13,7 @@ class ECM: ...@@ -13,6 +13,7 @@ class ECM:
13 self.pwd = conf.ECM_PWD 13 self.pwd = conf.ECM_PWD
14 self.oauth_url = conf.ECM_OAUTH_URL 14 self.oauth_url = conf.ECM_OAUTH_URL
15 self.download_url = conf.ECM_DOWNLOAD_URL 15 self.download_url = conf.ECM_DOWNLOAD_URL
16 self.upload_url = conf.ECM_UPLOAD_URL
16 self.oauth_headers = { 17 self.oauth_headers = {
17 'Content-Type': 'application/x-www-form-urlencoded' 18 'Content-Type': 'application/x-www-form-urlencoded'
18 } 19 }
...@@ -25,6 +26,22 @@ class ECM: ...@@ -25,6 +26,22 @@ class ECM:
25 self.token_type = 'Bearer' 26 self.token_type = 'Bearer'
26 self.token_type_key = 'token_type' 27 self.token_type_key = 'token_type'
27 self.expires_key = 'expires_in' 28 self.expires_key = 'expires_in'
29 self.doc_type_map = {
30 'ACCEPTANCE': ('acceptance', conf.ECM_FOLDER_CA),
31 'SETTLEMENT': ('settlement', conf.ECM_FOLDER_SE),
32 'CONTRACTMANAGEMENT': ('contract_management', conf.ECM_FOLDER_CA),
33 }
34 self.doc_base_map = {
35 'AFC': 'SF5_CN',
36 'HIL': 'SF5_CL',
37 }
38 self.prefix = 'OCR'
39 self.upload_fields = ["b_region", "b_region_name", "r_object_type", "r_content_type", "r_creation_date",
40 "r_creator_name", "r_modify_date", "r_modifier", "owner", "b_short_application_no",
41 "b_short_contract_no", "b_customer_id", "b_customer_name", "b_customer_mobile",
42 "b_coborrower_id", "b_coborrower_name", "b_guarantor_id", "b_guarantor_name",
43 "b_frontend_partner", "b_dealer_code", "b_dealer_name", "b_input_date", "b_comment",
44 "b_contract_no", "b_location"]
28 45
29 def update_oauth_token(self): 46 def update_oauth_token(self):
30 response = requests.post(self.oauth_url, headers=self.oauth_headers, data=self.oauth_payload, verify=False) 47 response = requests.post(self.oauth_url, headers=self.oauth_headers, data=self.oauth_payload, verify=False)
...@@ -46,20 +63,21 @@ class ECM: ...@@ -46,20 +63,21 @@ class ECM:
46 self.update_oauth_token() 63 self.update_oauth_token()
47 return self.oauth_token 64 return self.oauth_token
48 65
49 def download(self, save_path, doc_base, doc_type, object_id): 66 def get_headers(self):
50 download_headers = { 67 return {'Authorization': '{0} {1}'.format(self.token_type, self.get_oauth_token())}
51 'Authorization': '{0} {1}'.format(self.token_type, self.get_oauth_token()) 68
52 } 69 def download(self, save_path, object_id, document_scheme, business_type):
70 doc_type, _ = self.doc_type_map.get(document_scheme)
53 download_json = { 71 download_json = {
54 "userName": self.username, 72 "userName": self.username,
55 "password": self.pwd, 73 "password": self.pwd,
56 "docbase": doc_base, 74 "docbase": self.doc_base_map.get(business_type),
57 "documentType": doc_type, 75 "documentType": doc_type,
58 "objectId": object_id, 76 "objectId": object_id,
59 } 77 }
60 response = requests.post(self.download_url, headers=download_headers, json=download_json, verify=False) 78 response = requests.post(self.download_url, headers=self.get_headers(), json=download_json, verify=False)
61 if response.status_code != 200: 79 if response.status_code != 200:
62 raise ECMException('ECM download response with code: {0}'.format(response.status_code)) 80 raise ECMException('ECM download failed with code: {0}'.format(response.status_code))
63 base64_data = response.json().get('Envelope', {}).get('Body', {}).get('getResponse', {}).get('return', {}).get( 81 base64_data = response.json().get('Envelope', {}).get('Body', {}).get('getResponse', {}).get('return', {}).get(
64 'DataObjects', {}).get('Contents', {}).get('Value') 82 'DataObjects', {}).get('Contents', {}).get('Value')
65 if not isinstance(base64_data, str): 83 if not isinstance(base64_data, str):
...@@ -67,17 +85,38 @@ class ECM: ...@@ -67,17 +85,38 @@ class ECM:
67 with open(save_path, "wb") as fh: 85 with open(save_path, "wb") as fh:
68 fh.write(base64.b64decode(base64_data.encode())) 86 fh.write(base64.b64decode(base64_data.encode()))
69 87
70 def upload(self): 88 def get_doc_file_name(self, doc_name):
71 { 89 if not isinstance(doc_name, str):
72 "username": "fanliubing", 90 return self.prefix
73 "password": "Bmwecm123456&api1", 91 if doc_name.endswith('.pdf') or doc_name.endswith('.PDF') or \
74 "docbase": "SF5_CN", 92 doc_name.endswith('.pdF') or doc_name.endswith('.pDF') or doc_name.endswith('.pDf') or \
75 "documentType": "acceptance", 93 doc_name.endswith('.Pdf') or doc_name.endswith('.PdF') or doc_name.endswith('.PDf'):
76 "object_name": "JoeMyPDF202109061529.pdf", 94 name, _ = os.path.splitext(doc_name)
77 "folder": "/Wholesale/Operations/Audits", 95 return '{0}{1}'.format(self.prefix, name)
78 "format": "pdf", 96 return '{0}{1}'.format(self.prefix, doc_name)
79 "b_application_no": "", 97
98 def upload(self, file_path, doc, business_type):
99 doc_type, folder = self.doc_type_map.get(doc.document_scheme)
100 args = {
101 "username": self.username,
102 "password": self.pwd,
103 "docbase": self.doc_base_map.get(business_type),
104 "documentType": doc_type,
105 "object_name": self.get_doc_file_name(doc.document_name),
106 "folder": folder,
107 "format": "xlsx",
108 "b_application_no": doc.application_id,
80 "file_base64_content": "", 109 "file_base64_content": "",
81 } 110 }
82 111 for key in self.upload_fields:
83 112 args[key] = ''
113 with open(file_path, 'rb') as f:
114 base64_data = base64.b64encode(f.read())
115 # 获取解码后的base64值
116 file_data = base64_data.decode()
117 args['file_base64_content'] = file_data
118 response = requests.post(self.upload_url, headers=self.get_headers(), json=args, verify=False)
119 if response.status_code != 200:
120 raise ECMException('ECM upload failed with code: {0}'.format(response.status_code))
121 if 'ns6:createResponse' not in response.json().get('S:Envelope', {}).get('S:Body', {}):
122 raise ECMException('ECM upload failed: {0}'.format(response.json()))
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!