|
1 | | -import os |
2 | | -import csv |
3 | | -import pyminizip |
4 | | -from datetime import datetime |
5 | | - |
6 | | -from django.conf import settings |
7 | | -from django.core.mail import EmailMessage |
8 | | -from django.utils.crypto import get_random_string |
9 | | - |
10 | 1 | from config import celery_app as app |
11 | 2 | from apps.surveys19.models import FarmerStat, Survey |
12 | | -from apps.surveys19.export import SurveyRelationGeneratorFactory, StatisticsExporter |
13 | | - |
14 | | - |
15 | | -@app.task |
16 | | -def async_export_107(email): |
17 | | - try: |
18 | | - factory = SurveyRelationGeneratorFactory(excludes={'note__icontains': '無效戶'}) |
19 | | - row_generator = factory.export_generator() |
20 | | - |
21 | | - file_name = f"107_Full_Export_{datetime.now().strftime('%Y_%m_%d_%H_%M_%S')}" |
22 | | - csv_path = f'{file_name}.csv' |
23 | | - zip_path = f'{file_name}.zip' |
24 | | - |
25 | | - with open(csv_path, 'w+', encoding="utf-8") as file: |
26 | | - writer = csv.writer(file) |
27 | | - for row in row_generator: |
28 | | - writer.writerow(row) |
29 | | - |
30 | | - password = get_random_string(length=24) |
31 | | - pyminizip.compress(csv_path, "", zip_path, password, 5) |
32 | | - |
33 | | - with open(zip_path, 'rb') as zip_file: |
34 | | - mail = EmailMessage( |
35 | | - '107調查表匯出完成', |
36 | | - f'匯出結果如附件,解壓縮密碼請輸入:{password}', |
37 | | - settings.DEFAULT_FROM_EMAIL, |
38 | | - [email] |
39 | | - ) |
40 | | - mail.attach('107調查表.zip', zip_file.read(), 'application/zip') |
41 | | - mail.send() |
42 | | - except Exception as e: |
43 | | - EmailMessage( |
44 | | - '107調查表匯出失敗', |
45 | | - f"系統發生錯誤,請通知管理員處理。\n{e}", |
46 | | - settings.DEFAULT_FROM_EMAIL, |
47 | | - [email] |
48 | | - ).send() |
49 | | - finally: |
50 | | - try: |
51 | | - os.remove(csv_path) |
52 | | - os.remove(zip_path) |
53 | | - except Exception: |
54 | | - pass |
55 | | - |
56 | | -@app.task |
57 | | -def async_export_107_statistics(email): |
58 | | - try: |
59 | | - exporter = StatisticsExporter() |
60 | | - |
61 | | - file_name = f"107_Statistic_Report_{datetime.now().strftime('%Y_%m_%d_%H_%M_%S')}" |
62 | | - |
63 | | - file_path = f'{file_name}.xlsx' |
64 | | - zip_path = f'{file_name}.zip' |
65 | | - |
66 | | - exporter(file_path) |
67 | | - |
68 | | - password = get_random_string(length=24) |
69 | | - pyminizip.compress(file_path, "", zip_path, password, 5) |
70 | 3 |
|
71 | | - with open(zip_path, 'rb') as zip_file: |
72 | | - mail = EmailMessage( |
73 | | - '107平台統計結果表式匯出完成', |
74 | | - f'匯出結果如附件,解壓縮密碼請輸入:{password}', |
75 | | - settings.DEFAULT_FROM_EMAIL, |
76 | | - [email] |
77 | | - ) |
78 | | - mail.attach('107平台統計結果表式.zip', zip_file.read(), 'application/zip') |
79 | | - mail.send() |
80 | | - except Exception as e: |
81 | | - EmailMessage( |
82 | | - '107平台統計結果表式匯出失敗', |
83 | | - f"系統發生錯誤,請通知管理員處理。\n{e}", |
84 | | - settings.DEFAULT_FROM_EMAIL, |
85 | | - [email] |
86 | | - ).send() |
87 | | - finally: |
88 | | - try: |
89 | | - os.remove(file_path) |
90 | | - os.remove(zip_path) |
91 | | - except Exception: |
92 | | - pass |
93 | 4 |
|
94 | 5 | @app.task |
95 | 6 | def async_update_107_stratify(survey_id): |
|
0 commit comments