d4e9acd6 by 周伟奇

add email sender

1 parent 918035ec
...@@ -6,7 +6,7 @@ import base64 ...@@ -6,7 +6,7 @@ import base64
6 import asyncio 6 import asyncio
7 import aiohttp 7 import aiohttp
8 # from openpyxl import Workbook 8 # from openpyxl import Workbook
9 from apps.doc.ocr.wb import BSWorkbook 9 from apps.doc.ocr.wb import BSWorkbook, Workbook
10 from django.core.management import BaseCommand 10 from django.core.management import BaseCommand
11 11
12 from settings import conf 12 from settings import conf
...@@ -143,6 +143,7 @@ class Command(BaseCommand, LoggerMixin): ...@@ -143,6 +143,7 @@ class Command(BaseCommand, LoggerMixin):
143 continue 143 continue
144 sleep_second = int(conf.SLEEP_SECOND) 144 sleep_second = int(conf.SLEEP_SECOND)
145 # 3.PDF文件提取图片 145 # 3.PDF文件提取图片
146 start_time = time.time()
146 img_save_path = os.path.join(doc_data_path, 'img') 147 img_save_path = os.path.join(doc_data_path, 'img')
147 self.cronjob_log.info('{0} [pdf to img start] [business_type={1}] [doc_id={2}]'.format( 148 self.cronjob_log.info('{0} [pdf to img start] [business_type={1}] [doc_id={2}]'.format(
148 self.log_base, business_type, doc.id)) 149 self.log_base, business_type, doc.id))
...@@ -156,12 +157,13 @@ class Command(BaseCommand, LoggerMixin): ...@@ -156,12 +157,13 @@ class Command(BaseCommand, LoggerMixin):
156 role_summary = { 157 role_summary = {
157 '银行-户名': [] 158 '银行-户名': []
158 } 159 }
159 interest_keyword = Keywords.objects.filter( 160 # interest_keyword = Keywords.objects.filter(
160 type=KeywordsType.INTEREST.value).values_list('keyword', flat=True) 161 # type=KeywordsType.INTEREST.value).values_list('keyword', flat=True)
161 salary_keyword = Keywords.objects.filter( 162 # salary_keyword = Keywords.objects.filter(
162 type=KeywordsType.SALARY.value).values_list('keyword', flat=True) 163 # type=KeywordsType.SALARY.value).values_list('keyword', flat=True)
163 loan_keyword = Keywords.objects.filter(type=KeywordsType.LOAN.value).values_list('keyword', flat=True) 164 # loan_keyword = Keywords.objects.filter(type=KeywordsType.LOAN.value).values_list('keyword', flat=True)
164 wb = BSWorkbook(interest_keyword, salary_keyword, loan_keyword) 165 # wb = BSWorkbook(interest_keyword, salary_keyword, loan_keyword)
166 wb = Workbook()
165 loop = asyncio.get_event_loop() 167 loop = asyncio.get_event_loop()
166 tasks = [self.img_ocr_excel(wb, img_path, role_summary) for img_path in pdf_handler.img_path_list] 168 tasks = [self.img_ocr_excel(wb, img_path, role_summary) for img_path in pdf_handler.img_path_list]
167 loop.run_until_complete(asyncio.wait(tasks)) 169 loop.run_until_complete(asyncio.wait(tasks))
...@@ -189,7 +191,9 @@ class Command(BaseCommand, LoggerMixin): ...@@ -189,7 +191,9 @@ class Command(BaseCommand, LoggerMixin):
189 else: 191 else:
190 doc.status = DocStatus.COMPLETE.value 192 doc.status = DocStatus.COMPLETE.value
191 doc.save() 193 doc.save()
192 self.cronjob_log.info('{0} [doc process complete] [business_type={1}] [doc_id={2}]'.format( 194 end_time = time.time()
193 self.log_base, business_type, doc.id)) 195 speed_time = int(end_time - start_time)
196 self.cronjob_log.info('{0} [doc process complete] [business_type={1}] [doc_id={2}] '
197 '[speed_time={3}]'.format(self.log_base, business_type, doc.id, speed_time))
194 198
195 self.cronjob_log.info('{0} [stop safely]'.format(self.log_base)) 199 self.cronjob_log.info('{0} [stop safely]'.format(self.log_base))
......
...@@ -253,7 +253,7 @@ class DocView(GenericView, DocHandler): ...@@ -253,7 +253,7 @@ class DocView(GenericView, DocHandler):
253 query = application_id_query & status_query & data_source_query & upload_finish_time_query & create_time_query 253 query = application_id_query & status_query & data_source_query & upload_finish_time_query & create_time_query
254 val_tuple = ('id', 'application_id', 'upload_finish_time', 'create_time', 'data_source', 'status') 254 val_tuple = ('id', 'application_id', 'upload_finish_time', 'create_time', 'data_source', 'status')
255 doc_class, prefix = self.get_doc_class(business_type) 255 doc_class, prefix = self.get_doc_class(business_type)
256 doc_queryset = doc_class.objects.filter(query).values(*val_tuple).order_by('-upload_finish_time') 256 doc_queryset = doc_class.objects.filter(query).values(*val_tuple).order_by('-create_time')
257 doc_list = self.get_doc_list(doc_queryset, prefix) 257 doc_list = self.get_doc_list(doc_queryset, prefix)
258 258
259 total = len(doc_list) 259 total = len(doc_list)
......
1 import os
2 import smtplib
3 from email import encoders
4 from email.header import Header
5 from email.mime.base import MIMEBase
6 from email.mime.multipart import MIMEMultipart
7 from email.mime.text import MIMEText
8
9 MAIL_SERVER_HOST = 'smtp.exmail.qq.com'
10 MAIL_SERVER_PORT = 25
11
12 TIME_OUT = 50
13
14
15 class MailSender:
16
17 def __init__(self, sender, pwd):
18 self.sender = sender
19 self.pwd = pwd
20 self.server = smtplib.SMTP(timeout=TIME_OUT)
21 self.server.debuglevel = 0
22 self.server.connect(host=MAIL_SERVER_HOST,
23 port=MAIL_SERVER_PORT,)
24 self.server.login(self.sender, self.pwd)
25
26 def close(self):
27 self.server.close()
28
29 def send(self, to_addrs, subject, content, file_list=[]):
30 msg = MIMEMultipart()
31
32 for att_file in file_list:
33 att = MIMEBase('application', 'octet-stream')
34 att.set_payload(open(att_file, 'rb').read())
35 encoders.encode_base64(att)
36 att.add_header('Content-Disposition',
37 'attachment',
38 filename=Header(os.path.basename(att_file), 'utf-8').encode())
39 msg.attach(att)
40
41 msg['Subject'] = Header(subject, 'utf-8')
42 msg['From'] = self.sender
43 msg['To'] = ','.join(to_addrs)
44
45 content = u'Hi:<br><br>' + \
46 content + \
47 u'<br><br>祝好!<br><br><br>本邮件为系统自动发送,请勿直接回复!<hr>'
48
49 msg.attach(MIMEText(content.encode('utf-8'), 'html', 'utf-8'))
50 self.server.sendmail(self.sender, to_addrs, msg.as_string())
51 # smtplib.SMTPServerDisconnected
52
53
54 # if __name__ == '__main__':
55 # mail_sender = MailSender()
56 # mail_sender.send(['1304057458@qq.com'], 'hello', 'world.', [])
57 # mail_sender.close()
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!