ecm.py
9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import os
import time
import base64
import requests
from common.redis_cache import redis_handler as rh
from settings import conf
from apps.doc.exceptions import ECMException
from common.mixins import GenericView
class ECM(GenericView):
def __init__(self):
self.oauth_token = None
self.username = conf.ECM_USER
self.pwd = conf.ECM_PWD
self.oauth_url = conf.ECM_OAUTH_URL
self.download_url = conf.ECM_DOWNLOAD_URL
self.upload_url = conf.ECM_UPLOAD_URL
self.search_url = conf.ECM_SEARCH_URL
self.oauth_headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
self.oauth_payload = {
'grant_type': 'client_credentials',
'client_id': conf.ECM_OAUTH_ID,
'client_secret': conf.ECM_OAUTH_SECRET,
'scope': conf.ECM_OAUTH_SCOPE,
}
self.token_key = 'access_token'
self.token_type = 'Bearer'
self.token_type_key = 'token_type'
self.expires_key = 'expires_in'
self.settlement_type = 'settlement'
self.doc_type_map = {
'ACCEPTANCE': ('acceptance', conf.ECM_FOLDER_CA, conf.ECM_FOLDER_CA_HIL),
'SETTLEMENT': (self.settlement_type, conf.ECM_FOLDER_SE, conf.ECM_FOLDER_SE_HIL),
'CONTRACTMANAGEMENT': ('contract_management', conf.ECM_FOLDER_CA, conf.ECM_FOLDER_CA_HIL),
'INSURANCE': ('insurance', conf.ECM_FOLDER_SE, conf.ECM_FOLDER_SE_HIL),
'CONTRACT_MANAGEMENT': ('contract_management', conf.ECM_FOLDER_CA, conf.ECM_FOLDER_CA_HIL),
'DE_MORTGAGE': ('de_mortgage', conf.ECM_FOLDER_SE, conf.ECM_FOLDER_SE_HIL),
'ASP_PT': ('asp_pt', conf.ECM_FOLDER_SE, conf.ECM_FOLDER_SE_HIL),
'ASP_INVOICE': ('asp_invoice', conf.ECM_FOLDER_SE, conf.ECM_FOLDER_SE_HIL),
}
self.doc_base_map = {
'AFC': 'SF5_CN',
'HIL': 'SF5_CL',
}
self.b_region_name_map = {
'AFC': 'CN',
'HIL': 'CL',
}
self.prefix = 'OCR'
self.upload_fields = ["r_object_type", "r_creation_date",
"r_creator_name", "r_modify_date", "r_modifier", "owner", "b_short_application_no",
"b_short_contract_no", "b_customer_id", "b_customer_name", "b_customer_mobile",
"b_coborrower_id", "b_coborrower_name", "b_guarantor_id", "b_guarantor_name",
"b_frontend_partner", "b_dealer_code", "b_dealer_name", "b_comment",
"b_contract_no", "b_location", "b_company_name", "b_certificate_code", "b_vin",
"b_registration_no", "b_F2I_name"]
self.log_base = '[ecm]'
def update_oauth_token(self):
response = requests.post(self.oauth_url, headers=self.oauth_headers, data=self.oauth_payload, verify=False)
if response.status_code != 200:
raise ECMException('ECM Oauth response with code: {0}'.format(response.status_code))
token = response.json().get(self.token_key)
if not isinstance(token, str):
raise ECMException('ECM Oauth can not get token: {0}'.format(response.json()))
self.oauth_token = token
self.token_type = response.json().get(self.token_type_key, self.token_type)
expires = response.json().get(self.expires_key, 3600)
if isinstance(expires, int):
expires_int = expires - 10
elif isinstance(expires, str):
expires_int = int(expires) - 10
else:
expires_int = 3600 - 10
rh.set_ecm_token(self.oauth_token, expires_int)
def get_oauth_token(self):
# if self.oauth_token is None:
# redis获取token
self.oauth_token = None
#self.oauth_token = rh.get_ecm_token()
if self.oauth_token is None:
self.update_oauth_token()
return self.oauth_token
def get_headers(self):
return {
'Authorization': '{0} {1}'.format(self.token_type, self.get_oauth_token()),
'client_id': conf.ECM_OAUTH_ID,
'client_secret': conf.ECM_OAUTH_SECRET
}
def download(self, save_path, object_id, document_scheme, business_type):
doc_type, _, _ = self.doc_type_map.get(document_scheme)
download_json = {
"userName": self.username,
"password": self.pwd,
"docbase": self.doc_base_map.get(business_type),
"documentType": doc_type,
"objectId": object_id,
"b_input_date": time.strftime("%m/%d/%Y %X"),
"b_credit_signing_date": time.strftime("%m/%d/%Y %X"),
"b_credit_check": True,
"b_id_number": '',
}
header_info = self.get_headers()
self.running_log.info("{0} download header_info:{1}".format(self.log_base, header_info))
self.running_log.info("{0} download args_info:{1}".format(self.log_base, download_json))
response = requests.post(self.download_url, headers=header_info, json=download_json, verify=False)
if response.status_code != 200:
raise ECMException('ECM download failed with code: {0}'.format(response.status_code))
base64_data = response.json().get('Envelope', {}).get('Body', {}).get('getResponse', {}).get('return', {}).get(
'DataObjects', {}).get('Contents', {}).get('Value')
if not isinstance(base64_data, str):
raise ECMException('ECM download failed: {0}'.format(response.json()))
with open(save_path, "wb") as fh:
fh.write(base64.b64decode(base64_data.encode()))
def get_doc_file_name(self, doc_name):
if not isinstance(doc_name, str):
return self.prefix
if doc_name.endswith('.pdf') or doc_name.endswith('.PDF') or \
doc_name.endswith('.pdF') or doc_name.endswith('.pDF') or doc_name.endswith('.pDf') or \
doc_name.endswith('.Pdf') or doc_name.endswith('.PdF') or doc_name.endswith('.PDf'):
name, _ = os.path.splitext(doc_name)
return '{0}{1}'.format(self.prefix, name)
return '{0}{1}'.format(self.prefix, doc_name)
def upload(self, file_path, doc, business_type, need_follow):
doc_type, folder_afc, folder_hil = self.doc_type_map.get(doc.document_scheme)
folder = folder_afc if business_type == 'AFC' else folder_hil
object_name = '关注' + self.get_doc_file_name(doc.document_name) if need_follow else self.get_doc_file_name(doc.document_name)
args = {
"username": self.username,
"password": self.pwd,
"docbase": self.doc_base_map.get(business_type),
"documentType": doc_type,
"object_name": object_name,
"folder": folder,
"format": "excel12book",
"r_content_type": "excel12book",
"b_application_no": doc.application_id,
"b_region": "0",
"b_region_name": self.b_region_name_map.get(business_type),
"b_input_date": time.strftime("%m/%d/%Y %X"),
"b_credit_signing_date": time.strftime("%m/%d/%Y %X"),
"b_credit_check": True,
"b_id_number": '',
# "file_base64_content": "",
}
for key in self.upload_fields:
args[key] = ''
with open(file_path, 'rb') as f:
base64_data = base64.b64encode(f.read())
# 获取解码后的base64值
file_data = base64_data.decode()
args['file_base64_content'] = file_data
header_info = self.get_headers()
self.running_log.info("{0} upload header_info:{1}".format(self.log_base, header_info))
self.running_log.info("{0} upload args_info:{1}".format(self.log_base, args))
response = requests.post(self.upload_url, headers=header_info, json=args, verify=False)
if response.status_code != 200:
raise ECMException('ECM upload failed with code: {0} , with headers: {1} , with content: {2}'.format(
response.status_code, response.headers, response.text))
if 'ns12:createResponse' not in response.json().get('S:Envelope', {}).get('S:Body', {}):
raise ECMException('ECM upload failed: {0} , with headers: {1}'.format(response.json(), response.headers))
def search_doc_info_list(self, filePath, business_type):
args = {
#userName n大写,和其他接口不一样,是因为apigateway没有做统一
"userName": self.username,
"password": self.pwd,
"docbase": self.doc_base_map.get(business_type),
"documentType": "green_book",
"dql":"select r_object_id, object_name,b_application_no, r_object_type,b_customer_name,r_content_size, owner_name, b_input_date, r_modify_date, b_location from green_book where b_location = '{}'" .format(filePath),
}
header_info = self.get_headers()
self.running_log.info("{0} search header_info:{1}".format(self.log_base, header_info))
self.running_log.info("{0} search args_info:{1}".format(self.log_base, args))
response = requests.post(self.search_url, headers=header_info, json=args, verify=False)
if response.status_code != 200:
raise ECMException('ECM search failed with code: {0}'.format(response.status_code))
#self.running_log.info("{0} search response.json():{1}".format(self.log_base, response.json()))
return response.json()