-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.py
107 lines (84 loc) · 2.56 KB
/
cron.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from datetime import datetime
import sys
from dotenv import load_dotenv
import pytz
import sqlalchemy as sa
from emails import *
from models import *
from utils import *
load_dotenv()
EMAIL_PW = os.environ["EMAIL_PW"]
BATCH_SIZE = 50
"""
These two methods are designed to be run once a day at 9am.
"""
def send_overdue_email(test_email=None):
header = f"Subject: {OVERDUE_SUBJECT}\n\n"
message = header + OVERDUE_MESSAGE
to = []
if test_email:
print(f"Sending test overdue email to {test_email}")
send_email(
[test_email],
message,
EMAIL_PW,
)
return
config = Config.query.first()
due_date = timezone("US/Eastern").localize(config.due_date)
diff = datetime.now(tz=pytz.timezone("US/Eastern")) - due_date
if diff.days != 0:
print("Skipped sending overdue emails")
return
print("Sending overdue emails")
users = db.query(User).filter(User.entry == sa.null())
for user in users:
to.append(user.email)
for i in range(0, len(to), BATCH_SIZE):
batch_to = to[i : i + BATCH_SIZE]
print(f"Emailing {batch_to}")
send_email(
batch_to,
message,
EMAIL_PW,
)
log_email(users, "overdue")
print("Sent overdue emails")
def send_reminder_email(test_email=None):
header = f"Subject: {REMINDER_SUBJECT}\n\n"
message = header + REMINDER_MESSAGE
to = []
if test_email:
print(f"Sending test reminder email to {test_email}")
send_email(
[test_email],
message,
EMAIL_PW,
)
return
config = Config.query.first()
due_date = timezone("US/Eastern").localize(config.due_date)
diff = due_date - datetime.now(tz=pytz.timezone("US/Eastern"))
if diff.days != 1:
print("Skipped sending reminder emails")
return
print("Sending reminder emails")
users = User.query.all()
for user in users:
to.append(user.email)
for i in range(0, len(to), BATCH_SIZE):
batch_to = to[i : i + BATCH_SIZE]
print(f"Emailing {batch_to}")
send_email(
batch_to,
message,
EMAIL_PW,
)
log_email(users, "reminder")
print("Send reminder emails")
if __name__ == "__main__":
# Add a test email address after python cron.py
# $ python cron.py [email protected]
test_email_address = sys.argv[1] if len(sys.argv) == 2 else None
send_reminder_email(test_email_address)
send_overdue_email(test_email_address)