Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
H
hh_ccs
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
贺阳
hh_ccs
Commits
d7256abe
提交
d7256abe
authored
12月 09, 2024
作者:
刘擎阳
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1.增加附件识别
上级
51805629
全部展开
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
104 行增加
和
2 行删除
+104
-2
common_common.py
ccs_base/models/common_common.py
+0
-0
fetch_mail.py
ccs_base/models/fetch_mail.py
+19
-1
order_state_change_rule.py
ccs_base/models/order_state_change_rule.py
+85
-1
没有找到文件。
ccs_base/models/common_common.py
浏览文件 @
d7256abe
差异被折叠。
点击展开。
ccs_base/models/fetch_mail.py
浏览文件 @
d7256abe
...
...
@@ -8,6 +8,7 @@ from ssl import SSLError
from
socket
import
gaierror
,
timeout
from
imaplib
import
IMAP4
,
IMAP4_SSL
from
poplib
import
POP3
,
POP3_SSL
from
email.header
import
decode_header
import
email
from
odoo
import
api
,
fields
,
models
,
tools
,
_
from
odoo.exceptions
import
UserError
...
...
@@ -122,6 +123,20 @@ class FetchmailServer(models.Model):
if
a_time
>
before_now
:
hdr
,
sender_email
=
parseaddr
(
msg
.
get
(
'From'
))
# 获取发件人邮箱
email_body
=
''
attachment_arr
=
[]
if
msg
.
is_multipart
():
for
part
in
msg
.
walk
():
# 检查邮件每个部分的内容类型
content_disposition
=
str
(
part
.
get
(
"Content-Disposition"
))
if
"attachment"
in
content_disposition
:
# 获取附件的文件名
filename
=
part
.
get_filename
()
if
filename
:
# 解码文件名
filename
,
encoding
=
decode_header
(
filename
)[
0
]
if
isinstance
(
filename
,
bytes
):
filename
=
filename
.
decode
(
encoding
if
encoding
else
"utf-8"
)
attachment_arr
.
append
(
part
.
get_payload
(
decode
=
True
))
for
part
in
msg
.
walk
():
# 如果ture的话内容是没用的
if
not
part
.
is_multipart
():
...
...
@@ -129,8 +144,11 @@ class FetchmailServer(models.Model):
# email_body = part.get_payload(decode=True).decode('utf-8', 'ignore')
email_body
=
part
.
get_payload
(
decode
=
True
)
.
decode
(
'gbk'
,
'ignore'
)
break
if
email_body
:
if
'CDS REPORT'
in
subject
.
upper
():
if
attachment_arr
:
rule_obj
=
self
.
env
[
'order.state.change.rule'
]
.
sudo
()
rule_obj
.
fetch_mail_dlv_attachment
(
attachment_arr
=
attachment_arr
)
elif
email_body
:
rule_obj
=
self
.
env
[
'order.state.change.rule'
]
.
sudo
()
rule_obj
.
fetch_mail_dlv
(
email_body
=
email_body
,
year
=
year
)
imap_server
.
store
(
num
,
'-FLAGS'
,
'
\\
Seen'
)
...
...
ccs_base/models/order_state_change_rule.py
浏览文件 @
d7256abe
...
...
@@ -6,8 +6,11 @@ from odoo import models, fields, api, _
from
odoo.exceptions
import
ValidationError
,
Warning
import
logging
import
json
import
re
import
xlrd
import
base64
import
pdfplumber
import
demjson
from
io
import
BytesIO
from
datetime
import
datetime
,
timedelta
import
re
_logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -42,6 +45,51 @@ class OrderStateChangeRule(models.Model):
# regular_expression = fields.Char('正则表达式')
# active = fields.Boolean('有效性', default=True)
def
get_file_path
(
self
,
report_file_ids
):
"""
得到excel表格的内容
:return:
"""
report_path
=
report_file_ids
.
_full_path
(
report_file_ids
.
store_fname
)
try
:
if
report_file_ids
.
name
[
-
3
:]
==
'xls'
:
data
=
xlrd
.
open_workbook
(
report_path
,
formatting_info
=
True
)
elif
report_file_ids
.
name
[
-
3
:]
in
[
'pdf'
,
'PDF'
]:
data
=
pdfplumber
.
open
(
report_path
)
else
:
data
=
xlrd
.
open_workbook
(
report_path
)
except
Exception
as
e
:
logging
.
info
(
'解析错误
%
s'
%
str
(
e
))
f
=
open
(
report_path
,
'rb'
)
data
=
f
.
readlines
()
return
data
def
read_pdf
(
self
,
pdf_data
):
"""
:param pdf: pdf数据
:return:
"""
order_no
=
''
date_arr
=
[]
pdf_data
=
BytesIO
(
pdf_data
)
pdf
=
pdfplumber
.
open
(
pdf_data
)
for
page
in
pdf
.
pages
:
# 获取当前页面的全部文本信息,包括表格中的文字
text_p
=
page
.
extract_text
()
text_lines
=
text_p
.
split
(
'
\n
'
)
ze_2
=
re
.
compile
(
"
\\
d{3}-
\\
d{8,}"
)
pattern
=
r'\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}'
# date_obj = datetime.strptime(date_str, '%d/%m/%Y %H:%M:%S')
if
text_lines
:
for
line
in
text_lines
:
if
ze_2
.
findall
(
line
):
order_no
=
ze_2
.
findall
(
line
)[
0
]
matches
=
re
.
findall
(
pattern
,
line
)
if
matches
:
date_arr
=
matches
[
0
]
pdf
.
close
()
return
order_no
,
date_arr
# 函数处理输出
def
print_match
(
self
,
text
):
# 正则表达式
...
...
@@ -58,6 +106,42 @@ class OrderStateChangeRule(models.Model):
data_arr
=
data_re
.
findall
(
email_body
)
return
data_arr
def
upload_cds_attachment
(
self
,
bl_obj
,
data
):
file_obj
=
self
.
env
[
'cc.clearance.file'
]
.
sudo
()
.
search
([(
'file_name'
,
'='
,
'海关CDS申报单(import和授权方式检查拉齐等)'
),
(
'bl_id'
,
'='
,
bl_obj
.
id
)],
limit
=
1
)
file_obj
.
file
=
base64
.
encodebytes
(
data
)
file_obj
.
is_upload
=
False
file_obj
.
action_sync
()
def
fetch_mail_dlv_attachment
(
self
,
**
kwargs
):
attachment_arr
=
kwargs
[
'attachment_arr'
]
for
attachment
in
attachment_arr
:
try
:
order_no
,
date_str
=
self
.
read_pdf
(
attachment
)
if
order_no
:
# 转换为 datetime 对象
local_time
=
datetime
.
strptime
(
date_str
,
'
%
d/
%
m/
%
Y
%
H:
%
M:
%
S'
)
# 调整时区
users_obj
=
self
.
env
[
'res.users'
]
.
search
([(
'name'
,
'='
,
'邮件接收'
)],
limit
=
1
)
tz
=
users_obj
.
tz
or
'UTC'
timezone_offset
=
self
.
env
[
'common.common'
]
.
sudo
()
.
init_timezone_data
(
tz
)
utc_time
=
local_time
-
timedelta
(
hours
=
int
(
timezone_offset
))
sql
=
"select id from cc_bl where UPPER(REPLACE(REPLACE(REPLACE(bl_no, ' ', ''), '-', ''), '/', '')) = '{0}' order by create_date desc limit 1"
.
format
(
order_no
.
replace
(
' '
,
''
)
.
replace
(
'-'
,
''
)
.
replace
(
'/'
,
''
))
self
.
_cr
.
execute
(
sql
)
result
=
self
.
_cr
.
fetchall
()
bl_obj
=
self
.
env
[
'cc.bl'
]
.
sudo
()
.
search
([(
'id'
,
'='
,
result
[
0
][
0
])])
if
result
else
False
logging
.
info
(
'order_no:
%
s, bl_obj:
%
s'
%
(
order_no
,
bl_obj
))
if
bl_obj
:
self
.
upload_cds_attachment
(
bl_obj
,
attachment
)
if
bl_obj
and
bl_obj
.
state
!=
'done'
:
redis_conn
=
self
.
env
[
'common.common'
]
.
sudo
()
.
get_redis
()
if
redis_conn
==
'no'
:
raise
ValidationError
(
'未连接redis'
)
else
:
redis_conn
.
lpush
(
'mail_push_package_list'
,
json
.
dumps
({
'id'
:
bl_obj
.
id
,
'utc_time'
:
utc_time
.
strftime
(
"
%
Y-
%
m-
%
d
%
H:
%
M:
%
S"
)}))
except
Exception
as
err
:
logging
.
error
(
'fetch_mail_dlv_attachment--error:
%
s'
%
str
(
err
))
def
fetch_mail_dlv
(
self
,
**
kwargs
):
email_body
=
kwargs
[
'email_body'
]
year
=
kwargs
[
'year'
]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论