-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathcron.py
More file actions
230 lines (206 loc) · 11.9 KB
/
cron.py
File metadata and controls
230 lines (206 loc) · 11.9 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from datetime import timedelta
from django.conf import settings
from django.core.mail import get_connection
from django.utils import timezone
import arrow
from django_cron import CronJobBase, Schedule
from coderdojochi.models import MentorOrder, Order, Session
from coderdojochi.util import email
class SendReminders(CronJobBase):
# Run cron every hour during the day so people get emails quickly
# but not in the middle of the night
RUN_AT_TIMES = ['09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00']
schedule = Schedule(run_at_times=RUN_AT_TIMES)
code = 'coderdojochi.send_reminders'
def do(self):
orders_within_a_week = Order.objects.filter(
is_active=True,
week_reminder_sent=False,
session__start_date__lte=(timezone.now() + timedelta(days=7)),
session__start_date__gte=(timezone.now() + timedelta(days=1)),
)
orders_within_a_day = Order.objects.filter(
is_active=True,
day_reminder_sent=False,
session__start_date__lte=(timezone.now() + timedelta(days=1)),
session__start_date__gte=(timezone.now() - timedelta(days=2)),
)
sessions_within_a_week = Session.objects.filter(
is_active=True,
mentors_week_reminder_sent=False,
start_date__lte=(timezone.now() + timedelta(days=7)),
start_date__gte=(timezone.now() + timedelta(days=1)),
)
sessions_within_a_day = Session.objects.filter(
is_active=True,
mentors_day_reminder_sent=False,
start_date__lte=(timezone.now() + timedelta(days=1)),
start_date__gte=(timezone.now() - timedelta(days=2)),
)
# Clear email send data
merge_data = {}
recipients = []
for order in orders_within_a_week:
recipients.append(order.guardian.user.email)
merge_data[order.guardian.user.email] = {
'first_name': order.guardian.user.first_name,
'last_name': order.guardian.user.last_name,
'student_first_name': order.student.first_name,
'student_last_name': order.student.last_name,
'class_code': order.session.course.code,
'class_title': order.session.course.title,
'class_description': order.session.course.description,
'class_start_date': arrow.get(order.session.start_date).to('local').format('dddd, MMMM D, YYYY'),
'class_start_time': arrow.get(order.session.start_date).to('local').format('h:mma'),
'class_end_date': arrow.get(order.session.end_date).to('local').format('dddd, MMMM D, YYYY'),
'class_end_time': arrow.get(order.session.end_date).to('local').format('h:mma'),
'class_location_name': order.session.location.name,
'class_location_address': order.session.location.address,
'class_location_city': order.session.location.city,
'class_location_state': order.session.location.state,
'class_location_zip': order.session.location.zip,
'class_additional_info': order.session.additional_info,
'class_url': f"{settings.SITE_URL}{order.session.get_absolute_url()}",
'class_calendar_url': f"{settings.SITE_URL}{order.session.get_calendar_url()}",
'microdata_start_date': arrow.get(order.session.start_date).to('local').isoformat(),
'microdata_end_date': arrow.get(order.session.end_date).to('local').isoformat(),
'order_id': order.id,
'online_video_link': order.session.online_video_link,
'online_video_description': order.session.online_video_description,
}
email(
subject='Upcoming class reminder',
template_name='class-reminder-guardian-one-week',
merge_data=merge_data,
recipients=recipients,
preheader='Your class is just a few days away!',
unsub_group_id=settings.SENDGRID_UNSUB_CLASSANNOUNCE,
)
for order in orders_within_a_week:
order.week_reminder_sent = True
order.save()
# Clear email send data
merge_data = {}
recipients = []
for order in orders_within_a_day:
recipients.append(order.guardian.user.email)
merge_data[order.guardian.user.email] = {
'first_name': order.guardian.user.first_name,
'last_name': order.guardian.user.last_name,
'student_first_name': order.student.first_name,
'student_last_name': order.student.last_name,
'class_code': order.session.course.code,
'class_title': order.session.course.title,
'class_description': order.session.course.description,
'class_start_date': arrow.get(order.session.start_date).to('local').format('dddd, MMMM D, YYYY'),
'class_start_time': arrow.get(order.session.start_date).to('local').format('h:mma'),
'class_end_date': arrow.get(order.session.end_date).to('local').format('dddd, MMMM D, YYYY'),
'class_end_time': arrow.get(order.session.end_date).to('local').format('h:mma'),
'class_location_name': order.session.location.name,
'class_location_address': order.session.location.address,
'class_location_city': order.session.location.city,
'class_location_state': order.session.location.state,
'class_location_zip': order.session.location.zip,
'class_additional_info': order.session.additional_info,
'class_url': f"{settings.SITE_URL}{order.session.get_absolute_url()}",
'class_calendar_url': f"{settings.SITE_URL}{order.session.get_calendar_url()}",
'microdata_start_date': arrow.get(order.session.start_date).to('local').isoformat(),
'microdata_end_date': arrow.get(order.session.end_date).to('local').isoformat(),
'order_id': order.id,
'online_video_link': order.session.online_video_link,
'online_video_description': order.session.online_video_description,
}
email(
subject='Your class is coming up!',
template_name='class-reminder-guardian-24-hour',
merge_data=merge_data,
recipients=recipients,
preheader='Your class is just hours away!',
unsub_group_id=settings.SENDGRID_UNSUB_CLASSANNOUNCE,
)
for order in orders_within_a_day:
order.day_reminder_sent = True
order.save()
for session in sessions_within_a_week:
orders = MentorOrder.objects.filter(session=session)
# Clear email send data
merge_data = {}
recipients = []
for order in orders:
recipients.append(order.mentor.user.email)
merge_data[order.mentor.user.email] = {
'first_name': order.mentor.user.first_name,
'last_name': order.mentor.user.last_name,
'class_code': order.session.course.code,
'class_title': order.session.course.title,
'class_description': order.session.course.description,
'class_start_date': arrow.get(
order.session.mentor_start_date
).to('local').format('dddd, MMMM D, YYYY'),
'class_start_time': arrow.get(order.session.mentor_start_date).to('local').format('h:mma'),
'class_end_date': arrow.get(order.session.mentor_end_date).to('local').format('dddd, MMMM D, YYYY'),
'class_end_time': arrow.get(order.session.mentor_end_date).to('local').format('h:mma'),
'class_location_name': order.session.location.name,
'class_location_address': order.session.location.address,
'class_location_city': order.session.location.city,
'class_location_state': order.session.location.state,
'class_location_zip': order.session.location.zip,
'class_additional_info': order.session.additional_info,
'class_url': f"{settings.SITE_URL}{order.session.get_absolute_url()}",
'class_calendar_url': f"{settings.SITE_URL}{order.session.get_calendar_url()}",
'microdata_start_date': arrow.get(order.session.start_date).to('local').isoformat(),
'microdata_end_date': arrow.get(order.session.end_date).to('local').isoformat(),
'order_id': order.id,
'online_video_link': order.session.online_video_link,
'online_video_description': order.session.online_video_description,
}
email(
subject='Your We All Code class is in less than a week!',
template_name='class-reminder-mentor-one-week',
merge_data=merge_data,
recipients=recipients,
preheader='The class is just a few days away!',
unsub_group_id=settings.SENDGRID_UNSUB_CLASSANNOUNCE,
)
session.mentors_week_reminder_sent = True
session.save()
for session in sessions_within_a_day:
orders = MentorOrder.objects.filter(session=session)
for order in orders:
recipients.append(order.mentor.user.email)
merge_data[order.mentor.user.email] = {
'first_name': order.mentor.user.first_name,
'last_name': order.mentor.user.last_name,
'class_code': order.session.course.code,
'class_title': order.session.course.title,
'class_description': order.session.course.description,
'class_start_date': arrow.get(
order.session.mentor_start_date
).to('local').format('dddd, MMMM D, YYYY'),
'class_start_time': arrow.get(order.session.mentor_start_date).to('local').format('h:mma'),
'class_end_date': arrow.get(order.session.mentor_end_date).to('local').format('dddd, MMMM D, YYYY'),
'class_end_time': arrow.get(order.session.mentor_end_date).to('local').format('h:mma'),
'class_location_name': order.session.location.name,
'class_location_address': order.session.location.address,
'class_location_city': order.session.location.city,
'class_location_state': order.session.location.state,
'class_location_zip': order.session.location.zip,
'class_additional_info': order.session.additional_info,
'class_url': f"{settings.SITE_URL}{order.session.get_absolute_url()}",
'class_calendar_url': f"{settings.SITE_URL}{order.session.get_calendar_url()}",
'microdata_start_date': arrow.get(order.session.start_date).to('local').isoformat(),
'microdata_end_date': arrow.get(order.session.end_date).to('local').isoformat(),
'order_id': order.id,
'online_video_link': order.session.online_video_link,
'online_video_description': order.session.online_video_description,
}
email(
subject='Your We All Code class is tomorrow!',
template_name='class-reminder-mentor-24-hour',
merge_data=merge_data,
recipients=recipients,
preheader='The class is just a few hours away!',
unsub_group_id=settings.SENDGRID_UNSUB_CLASSANNOUNCE,
)
session.mentors_day_reminder_sent = True
session.save()