session_id cache
Showing
5 changed files
with
37 additions
and
17 deletions
| ... | @@ -2,6 +2,7 @@ import requests | ... | @@ -2,6 +2,7 @@ import requests |
| 2 | from zeep import Client | 2 | from zeep import Client |
| 3 | from settings import conf | 3 | from settings import conf |
| 4 | from . import consts | 4 | from . import consts |
| 5 | from common.redis_cache import redis_handler as rh | ||
| 5 | 6 | ||
| 6 | 7 | ||
| 7 | class EDMS: | 8 | class EDMS: |
| ... | @@ -15,12 +16,25 @@ class EDMS: | ... | @@ -15,12 +16,25 @@ class EDMS: |
| 15 | self.pwd = pwd | 16 | self.pwd = pwd |
| 16 | self.session_id = None | 17 | self.session_id = None |
| 17 | 18 | ||
| 18 | def get_session_id(self): | 19 | def set_session_id(self): |
| 19 | self.session_id = self.sm_client.service.StartSession(login=self.user_name, | 20 | self.session_id = self.sm_client.service.StartSession(login=self.user_name, |
| 20 | password=self.pwd, | 21 | password=self.pwd, |
| 21 | clientType=consts.CUSTOM_CLIENT) | 22 | clientType=consts.CUSTOM_CLIENT) |
| 23 | rh.set_session_id(self.session_id) | ||
| 22 | return self.session_id | 24 | return self.session_id |
| 23 | 25 | ||
| 26 | def get_session_id(self): | ||
| 27 | if self.session_id is None: | ||
| 28 | # redis获取session_id | ||
| 29 | self.session_id = rh.get_session_id() | ||
| 30 | if self.session_id is None: | ||
| 31 | return self.set_session_id() | ||
| 32 | # 验证session_id | ||
| 33 | is_session_valid = self.sm_client.service.IsSessionValid(sessionId=self.session_id, keepAlive=False) | ||
| 34 | if is_session_valid is True: | ||
| 35 | return self.session_id | ||
| 36 | return self.set_session_id() | ||
| 37 | |||
| 24 | def get_download_token(self, headers, metadata_version_id): | 38 | def get_download_token(self, headers, metadata_version_id): |
| 25 | with self.dm_client.settings(extra_http_headers=headers): | 39 | with self.dm_client.settings(extra_http_headers=headers): |
| 26 | res = self.dm_client.service.PrepareSingleDocumentToDownload(metadataVersionId=metadata_version_id, | 40 | res = self.dm_client.service.PrepareSingleDocumentToDownload(metadataVersionId=metadata_version_id, | ... | ... |
| ... | @@ -12,12 +12,11 @@ from zeep import Client | ... | @@ -12,12 +12,11 @@ from zeep import Client |
| 12 | 12 | ||
| 13 | from django.core.management import BaseCommand | 13 | from django.core.management import BaseCommand |
| 14 | from common.mixins import LoggerMixin | 14 | from common.mixins import LoggerMixin |
| 15 | from common.redis_cache import redis_handler as rh | ||
| 16 | from common.tools.file_tools import write_zip_file | 15 | from common.tools.file_tools import write_zip_file |
| 17 | from apps.doc.models import DocStatus, HILDoc, AFCDoc | 16 | from apps.doc.models import DocStatus, HILDoc, AFCDoc |
| 18 | from apps.doc import consts | 17 | from apps.doc import consts |
| 19 | from settings import conf | 18 | from settings import conf |
| 20 | from apps.doc.edms import EDMS | 19 | from apps.doc.edms import EDMS, rh |
| 21 | 20 | ||
| 22 | 21 | ||
| 23 | class Command(BaseCommand, LoggerMixin): | 22 | class Command(BaseCommand, LoggerMixin): |
| ... | @@ -57,7 +56,7 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -57,7 +56,7 @@ class Command(BaseCommand, LoggerMixin): |
| 57 | doc_id = int(doc_id_str) | 56 | doc_id = int(doc_id_str) |
| 58 | doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc | 57 | doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc |
| 59 | doc_info = doc_class.objects.filter(id=doc_id, status=DocStatus.INIT.value).values( | 58 | doc_info = doc_class.objects.filter(id=doc_id, status=DocStatus.INIT.value).values( |
| 60 | 'id', 'metadata_version_id', 'document_name').first() | 59 | 'id', 'metadata_version_id', 'application_id', 'document_name').first() |
| 61 | if doc_info is None: | 60 | if doc_info is None: |
| 62 | self.cronjob_log.warn('{0} [get_doc_info] [doc completed] [task_str={1}] [is_priority={2}]'.format( | 61 | self.cronjob_log.warn('{0} [get_doc_info] [doc completed] [task_str={1}] [is_priority={2}]'.format( |
| 63 | self.log_base, task_str, is_priority)) | 62 | self.log_base, task_str, is_priority)) |
| ... | @@ -125,7 +124,7 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -125,7 +124,7 @@ class Command(BaseCommand, LoggerMixin): |
| 125 | s = item[1] # xref of its /SMask | 124 | s = item[1] # xref of its /SMask |
| 126 | is_rgb = True if item[5] == 'DeviceRGB' else False | 125 | is_rgb = True if item[5] == 'DeviceRGB' else False |
| 127 | 126 | ||
| 128 | # RGB | 127 | # GRAY/RGB # TODO 颜色空间不同处理 |
| 129 | if is_rgb: | 128 | if is_rgb: |
| 130 | if s == 0: | 129 | if s == 0: |
| 131 | return doc.extractImage(x) | 130 | return doc.extractImage(x) |
| ... | @@ -143,7 +142,7 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -143,7 +142,7 @@ class Command(BaseCommand, LoggerMixin): |
| 143 | pix1 = pix2 = None # free temp pixmaps | 142 | pix1 = pix2 = None # free temp pixmaps |
| 144 | return self.getimage(pix) | 143 | return self.getimage(pix) |
| 145 | 144 | ||
| 146 | # GRAY/CMYK | 145 | # CMYK |
| 147 | pix1 = fitz.Pixmap(doc, x) | 146 | pix1 = fitz.Pixmap(doc, x) |
| 148 | pix = fitz.Pixmap(pix1) # copy of pix1, alpha channel added | 147 | pix = fitz.Pixmap(pix1) # copy of pix1, alpha channel added |
| 149 | 148 | ||
| ... | @@ -159,7 +158,7 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -159,7 +158,7 @@ class Command(BaseCommand, LoggerMixin): |
| 159 | 158 | ||
| 160 | pix1 = pix2 = None # free temp pixmaps | 159 | pix1 = pix2 = None # free temp pixmaps |
| 161 | 160 | ||
| 162 | pix = fitz.Pixmap(fitz.csRGB, pix) # GRAY/CMYK to RGB | 161 | pix = fitz.Pixmap(fitz.csRGB, pix) # CMYK to RGB |
| 163 | return self.getimage(pix) | 162 | return self.getimage(pix) |
| 164 | 163 | ||
| 165 | @staticmethod | 164 | @staticmethod | ... | ... |
| ... | @@ -6,22 +6,22 @@ from . import consts | ... | @@ -6,22 +6,22 @@ from . import consts |
| 6 | class DocHandler: | 6 | class DocHandler: |
| 7 | 7 | ||
| 8 | @staticmethod | 8 | @staticmethod |
| 9 | def get_link(doc_id, file='pdf'): | 9 | def get_link(doc_id, business_type, file='pdf'): |
| 10 | if file == 'pdf': | 10 | if file == 'pdf': |
| 11 | return '/data/{0}/{0}.pdf'.format(doc_id) | 11 | return '/data/{1}/{0}/{0}.pdf'.format(doc_id, business_type) |
| 12 | elif file == 'img': | 12 | elif file == 'img': |
| 13 | return '/data/{0}/{0}_img.zip'.format(doc_id) | 13 | return '/data/{1}/{0}/{0}_img.zip'.format(doc_id, business_type) |
| 14 | else: | 14 | else: |
| 15 | return '/data/{0}/{0}.xls'.format(doc_id) | 15 | return '/data/{1}/{0}/{0}.xls'.format(doc_id, business_type) |
| 16 | 16 | ||
| 17 | def get_doc_list(self, doc_queryset): | 17 | def get_doc_list(self, doc_queryset, business_type): |
| 18 | for doc_dict in doc_queryset: | 18 | for doc_dict in doc_queryset: |
| 19 | if doc_dict['status'] != DocStatus.COMPLETE.value: | 19 | if doc_dict['status'] != DocStatus.COMPLETE.value: |
| 20 | continue | 20 | continue |
| 21 | doc_id = doc_dict.get('id') | 21 | doc_id = doc_dict.get('id') |
| 22 | doc_dict['pdf_link'] = self.get_link(doc_id) | 22 | doc_dict['pdf_link'] = self.get_link(doc_id, business_type) |
| 23 | doc_dict['img_link'] = self.get_link(doc_id, 'img') | 23 | doc_dict['img_link'] = self.get_link(doc_id, business_type, file='img') |
| 24 | doc_dict['excel_link'] = self.get_link(doc_id, 'excel') | 24 | doc_dict['excel_link'] = self.get_link(doc_id, business_type, file='excel') |
| 25 | return list(doc_queryset) | 25 | return list(doc_queryset) |
| 26 | 26 | ||
| 27 | @staticmethod | 27 | @staticmethod | ... | ... |
| ... | @@ -247,9 +247,9 @@ class DocView(GenericView, DocHandler): | ... | @@ -247,9 +247,9 @@ class DocView(GenericView, DocHandler): |
| 247 | if create_time_start is not None and create_time_end is not None else Q() | 247 | if create_time_start is not None and create_time_end is not None else Q() |
| 248 | query = application_id_query & status_query & data_source_query & upload_finish_time_query & create_time_query | 248 | query = application_id_query & status_query & data_source_query & upload_finish_time_query & create_time_query |
| 249 | val_tuple = ('id', 'application_id', 'upload_finish_time', 'create_time', 'data_source', 'status') | 249 | val_tuple = ('id', 'application_id', 'upload_finish_time', 'create_time', 'data_source', 'status') |
| 250 | doc_class, _ = self.get_doc_class(business_type) | 250 | doc_class, prefix = self.get_doc_class(business_type) |
| 251 | doc_queryset = doc_class.objects.filter(query).values(*val_tuple).order_by('-upload_finish_time') | 251 | doc_queryset = doc_class.objects.filter(query).values(*val_tuple).order_by('-upload_finish_time') |
| 252 | doc_list = self.get_doc_list(doc_queryset) | 252 | doc_list = self.get_doc_list(doc_queryset, prefix) |
| 253 | 253 | ||
| 254 | total = len(doc_list) | 254 | total = len(doc_list) |
| 255 | start_index = page_size * (page - 1) | 255 | start_index = page_size * (page - 1) | ... | ... |
| ... | @@ -35,6 +35,7 @@ class RedisHandler: | ... | @@ -35,6 +35,7 @@ class RedisHandler: |
| 35 | self.prefix = 'bwm_ocr' | 35 | self.prefix = 'bwm_ocr' |
| 36 | self.common_queue_key = '{0}:common_queue'.format(self.prefix) | 36 | self.common_queue_key = '{0}:common_queue'.format(self.prefix) |
| 37 | self.priority_queue_key = '{0}:priority_queue'.format(self.prefix) | 37 | self.priority_queue_key = '{0}:priority_queue'.format(self.prefix) |
| 38 | self.session_id_key = '{0}:session_id'.format(self.prefix) | ||
| 38 | 39 | ||
| 39 | def enqueue(self, tasks, is_priority=False): | 40 | def enqueue(self, tasks, is_priority=False): |
| 40 | # 1 | 41 | # 1 |
| ... | @@ -50,3 +51,9 @@ class RedisHandler: | ... | @@ -50,3 +51,9 @@ class RedisHandler: |
| 50 | is_priority = False | 51 | is_priority = False |
| 51 | return task, is_priority | 52 | return task, is_priority |
| 52 | 53 | ||
| 54 | def set_session_id(self, session_id): | ||
| 55 | return self.redis.set(self.session_id_key, session_id) | ||
| 56 | |||
| 57 | def get_session_id(self): | ||
| 58 | return self.redis.get(self.session_id_key) | ||
| 59 | ... | ... |
-
Please register or sign in to post a comment