Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
周伟奇
/
bmw-ocr
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
6def5d4f
authored
2025-07-09 15:00:18 +0800
by
冯轩
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
invoice api move to python
1 parent
8ddb1d4c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
142 additions
and
1 deletions
src/apps/doc/views.py
src/common/response2.py
src/apps/doc/views.py
View file @
6def5d4
...
...
@@ -17,6 +17,7 @@ from webargs import fields, validate
from
webargs.djangoparser
import
use_args
,
parser
from
settings
import
conf
from
common
import
response
from
common
import
response2
from
common.mixins
import
GenericView
,
DocGenericView
from
common.tools.file_tools
import
file_write
from
common.redis_cache
import
redis_handler
as
rh
...
...
@@ -57,6 +58,7 @@ from .models import (
HILGreenBookHistoryFile
,
AFCGreenBookHistoryFile
)
from
common.exceptions
import
(
NoPermissionException
)
from
.named_enum
import
ErrorType
,
AutoResult
,
WholeResult
,
RPAResult
,
SystemName
,
RequestTeam
from
.mixins
import
DocHandler
,
MPOSHandler
,
PreSEHandler
from
.
import
consts
...
...
@@ -66,6 +68,7 @@ from prese.compare import get_empty_result
from
apps.doc.ocr.ecm
import
ECM
import
time
from
PIL
import
Image
from
django.http
import
HttpResponse
class
CustomDate
(
fields
.
Date
):
...
...
@@ -580,6 +583,11 @@ mpos_args = {
'file_base64_content'
:
fields
.
List
(
fields
.
Str
(),
required
=
True
,
validate
=
validate
.
Length
(
min
=
1
)),
}
invoice_download_args
=
{
'application_entity'
:
fields
.
Int
(
required
=
True
),
'application_ids'
:
fields
.
Str
(
required
=
True
),
}
class
UploadDocView
(
GenericView
,
DocHandler
):
# permission_classes = []
...
...
@@ -1976,4 +1984,87 @@ class DownloadGBHistoryFileView(GenericView):
self
.
running_log
.
info
(
'[DownloadGBHistoryFileView] [args={0}] '
.
format
(
args
))
return
response
.
ok
(
data
=
True
)
except
Exception
as
e
:
return
response
.
ok
(
data
=
False
)
\ No newline at end of file
return
response
.
ok
(
data
=
False
)
class
InvoiceExcelView
(
GenericView
):
#permission_classes = [IsAuthenticated]
#authentication_classes = [OAuth2AuthenticationWithUser]
# 下载发票excel
@use_args
(
invoice_download_args
,
location
=
'data'
)
def
post
(
self
,
request
,
args
):
application_ids
=
args
.
get
(
'application_ids'
)
application_entity
=
args
.
get
(
'application_entity'
)
self
.
running_log
.
info
(
'[InvoiceExcelView] [user_role={0}] '
.
format
(
'111222333'
))
# 角色权限不符,返回异常
token
=
request
.
META
.
get
(
"HTTP_AUTHORIZATION"
)
user_role
=
rh
.
get_token
(
token
[
-
11
:])
self
.
running_log
.
info
(
'[InvoiceExcelView] [user_role={0}] '
.
format
(
user_role
))
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'
):
self
.
running_log
.
info
(
'[InvoiceExcelView no permission] [user_role={0}] [application_entity={1}]'
.
format
(
user_role
,
application_entity
))
raise
NoPermissionException
(
'no permission'
)
url
=
'http://127.0.0.1:8088/napi/invoice/downloadExcelOri'
body
=
{
'applicationIds'
:
application_ids
,
'applicationEntity'
:
application_entity
}
try
:
self
.
running_log
.
info
(
"request java invoice excel api, url:{0}, body:{1}"
.
format
(
url
,
json
.
dumps
(
body
)))
headers
=
{
'Content-Type'
:
'application/json'
}
resp
=
requests
.
post
(
url
,
headers
=
headers
,
json
=
body
)
self
.
running_log
.
info
(
"java invoice excel api finish, applicationIds:{0},{1}"
.
format
(
application_ids
,
resp
.
text
))
res_json
=
json
.
loads
(
resp
.
text
)
file_path
=
res_json
.
get
(
'result'
)
self
.
running_log
.
info
(
"java invoice excel after process, filePath:{0}"
.
format
(
file_path
))
current_time
=
time
.
strftime
(
'
%
Y-
%
m-
%
d_
%
H_
%
M_
%
S'
,
time
.
localtime
())
download_file_name
=
"发票信息提取-"
+
current_time
+
".xlsx"
f
=
open
(
file_path
,
"rb"
)
response
=
HttpResponse
(
content_type
=
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
response
[
'Content-Disposition'
]
=
'attachment; filename="{0}"'
.
format
(
escape_uri_path
(
download_file_name
))
response
[
'Access-Control-Expose-Headers'
]
=
'content-disposition'
response
.
write
(
f
.
read
())
f
.
close
()
return
response
except
Exception
as
e
:
self
.
running_log
.
error
(
"invoice excel request to java error, url:{0}, param:{1}, errorMsg:{2}"
.
format
(
url
,
json
.
dumps
(
body
),
traceback
.
format_exc
()))
class
InvoiceQueryInfoView
(
GenericView
):
#permission_classes = [IsAuthenticated]
#authentication_classes = [OAuth2AuthenticationWithUser]
@use_args
(
invoice_download_args
,
location
=
'data'
)
def
post
(
self
,
request
,
args
):
application_ids
=
args
.
get
(
'application_ids'
)
application_entity
=
args
.
get
(
'application_entity'
)
self
.
running_log
.
info
(
'[InvoiceExcelView] [user_role={0}] '
.
format
(
'111222333'
))
# 角色权限不符,返回异常
token
=
request
.
META
.
get
(
"HTTP_AUTHORIZATION"
)
user_role
=
rh
.
get_token
(
token
[
-
11
:])
self
.
running_log
.
info
(
'[InvoiceQueryInfoView] [user_role={0}] '
.
format
(
user_role
))
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'
):
self
.
running_log
.
info
(
'[InvoiceExcelView no permission] [user_role={0}] [application_entity={1}]'
.
format
(
user_role
,
application_entity
))
raise
NoPermissionException
(
'no permission'
)
url
=
'http://127.0.0.1:8088/napi/invoice/queryInfoOri'
body
=
{
'applicationIds'
:
application_ids
,
'applicationEntity'
:
application_entity
}
try
:
self
.
running_log
.
info
(
"request java invoice info api, url:{0}, body:{1}"
.
format
(
url
,
json
.
dumps
(
body
)))
headers
=
{
'Content-Type'
:
'application/json'
}
resp
=
requests
.
post
(
url
,
headers
=
headers
,
json
=
body
)
self
.
running_log
.
info
(
"java invoice info api finish, applicationIds:{0},{1}"
.
format
(
application_ids
,
resp
.
text
))
res_json
=
json
.
loads
(
resp
.
text
)
java_result
=
res_json
.
get
(
'result'
)
return
response2
.
ok
(
data
=
java_result
)
except
Exception
as
e
:
self
.
running_log
.
error
(
"invoice info request to java error, url:{0}, param:{1}, errorMsg:{2}"
.
format
(
url
,
json
.
dumps
(
body
),
traceback
.
format_exc
()))
\ No newline at end of file
...
...
src/common/response2.py
0 → 100644
View file @
6def5d4
import
enum
from
django.http
import
JsonResponse
,
HttpResponse
from
.named_enum
import
NamedEnum
def
res_content
(
meta_status
,
msg
,
data
=
None
):
res
=
{
'code'
:
meta_status
,
'msg'
:
msg
}
if
data
is
not
None
:
res
[
'result'
]
=
data
return
res
@enum.unique
class
MetaStatus
(
NamedEnum
):
SUCCESS
=
(
0
,
'success'
)
NEED_LOGIN
=
(
1
,
'need login'
)
INVALID_PARAMS
=
(
2
,
'invalid params'
)
INTERNAL_ERROR
=
(
3
,
'internal error'
)
NOT_EXIST
=
(
4
,
'object not exist'
)
ASYNC_WAIT
=
(
5
,
'async wait'
)
NO_PERMISSION
=
(
6
,
'no permission'
)
ILLEGAL_OPERATION
=
(
7
,
'illegal operation'
)
NEED_UPDATE
=
(
8
,
'need update'
)
class
APIResponse
(
JsonResponse
):
def
__init__
(
self
,
meta_status
,
data
=
None
,
msg
=
''
,
json_dumps_params
=
None
,
**
kwargs
):
data
=
res_content
(
meta_status
,
msg
,
data
)
json_dumps_params
=
json_dumps_params
or
{
'ensure_ascii'
:
False
}
kwargs
[
'json_dumps_params'
]
=
json_dumps_params
super
()
.
__init__
(
data
,
**
kwargs
)
def
ok
(
**
kwargs
):
return
APIResponse
(
10000
,
msg
=
MetaStatus
.
SUCCESS
.
verbose_name
,
**
kwargs
)
def
error_msg
(
msg
=
'internal error'
,
**
kwargs
):
return
APIResponse
(
MetaStatus
.
INTERNAL_ERROR
.
value
,
msg
=
msg
,
**
kwargs
)
def
need_update
(
**
kwargs
):
return
APIResponse
(
MetaStatus
.
NEED_UPDATE
.
value
,
msg
=
MetaStatus
.
NEED_UPDATE
.
verbose_name
,
**
kwargs
)
def
excel_response
(
file_name
,
io_content
):
http_response
=
HttpResponse
(
content_type
=
"application/vnd.ms-excel"
)
http_response
[
'Content-Disposition'
]
=
'attachment;filename={0}.xlsx'
.
format
(
file_name
)
http_response
.
write
(
io_content
.
getvalue
())
return
http_response
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment