-
Notifications
You must be signed in to change notification settings - Fork 16
/
async_workflow_notifications_demo.py
48 lines (35 loc) · 1.76 KB
/
async_workflow_notifications_demo.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
""" Building emails to send asynchronously, via cron job or scheduler """
# THIS IS A DEMO WHICH LIMITS THE NUMBER OF EMAILS SENT, and is rate limited for mailtrap
from portality import app_email
from portality.core import app
from portality.tasks import async_workflow_notifications
import time
# Replace the async workflow send function with this one to limit emails sent in this demo.
def send_emails(emails_dict):
for (email, (to_name, paragraphs)) in emails_dict.items():
time.sleep(0.6)
pre = 'Dear ' + to_name + ',\n\n'
post = '\n\nThe DOAJ Team\n\n***\nThis is an automated message. Please do not reply to this email.'
full_body = pre + '\n\n'.join(paragraphs) + post
app_email.send_mail(to=[email],
fro=app.config.get('SYSTEM_EMAIL_FROM', 'helpdesk@doaj.org'),
subject="DOAJ editorial reminders",
msg_body=full_body)
# Main function for running all notification types in sequence
def run_async_notifications():
""" Run through each notification type, then send emails """
# Create a request context to render templates
ctx = app.test_request_context()
ctx.push()
# Store all of the emails: { email_addr : (name, [paragraphs]) }
emails_dict = {}
# Gather info and build the notifications
async_workflow_notifications.managing_editor_notifications(emails_dict)
async_workflow_notifications.editor_notifications(emails_dict, limit=5)
async_workflow_notifications.associate_editor_notifications(emails_dict, limit=5)
# Discard the context (the send mail function makes its own)
ctx.pop()
send_emails(emails_dict)
# Run all if the script is called.
if __name__ == '__main__':
run_async_notifications()