eb2d5a51 by 周伟奇

add eDMS

1 parent 639ea2eb
...@@ -8,4 +8,8 @@ HIL_SET = {'HIL', 'HIl', 'HiL', 'Hil', 'hIL', 'hIl', 'hiL', 'hil', 'CO00002'} ...@@ -8,4 +8,8 @@ HIL_SET = {'HIL', 'HIl', 'HiL', 'Hil', 'hIL', 'hIl', 'hiL', 'hil', 'CO00002'}
8 HIL_PREFIX = 'HIL' 8 HIL_PREFIX = 'HIL'
9 AFC_PREFIX = 'AFC' 9 AFC_PREFIX = 'AFC'
10 10
11 SESSION_PREFIX = 'FHLSID'
11 CUSTOM_CLIENT = 'CustomClient' 12 CUSTOM_CLIENT = 'CustomClient'
13 FIXED_TOKEN = '00000000-0000-0000-0000-000000000000'
14 FIXED_FILE_SIZE = 0
15 DOWNLOAD_ACTION_TYPE = 'Downloaded'
......
1 import requests
2 from zeep import Client
3 from settings import conf
4 from . import consts
5
6
7 class EDMS:
8
9 def __init__(self, user_name, pwd):
10 self.sm_client = Client(wsdl=conf.SM_WSDL)
11 self.dm_client = Client(wsdl=conf.DM_WSDL)
12 self.df_client = Client(wsdl=conf.DF_WSDL)
13 self.download_url = conf.EDMS_DOWNLOAD_URL
14 self.user_name = user_name
15 self.pwd = pwd
16 self.session_id = None
17
18 def get_session_id(self):
19 self.session_id = self.sm_client.service.StartSession(login=self.user_name,
20 password=self.pwd,
21 clientType=consts.CUSTOM_CLIENT)
22 return self.session_id
23
24 def get_download_token(self, headers, metadata_version_id):
25 with self.dm_client.settings(extra_http_headers=headers):
26 res = self.dm_client.service.PrepareSingleDocumentToDownload(metadataVersionId=metadata_version_id,
27 token=consts.FIXED_TOKEN,
28 fileSize=consts.FIXED_FILE_SIZE,
29 actionType=consts.DOWNLOAD_ACTION_TYPE)
30 return res.token
31
32 def download(self, save_path, metadata_version_id):
33 session_id = self.get_session_id()
34 headers = {'Cookie': '{0}={1}'.format(consts.SESSION_PREFIX, session_id)}
35 token = self.get_download_token(headers, metadata_version_id)
36 params = {'token': token}
37
38 r = requests.get(self.download_url, params=params, headers=headers, stream=True)
39 with open(save_path, "wb") as f:
40 # chunk是指定每次写入的大小,每次只写了512byte
41 for chunk in r.iter_content(chunk_size=512):
42 if chunk:
43 f.write(chunk)
44 f.flush()
45
...@@ -17,6 +17,7 @@ from common.tools.file_tools import write_zip_file ...@@ -17,6 +17,7 @@ from common.tools.file_tools import write_zip_file
17 from apps.doc.models import DocStatus, HILDoc, AFCDoc 17 from apps.doc.models import DocStatus, HILDoc, AFCDoc
18 from apps.doc import consts 18 from apps.doc import consts
19 from settings import conf 19 from settings import conf
20 from apps.doc.edms import EDMS
20 21
21 22
22 class Command(BaseCommand, LoggerMixin): 23 class Command(BaseCommand, LoggerMixin):
...@@ -39,7 +40,7 @@ class Command(BaseCommand, LoggerMixin): ...@@ -39,7 +40,7 @@ class Command(BaseCommand, LoggerMixin):
39 'Content-Type': 'application/json' 40 'Content-Type': 'application/json'
40 } 41 }
41 # EDMS web_service_api 42 # EDMS web_service_api
42 self.sm_client = Client(wsdl=conf.SM_WSDL) 43 self.edms = EDMS(conf.EDMS_USER, conf.EDMS_PWD)
43 # 优雅退出信号:15 44 # 优雅退出信号:15
44 signal.signal(signal.SIGTERM, self.signal_handler) 45 signal.signal(signal.SIGTERM, self.signal_handler)
45 46
...@@ -70,9 +71,6 @@ class Command(BaseCommand, LoggerMixin): ...@@ -70,9 +71,6 @@ class Command(BaseCommand, LoggerMixin):
70 if doc_info is None: 71 if doc_info is None:
71 return None, None, None 72 return None, None, None
72 # TODO EDMS下载pdf 73 # TODO EDMS下载pdf
73 # session_id = self.sm_client.service.StartSession(login=conf.EDMS_USER,
74 # password=conf.EDMS_PWD,
75 # clientType=consts.CUSTOM_CLIENT)
76 74
77 doc_data_path = os.path.join(self.data_dir, business_type, str(doc_id)) 75 doc_data_path = os.path.join(self.data_dir, business_type, str(doc_id))
78 pdf_path = os.path.join(doc_data_path, '{0}.pdf'.format(doc_id)) 76 pdf_path = os.path.join(doc_data_path, '{0}.pdf'.format(doc_id))
...@@ -200,16 +198,20 @@ class Command(BaseCommand, LoggerMixin): ...@@ -200,16 +198,20 @@ class Command(BaseCommand, LoggerMixin):
200 def handle(self, *args, **kwargs): 198 def handle(self, *args, **kwargs):
201 sleep_second = int(conf.SLEEP_SECOND) 199 sleep_second = int(conf.SLEEP_SECOND)
202 max_sleep_second = int(conf.MAX_SLEEP_SECOND) 200 max_sleep_second = int(conf.MAX_SLEEP_SECOND)
201
203 while self.switch: 202 while self.switch:
204 # 1. 从队列获取文件信息 203 # 1. 从队列获取文件信息
205 doc_info, doc_class, doc_id, business_type = self.get_doc_info() 204 doc_info, doc_class, doc_id, business_type = self.get_doc_info()
205
206 # 2. 从EDMS获取PDF文件 206 # 2. 从EDMS获取PDF文件
207 doc_data_path, excel_path, pdf_path = self.pdf_download(doc_id, doc_info, business_type) 207 doc_data_path, excel_path, pdf_path = self.pdf_download(doc_id, doc_info, business_type)
208
208 # 队列为空时的处理 209 # 队列为空时的处理
209 if pdf_path is None: 210 if pdf_path is None:
210 time.sleep(sleep_second) 211 time.sleep(sleep_second)
211 sleep_second = min(max_sleep_second, sleep_second+5) 212 sleep_second = min(max_sleep_second, sleep_second+5)
212 continue 213 continue
214
213 sleep_second = int(conf.SLEEP_SECOND) 215 sleep_second = int(conf.SLEEP_SECOND)
214 try: 216 try:
215 # 3.PDF文件提取图片 217 # 3.PDF文件提取图片
...@@ -280,6 +282,7 @@ class Command(BaseCommand, LoggerMixin): ...@@ -280,6 +282,7 @@ class Command(BaseCommand, LoggerMixin):
280 self.cronjob_log.info('{0} [pdf to img success] [doc_id={1}]'.format(self.log_base, doc_id)) 282 self.cronjob_log.info('{0} [pdf to img success] [doc_id={1}]'.format(self.log_base, doc_id))
281 283
282 write_zip_file(img_save_path, os.path.join(doc_data_path, '{0}_img.zip'.format(doc_id))) 284 write_zip_file(img_save_path, os.path.join(doc_data_path, '{0}_img.zip'.format(doc_id)))
285
283 # 4.图片调用算法判断是否为银行流水, 图片调用算法OCR为excel文件 286 # 4.图片调用算法判断是否为银行流水, 图片调用算法OCR为excel文件
284 wb = xlwt.Workbook() 287 wb = xlwt.Workbook()
285 loop = asyncio.get_event_loop() 288 loop = asyncio.get_event_loop()
...@@ -288,7 +291,9 @@ class Command(BaseCommand, LoggerMixin): ...@@ -288,7 +291,9 @@ class Command(BaseCommand, LoggerMixin):
288 # loop.close() 291 # loop.close()
289 wb.save(excel_path) # TODO no sheet (res always []) 292 wb.save(excel_path) # TODO no sheet (res always [])
290 # 整合excel文件 293 # 整合excel文件
294
291 # 5.上传至EDMS 295 # 5.上传至EDMS
296
292 except Exception as e: 297 except Exception as e:
293 doc_class.objects.filter(id=doc_id).update(status=DocStatus.PROCESS_FAILED.value) 298 doc_class.objects.filter(id=doc_id).update(status=DocStatus.PROCESS_FAILED.value)
294 self.cronjob_log.error('{0} [process failed] [doc_id={1}] [err={2}]'.format(self.log_base, doc_id, e)) 299 self.cronjob_log.error('{0} [process failed] [doc_id={1}] [err={2}]'.format(self.log_base, doc_id, e))
......
...@@ -9,6 +9,8 @@ WSDL_DIR = os.path.join(os.path.dirname(BASE_DIR), 'wsdl') ...@@ -9,6 +9,8 @@ WSDL_DIR = os.path.join(os.path.dirname(BASE_DIR), 'wsdl')
9 SECRET_CONF_FILE = os.path.join(SECRET_CONF_DIR, 'secret.ini') 9 SECRET_CONF_FILE = os.path.join(SECRET_CONF_DIR, 'secret.ini')
10 LOGGING_CONFIG_FILE = os.path.join(COMMON_CONF_DIR, 'logging.conf') 10 LOGGING_CONFIG_FILE = os.path.join(COMMON_CONF_DIR, 'logging.conf')
11 SM_WSDL = os.path.join(WSDL_DIR, 'SessionManager.wsdl') 11 SM_WSDL = os.path.join(WSDL_DIR, 'SessionManager.wsdl')
12 DM_WSDL = os.path.join(WSDL_DIR, 'DocumentManager.wsdl')
13 DF_WSDL = os.path.join(WSDL_DIR, 'DocumentFinder.wsdl')
12 14
13 # 文件存放根目录 15 # 文件存放根目录
14 LOG_DIR = os.path.join(os.path.dirname(BASE_DIR), 'logs') 16 LOG_DIR = os.path.join(os.path.dirname(BASE_DIR), 'logs')
......
1 [settings] 1 [settings]
2 DEBUG = False 2 DEBUG = False
3 SLEEP_SECOND = 5 3 SLEEP_SECOND = 5
4 MAX_SLEEP_SECOND = 60
...\ No newline at end of file ...\ No newline at end of file
4 MAX_SLEEP_SECOND = 60
5
6 EDMS_DOWNLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/DownloadHandler.ashx
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -2,3 +2,5 @@ ...@@ -2,3 +2,5 @@
2 DEBUG = True 2 DEBUG = True
3 SLEEP_SECOND = 5 3 SLEEP_SECOND = 5
4 MAX_SLEEP_SECOND = 60 4 MAX_SLEEP_SECOND = 60
5
6 EDMS_DOWNLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/DownloadHandler.ashx
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -2,3 +2,5 @@ ...@@ -2,3 +2,5 @@
2 DEBUG = False 2 DEBUG = False
3 SLEEP_SECOND = 5 3 SLEEP_SECOND = 5
4 MAX_SLEEP_SECOND = 60 4 MAX_SLEEP_SECOND = 60
5
6 EDMS_DOWNLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/DownloadHandler.ashx
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!