-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.py
191 lines (160 loc) · 6.61 KB
/
background.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# coding=utf-8
# Copyright: Taavi Eomäe 2017-2020
# SPDX-License-Identifier: AGPL-3.0-only
# Background tasks
from datetime import timedelta
from flask_mail import Message
from main import app, celery, mail, security
from utility import *
from utility_standalone import *
getLogger().setLevel(Config.LOGLEVEL)
logger = getLogger()
@celery.task()
def send_security_email(message):
"""
Sends asynchronous email
"""
with app.app_context():
try:
msg = Message(message["subject"],
message["recipients"])
msg.body = message["body"]
msg.html = message["html"]
msg.sender = message["sender"]
mail.send(msg)
except Exception:
sentry_sdk.capture_exception()
# Override security email sender
@security.send_mail_task
def delay_security_email(msg):
"""
Allows sending e-mail non-blockingly
"""
try:
send_security_email.delay(
{"subject": msg.subject,
"recipients": msg.recipients,
"body": msg.body,
"html": msg.html,
"sender": msg.sender}
)
except Exception:
sentry_sdk.capture_exception()
def remind_to_add(rate_limit=True):
logger.info("Started sending adding reminders")
now = datetime.now()
try:
with open("remind_to_add", "r+") as timer_file:
lastexec = timer_file.read()
lastexec = datetime(*map(int, reversed(lastexec.split("/"))))
if now - lastexec < timedelta(days=30):
logger.info(" Adding reminders were rate-limited")
if rate_limit:
return
else:
timer_file.seek(0)
timer_file.write(get_timestamp())
except Exception:
logger.info(" Adding reminders rate-limit file was not found")
with open("remind_to_add", "w") as timer_file:
timer_file.write(get_timestamp())
for user in User.query:
if user.last_activity_at:
if now - datetime(*map(int, user.last_activity_at.split("/"))) < timedelta(days=15):
continue
email_to_send = "Tere,\n"
email_to_send += "Tegemist on väikese meeldetuletusega enda nimekirja koostamisele hakata mõtlema\n"
email_to_send += "\n"
email_to_send += "Kirja saavad kõik kasutajad kes ei ole vähemalt 15 päeva sisse loginud\n"
email_to_send += "Jõulurakendus 🎄"
mail.send_message(subject="Meeldetuletus kinkide kohta",
body=email_to_send,
recipients=[user.email])
logger.info("Finished sending adding reminders")
def remind_to_buy(rate_limit=True):
logger.info("Started sending purchase reminders")
now = datetime.now()
try:
with open("remind_to_buy", "r+") as timer_file:
lastexec = timer_file.read()
lastexec = datetime(*map(int, reversed(lastexec.split("/"))))
if now - lastexec < timedelta(days=15):
logger.info("Buying reminders were rate-limited")
if rate_limit:
return
else:
timer_file.seek(0)
timer_file.write(get_timestamp())
except Exception:
logger.info(" Reminder to buy timer file was not found")
with open("remind_to_buy", "w") as timer_file:
timer_file.write(get_timestamp())
for user in User.query:
marked_entries = get_person_marked(user.id)
items_to_purchase = []
for entry in marked_entries:
if entry.status == "booked" or \
entry.status == "modified":
items_to_purchase.append((entry.item, get_person_name(entry.user_id)))
if len(items_to_purchase) == 0:
continue
email_to_send = "Tere,\n"
email_to_send += "Olete märkinud, et plaanite osta allpool loetletud kingitused kuid ei ole vastavate kingituste staatust uuendanud vähemalt viisteist päeva eelnevast meeldetuletusest:\n"
email_to_send += "\n"
email_to_send += "Kingitus | Kellele\n"
for item in items_to_purchase:
email_to_send += "\""
email_to_send += item[0]
email_to_send += "\" - "
email_to_send += item[1]
email_to_send += "\n"
email_to_send += "\n"
email_to_send += "Palun mitte unustada\n"
email_to_send += "Jõulurakendus🎄"
mail.send_message(subject="Meeldetuletus kinkide kohta",
body=email_to_send,
recipients=[user.email])
logger.info("Finished sending purchase reminders")
def remind_about_change(rate_limit=True):
logger.info(" Started sending change reminders")
now = datetime.now()
try:
with open("remind_about_change", "r+") as timer_file:
lastexec = timer_file.read()
lastexec = datetime(*map(int, reversed(lastexec.split("/"))))
if now - lastexec < timedelta(hours=6):
logger.info(" Changing reminders were rate-limited")
if rate_limit:
return
else:
timer_file.seek(0)
timer_file.write(get_timestamp())
except Exception:
logger.info(" Change reminder timer file was not found")
with open("remind_about_change", "w") as timer_file:
timer_file.write(get_timestamp())
for user in User.query:
marked_entries = get_person_marked(user.id)
items_to_purchase = []
for entry in marked_entries:
if entry.status == "modified":
items_to_purchase.append((entry.item, get_person_name(entry.user_id)))
if len(items_to_purchase) == 0:
continue
email_to_send = "Tere,\n"
email_to_send += "Viimase päeva jooksul on muudetud allpool loetletud soove, on oluline, et otsustaksite kas soovite ikka kinki osta või vabastate selle teistele:\n"
email_to_send += "\n"
email_to_send += "Kingitus | Kellele\n"
for item in items_to_purchase:
email_to_send += "\""
email_to_send += item[0]
email_to_send += "\" - "
email_to_send += item[1]
email_to_send += "\n"
email_to_send += "\n"
email_to_send += "Palume päeva jooksul enda otsus uuesti süsteemi sisestada\n"
email_to_send += "Jõulurakendus 🎄"
mail.send_message(subject="Meeldetuletus kinkide kohta",
body=email_to_send,
recipients=[user.email])
logger.info("Finished sending change reminders")