ecm.py
5.68 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
import base64
import requests
from common.redis_cache import redis_handler as rh
from settings import conf
from apps.doc.exceptions import ECMException
class ECM:
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.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,
}
self.token_key = 'access_token'
self.token_type = 'Bearer'
self.token_type_key = 'token_type'
self.expires_key = 'expires_in'
self.doc_type_map = {
'ACCEPTANCE': ('acceptance', conf.ECM_FOLDER_CA),
'SETTLEMENT': ('settlement', conf.ECM_FOLDER_SE),
'CONTRACTMANAGEMENT': ('contract_management', conf.ECM_FOLDER_CA),
}
self.doc_base_map = {
'AFC': 'SF5_CN',
'HIL': 'SF5_CL',
}
self.prefix = 'OCR'
self.upload_fields = ["b_region", "b_region_name", "r_object_type", "r_content_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_input_date", "b_comment",
"b_contract_no", "b_location"]
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)
rh.set_ecm_token(self.oauth_token, expires)
def get_oauth_token(self):
if self.oauth_token is None:
# redis获取token
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())}
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,
}
response = requests.post(self.download_url, headers=self.get_headers(), 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):
doc_type, folder = self.doc_type_map.get(doc.document_scheme)
args = {
"username": self.username,
"password": self.pwd,
"docbase": self.doc_base_map.get(business_type),
"documentType": doc_type,
"object_name": self.get_doc_file_name(doc.document_name),
"folder": folder,
"format": "xlsx",
"b_application_no": doc.application_id,
"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
response = requests.post(self.upload_url, headers=self.get_headers(), json=args, verify=False)
if response.status_code != 200:
raise ECMException('ECM upload failed with code: {0}'.format(response.status_code))
if 'ns6:createResponse' not in response.json().get('S:Envelope', {}).get('S:Body', {}):
raise ECMException('ECM upload failed: {0}'.format(response.json()))