invoice api move to python
Showing
2 changed files
with
142 additions
and
1 deletions
| ... | @@ -17,6 +17,7 @@ from webargs import fields, validate | ... | @@ -17,6 +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 import response2 | ||
| 20 | from common.mixins import GenericView,DocGenericView | 21 | from common.mixins import GenericView,DocGenericView |
| 21 | from common.tools.file_tools import file_write | 22 | from common.tools.file_tools import file_write |
| 22 | from common.redis_cache import redis_handler as rh | 23 | from common.redis_cache import redis_handler as rh |
| ... | @@ -57,6 +58,7 @@ from .models import ( | ... | @@ -57,6 +58,7 @@ from .models import ( |
| 57 | HILGreenBookHistoryFile, | 58 | HILGreenBookHistoryFile, |
| 58 | AFCGreenBookHistoryFile | 59 | AFCGreenBookHistoryFile |
| 59 | ) | 60 | ) |
| 61 | from common.exceptions import (NoPermissionException) | ||
| 60 | from .named_enum import ErrorType, AutoResult, WholeResult, RPAResult, SystemName, RequestTeam | 62 | from .named_enum import ErrorType, AutoResult, WholeResult, RPAResult, SystemName, RequestTeam |
| 61 | from .mixins import DocHandler, MPOSHandler, PreSEHandler | 63 | from .mixins import DocHandler, MPOSHandler, PreSEHandler |
| 62 | from . import consts | 64 | from . import consts |
| ... | @@ -66,6 +68,7 @@ from prese.compare import get_empty_result | ... | @@ -66,6 +68,7 @@ from prese.compare import get_empty_result |
| 66 | from apps.doc.ocr.ecm import ECM | 68 | from apps.doc.ocr.ecm import ECM |
| 67 | import time | 69 | import time |
| 68 | from PIL import Image | 70 | from PIL import Image |
| 71 | from django.http import HttpResponse | ||
| 69 | 72 | ||
| 70 | 73 | ||
| 71 | class CustomDate(fields.Date): | 74 | class CustomDate(fields.Date): |
| ... | @@ -580,6 +583,11 @@ mpos_args = { | ... | @@ -580,6 +583,11 @@ mpos_args = { |
| 580 | 'file_base64_content': fields.List(fields.Str(), required=True, validate=validate.Length(min=1)), | 583 | 'file_base64_content': fields.List(fields.Str(), required=True, validate=validate.Length(min=1)), |
| 581 | } | 584 | } |
| 582 | 585 | ||
| 586 | invoice_download_args = { | ||
| 587 | 'application_entity': fields.Int(required=True), | ||
| 588 | 'application_ids': fields.Str(required=True), | ||
| 589 | } | ||
| 590 | |||
| 583 | 591 | ||
| 584 | class UploadDocView(GenericView, DocHandler): | 592 | class UploadDocView(GenericView, DocHandler): |
| 585 | # permission_classes = [] | 593 | # permission_classes = [] |
| ... | @@ -1976,4 +1984,87 @@ class DownloadGBHistoryFileView(GenericView): | ... | @@ -1976,4 +1984,87 @@ class DownloadGBHistoryFileView(GenericView): |
| 1976 | self.running_log.info('[DownloadGBHistoryFileView] [args={0}] '.format(args)) | 1984 | self.running_log.info('[DownloadGBHistoryFileView] [args={0}] '.format(args)) |
| 1977 | return response.ok(data=True) | 1985 | return response.ok(data=True) |
| 1978 | except Exception as e: | 1986 | except Exception as e: |
| 1979 | return response.ok(data=False) | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1987 | return response.ok(data=False) | ||
| 1988 | |||
| 1989 | class InvoiceExcelView(GenericView): | ||
| 1990 | #permission_classes = [IsAuthenticated] | ||
| 1991 | #authentication_classes = [OAuth2AuthenticationWithUser] | ||
| 1992 | |||
| 1993 | # 下载发票excel | ||
| 1994 | @use_args(invoice_download_args, location='data') | ||
| 1995 | def post(self, request, args): | ||
| 1996 | application_ids = args.get('application_ids') | ||
| 1997 | application_entity = args.get('application_entity') | ||
| 1998 | self.running_log.info('[InvoiceExcelView] [user_role={0}] '.format('111222333')) | ||
| 1999 | # 角色权限不符,返回异常 | ||
| 2000 | token = request.META.get("HTTP_AUTHORIZATION") | ||
| 2001 | user_role = rh.get_token(token[-11:]) | ||
| 2002 | self.running_log.info('[InvoiceExcelView] [user_role={0}] '.format(user_role)) | ||
| 2003 | if user_role is None or user_role == '-1' or (user_role == '1' and application_entity == '2') or (user_role == '2' and application_entity == '1'): | ||
| 2004 | self.running_log.info('[InvoiceExcelView no permission] [user_role={0}] [application_entity={1}]'.format(user_role, application_entity)) | ||
| 2005 | raise NoPermissionException('no permission') | ||
| 2006 | |||
| 2007 | url = 'http://127.0.0.1:8088/napi/invoice/downloadExcelOri' | ||
| 2008 | body = { | ||
| 2009 | 'applicationIds': application_ids, | ||
| 2010 | 'applicationEntity': application_entity | ||
| 2011 | } | ||
| 2012 | try: | ||
| 2013 | self.running_log.info("request java invoice excel api, url:{0}, body:{1}".format(url, json.dumps(body))) | ||
| 2014 | headers = { | ||
| 2015 | 'Content-Type': 'application/json' | ||
| 2016 | } | ||
| 2017 | resp = requests.post(url, headers=headers, json=body) | ||
| 2018 | self.running_log.info("java invoice excel api finish, applicationIds:{0},{1}".format(application_ids, resp.text)) | ||
| 2019 | res_json = json.loads(resp.text) | ||
| 2020 | file_path = res_json.get('result') | ||
| 2021 | self.running_log.info("java invoice excel after process, filePath:{0}".format(file_path)) | ||
| 2022 | current_time = time.strftime('%Y-%m-%d_%H_%M_%S', time.localtime()) | ||
| 2023 | download_file_name = "发票信息提取-" + current_time + ".xlsx" | ||
| 2024 | f = open(file_path,"rb") | ||
| 2025 | response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') | ||
| 2026 | response['Content-Disposition'] = 'attachment; filename="{0}"'.format(escape_uri_path(download_file_name)) | ||
| 2027 | response['Access-Control-Expose-Headers'] = 'content-disposition' | ||
| 2028 | response.write(f.read()) | ||
| 2029 | f.close() | ||
| 2030 | return response | ||
| 2031 | except Exception as e: | ||
| 2032 | self.running_log.error("invoice excel request to java error, url:{0}, param:{1}, errorMsg:{2}".format( | ||
| 2033 | url, json.dumps(body), traceback.format_exc())) | ||
| 2034 | |||
| 2035 | class InvoiceQueryInfoView(GenericView): | ||
| 2036 | #permission_classes = [IsAuthenticated] | ||
| 2037 | #authentication_classes = [OAuth2AuthenticationWithUser] | ||
| 2038 | |||
| 2039 | @use_args(invoice_download_args, location='data') | ||
| 2040 | def post(self, request, args): | ||
| 2041 | application_ids = args.get('application_ids') | ||
| 2042 | application_entity = args.get('application_entity') | ||
| 2043 | self.running_log.info('[InvoiceExcelView] [user_role={0}] '.format('111222333')) | ||
| 2044 | |||
| 2045 | # 角色权限不符,返回异常 | ||
| 2046 | token = request.META.get("HTTP_AUTHORIZATION") | ||
| 2047 | user_role = rh.get_token(token[-11:]) | ||
| 2048 | self.running_log.info('[InvoiceQueryInfoView] [user_role={0}] '.format(user_role)) | ||
| 2049 | if user_role is None or user_role == '-1' or (user_role == '1' and application_entity == '2') or (user_role == '2' and application_entity == '1'): | ||
| 2050 | self.running_log.info('[InvoiceExcelView no permission] [user_role={0}] [application_entity={1}]'.format(user_role, application_entity)) | ||
| 2051 | raise NoPermissionException('no permission') | ||
| 2052 | |||
| 2053 | url = 'http://127.0.0.1:8088/napi/invoice/queryInfoOri' | ||
| 2054 | body = { | ||
| 2055 | 'applicationIds': application_ids, | ||
| 2056 | 'applicationEntity': application_entity | ||
| 2057 | } | ||
| 2058 | try: | ||
| 2059 | self.running_log.info("request java invoice info api, url:{0}, body:{1}".format(url, json.dumps(body))) | ||
| 2060 | headers = { | ||
| 2061 | 'Content-Type': 'application/json' | ||
| 2062 | } | ||
| 2063 | resp = requests.post(url, headers=headers, json=body) | ||
| 2064 | self.running_log.info("java invoice info api finish, applicationIds:{0},{1}".format(application_ids, resp.text)) | ||
| 2065 | res_json = json.loads(resp.text) | ||
| 2066 | java_result = res_json.get('result') | ||
| 2067 | return response2.ok(data=java_result) | ||
| 2068 | except Exception as e: | ||
| 2069 | self.running_log.error("invoice info request to java error, url:{0}, param:{1}, errorMsg:{2}".format( | ||
| 2070 | url, json.dumps(body), traceback.format_exc())) | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
src/common/response2.py
0 → 100644
| 1 | import enum | ||
| 2 | from django.http import JsonResponse, HttpResponse | ||
| 3 | from .named_enum import NamedEnum | ||
| 4 | |||
| 5 | |||
| 6 | def res_content(meta_status, msg, data=None): | ||
| 7 | res = {'code': meta_status, 'msg': msg} | ||
| 8 | if data is not None: | ||
| 9 | res['result'] = data | ||
| 10 | return res | ||
| 11 | |||
| 12 | |||
| 13 | @enum.unique | ||
| 14 | class MetaStatus(NamedEnum): | ||
| 15 | SUCCESS = (0, 'success') | ||
| 16 | NEED_LOGIN = (1, 'need login') | ||
| 17 | INVALID_PARAMS = (2, 'invalid params') | ||
| 18 | INTERNAL_ERROR = (3, 'internal error') | ||
| 19 | NOT_EXIST = (4, 'object not exist') | ||
| 20 | ASYNC_WAIT = (5, 'async wait') | ||
| 21 | NO_PERMISSION = (6, 'no permission') | ||
| 22 | ILLEGAL_OPERATION = (7, 'illegal operation') | ||
| 23 | NEED_UPDATE = (8, 'need update') | ||
| 24 | |||
| 25 | |||
| 26 | class APIResponse(JsonResponse): | ||
| 27 | def __init__(self, meta_status, data=None, msg='', json_dumps_params=None, **kwargs): | ||
| 28 | data = res_content(meta_status, msg, data) | ||
| 29 | json_dumps_params = json_dumps_params or {'ensure_ascii': False} | ||
| 30 | kwargs['json_dumps_params'] = json_dumps_params | ||
| 31 | super().__init__(data, **kwargs) | ||
| 32 | |||
| 33 | |||
| 34 | def ok(**kwargs): | ||
| 35 | return APIResponse(10000, msg=MetaStatus.SUCCESS.verbose_name, **kwargs) | ||
| 36 | |||
| 37 | |||
| 38 | def error_msg(msg='internal error', **kwargs): | ||
| 39 | return APIResponse(MetaStatus.INTERNAL_ERROR.value, msg=msg, **kwargs) | ||
| 40 | |||
| 41 | |||
| 42 | def need_update(**kwargs): | ||
| 43 | return APIResponse(MetaStatus.NEED_UPDATE.value, msg=MetaStatus.NEED_UPDATE.verbose_name, **kwargs) | ||
| 44 | |||
| 45 | |||
| 46 | def excel_response(file_name, io_content): | ||
| 47 | http_response = HttpResponse(content_type="application/vnd.ms-excel") | ||
| 48 | http_response['Content-Disposition'] = 'attachment;filename={0}.xlsx'.format(file_name) | ||
| 49 | http_response.write(io_content.getvalue()) | ||
| 50 | return http_response |
-
Please register or sign in to post a comment