Merge branch 'feature/CHINARPA-3529' into feature/uat-tmp
Showing
2 changed files
with
66 additions
and
10 deletions
| ... | @@ -17,7 +17,7 @@ from webargs import fields, validate | ... | @@ -17,7 +17,7 @@ from webargs import fields, validate |
| 17 | from webargs.djangoparser import use_args, parser | 17 | from webargs.djangoparser import use_args, parser |
| 18 | from settings import conf | 18 | from settings import conf |
| 19 | from common import response | 19 | from common import response |
| 20 | from common.mixins import GenericView | 20 | from common.mixins import GenericView,DocGenericView |
| 21 | from common.tools.file_tools import file_write | 21 | from common.tools.file_tools import file_write |
| 22 | from common.redis_cache import redis_handler as rh | 22 | from common.redis_cache import redis_handler as rh |
| 23 | from .models import ( | 23 | from .models import ( |
| ... | @@ -1040,7 +1040,7 @@ class CompareOfflineView(GenericView): | ... | @@ -1040,7 +1040,7 @@ class CompareOfflineView(GenericView): |
| 1040 | ''' | 1040 | ''' |
| 1041 | 1041 | ||
| 1042 | 1042 | ||
| 1043 | class DocView(GenericView, DocHandler): | 1043 | class DocView(DocGenericView, DocHandler): |
| 1044 | 1044 | ||
| 1045 | # 文件列表页 | 1045 | # 文件列表页 |
| 1046 | @use_args(doc_list_args, location='querystring') | 1046 | @use_args(doc_list_args, location='querystring') |
| ... | @@ -1097,21 +1097,21 @@ class DocView(GenericView, DocHandler): | ... | @@ -1097,21 +1097,21 @@ class DocView(GenericView, DocHandler): |
| 1097 | return response.ok(data=res) | 1097 | return response.ok(data=res) |
| 1098 | 1098 | ||
| 1099 | # 上传pdf,模拟下单 | 1099 | # 上传pdf,模拟下单 |
| 1100 | @use_args(upload_pdf_args, location='files') | 1100 | # @use_args(upload_pdf_args) |
| 1101 | def post(self, request, args): | 1101 | def post(self, request): |
| 1102 | random_int = random.randint(0, consts.TIME_NUM) | 1102 | random_int = random.randint(0, consts.TIME_NUM) |
| 1103 | metadata_version_id = str(int(time.time()) - random_int) | 1103 | metadata_version_id = str(int(time.time()) - random_int) |
| 1104 | 1104 | ||
| 1105 | pdf_file = args.get('pdf_file') | 1105 | pdf_file = request.FILES.get('pdf_file') |
| 1106 | if isinstance(pdf_file.name, str): | 1106 | if isinstance(pdf_file.name, str): |
| 1107 | if not pdf_file.name.endswith('pdf') and not pdf_file.name.endswith('PDF'): | 1107 | if not pdf_file.name.endswith('pdf') and not pdf_file.name.endswith('PDF'): |
| 1108 | self.invalid_params(msg='invalid params: not a PDF file') | 1108 | self.invalid_params(msg='invalid params: not a PDF file') |
| 1109 | 1109 | ||
| 1110 | business_type = args.get('business_type', '') | 1110 | business_type = request.POST.get('business_type', '') |
| 1111 | document_scheme = args.get('document_scheme', '') | 1111 | document_scheme = request.POST.get('document_scheme', '') |
| 1112 | data_source = args.get('data_source', '') | 1112 | data_source = request.POST.get('data_source', '') |
| 1113 | document_name = args.get('document_name', '') | 1113 | document_name = request.POST.get('document_name', '') |
| 1114 | 1114 | args = {'business_type':business_type,'document_scheme':document_scheme,'data_source':data_source,'document_name':document_name,'pdf_file':pdf_file} | |
| 1115 | # business_type = random.choice(consts.BUSINESS_TYPE_LIST) | 1115 | # business_type = random.choice(consts.BUSINESS_TYPE_LIST) |
| 1116 | # business_type = consts.BUSINESS_TYPE_LIST[0] | 1116 | # business_type = consts.BUSINESS_TYPE_LIST[0] |
| 1117 | tmp_save_path = os.path.join(conf.DATA_DIR, business_type, '{0}.pdf'.format(metadata_version_id)) | 1117 | tmp_save_path = os.path.join(conf.DATA_DIR, business_type, '{0}.pdf'.format(metadata_version_id)) | ... | ... |
| ... | @@ -104,6 +104,62 @@ class GenericView(LoggerMixin, GenericExceptionMixin, GenericAPIView): | ... | @@ -104,6 +104,62 @@ class GenericView(LoggerMixin, GenericExceptionMixin, GenericAPIView): |
| 104 | def get_object(self): | 104 | def get_object(self): |
| 105 | return None | 105 | return None |
| 106 | 106 | ||
| 107 | class DocGenericView(LoggerMixin, GenericExceptionMixin, GenericAPIView): | ||
| 108 | need_print_logger = True | ||
| 109 | |||
| 110 | def print_logger(self, request): | ||
| 111 | # parameters = getattr(request, request.method, {}) | ||
| 112 | parameters = None | ||
| 113 | if not parameters: | ||
| 114 | parameters = getattr(request, 'data', {}) | ||
| 115 | if not parameters: | ||
| 116 | parameters = {} | ||
| 117 | parameters_string = '' | ||
| 118 | for key, value in parameters.items(): | ||
| 119 | parameters_string += '[%s=%s] ' % (key, value) | ||
| 120 | for key, value in self.kwargs.items(): | ||
| 121 | parameters_string += '[%s=%s] ' % (key, value) | ||
| 122 | if request.user and not isinstance(request.user, AnonymousUser): | ||
| 123 | user_id = request.user.id | ||
| 124 | else: | ||
| 125 | user_id = 0 | ||
| 126 | self.running_log.info('[%s_%s_request] with parameters [user_id=%s] %s' | ||
| 127 | % (self.__class__.__name__, request.method, | ||
| 128 | user_id, parameters_string)) | ||
| 129 | |||
| 130 | def dispatch(self, request, *args, **kwargs): | ||
| 131 | """ | ||
| 132 | `.dispatch()` is pretty much the same as Django's regular dispatch, | ||
| 133 | but with extra hooks for startup, finalize, and exception handling. | ||
| 134 | """ | ||
| 135 | self.args = args | ||
| 136 | self.kwargs = kwargs | ||
| 137 | request = self.initialize_request(request, *args, **kwargs) | ||
| 138 | self.request = request | ||
| 139 | self.headers = self.default_response_headers # deprecate? | ||
| 140 | try: | ||
| 141 | self.initial(request, *args, **kwargs) | ||
| 142 | |||
| 143 | # Get the appropriate handler method | ||
| 144 | if request.method.lower() in self.http_method_names: | ||
| 145 | handler = getattr(self, request.method.lower(), | ||
| 146 | self.http_method_not_allowed) | ||
| 147 | else: | ||
| 148 | handler = self.http_method_not_allowed | ||
| 149 | |||
| 150 | if self.need_print_logger: | ||
| 151 | self.print_logger(request) | ||
| 152 | |||
| 153 | response = handler(request, *args, **kwargs) | ||
| 154 | except Exception as exc: | ||
| 155 | response = self.handle_exception(exc) | ||
| 156 | |||
| 157 | self.response = self.finalize_response( | ||
| 158 | request, response, *args, **kwargs) | ||
| 159 | return self.response | ||
| 160 | |||
| 161 | def get_object(self): | ||
| 162 | return None | ||
| 107 | 163 | ||
| 108 | class IWABaseView: | 164 | class IWABaseView: |
| 109 | 165 | ... | ... |
-
Please register or sign in to post a comment