-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtasks.py
More file actions
231 lines (183 loc) · 7.15 KB
/
tasks.py
File metadata and controls
231 lines (183 loc) · 7.15 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
231
import logging
from datetime import timedelta
from urllib.parse import urljoin
from django.conf import settings
from django.utils import timezone
from conferences.models.conference_voucher import ConferenceVoucher
from conferences.tasks import send_conference_voucher_email
from conferences.vouchers import create_conference_voucher
from grants.models import Grant
from integrations import slack
from notifications.models import EmailTemplate, EmailTemplateIdentifier
from pycon.celery import app
from users.models import User
logger = logging.getLogger(__name__)
def get_name(user: User | None, fallback: str = "<no name specified>") -> str:
if not user:
return fallback
return user.full_name or user.name or user.username or fallback
@app.task
def send_grant_reply_approved_email(*, grant_id: int, is_reminder: bool) -> None:
logger.info("Sending Reply APPROVED email for Grant %s", grant_id)
grant = Grant.objects.get(id=grant_id)
total_amount = grant.total_grantee_reimbursement_amount
ticket_only = grant.has_ticket_only()
if total_amount == 0 and not ticket_only:
raise ValueError(
f"Grant {grant_id} has no reimbursement amount and is not ticket-only. "
"This indicates missing or zero-amount reimbursements."
)
reply_url = urljoin(settings.FRONTEND_URL, "/grants/reply/")
variables = {
"reply_url": reply_url,
"start_date": f"{grant.conference.start:%-d %B}",
"end_date": f"{grant.conference.end + timedelta(days=1):%-d %B}",
"deadline_date_time": f"{grant.applicant_reply_deadline:%-d %B %Y %H:%M %Z}",
"deadline_date": f"{grant.applicant_reply_deadline:%-d %B %Y}",
"visa_page_link": urljoin(settings.FRONTEND_URL, "/visa"),
"total_amount": f"{total_amount:.0f}" if total_amount > 0 else None,
"ticket_only": ticket_only,
"is_reminder": is_reminder,
}
_new_send_grant_email(
template_identifier=EmailTemplateIdentifier.grant_approved,
grant=grant,
placeholders=variables,
)
grant.status = Grant.Status.waiting_for_confirmation
grant.save()
logger.info("Email sent for Grant %s", grant.id)
@app.task
def send_grant_reply_waiting_list_email(*, grant_id):
logger.info("Sending Reply WAITING LIST email for Grant %s", grant_id)
_send_grant_waiting_list_email(
grant_id, template_identifier=EmailTemplateIdentifier.grant_waiting_list
)
@app.task
def send_grant_reply_waiting_list_update_email(*, grant_id):
logger.info("Sending Reply WAITING LIST UPDATE email for Grant %s", grant_id)
_send_grant_waiting_list_email(
grant_id, template_identifier=EmailTemplateIdentifier.grant_waiting_list_update
)
@app.task
def send_grant_reply_rejected_email(grant_id):
logger.info("Sending Reply REJECTED email for Grant %s", grant_id)
grant = Grant.objects.get(id=grant_id)
_new_send_grant_email(
template_identifier=EmailTemplateIdentifier.grant_rejected,
grant=grant,
placeholders={},
)
logger.info("Rejection email sent for Grant %s", grant.id)
@app.task
def notify_new_grant_reply_slack(*, grant_id, admin_url):
grant = Grant.objects.get(id=grant_id)
actions = []
if grant.status in (Grant.Status.confirmed, Grant.Status.refused):
actions.append(f"{Grant.Status(grant.status).label} the grant")
conference = grant.conference
slack.send_message(
[
{
"type": "section",
"text": {
"text": f"{grant.full_name} {' and '.join(actions)}",
"type": "mrkdwn",
},
}
],
[
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*<{admin_url}|Open admin>*",
},
},
],
},
],
oauth_token=conference.get_slack_oauth_token(),
channel_id=conference.slack_new_grant_reply_channel_id,
)
def _send_grant_waiting_list_email(grant_id, template_identifier):
grant = Grant.objects.get(id=grant_id)
reply_url = urljoin(settings.FRONTEND_URL, "/grants/reply/")
deadline = grant.conference.deadlines.filter(
type="grants_waiting_list_update"
).first()
if not deadline:
logger.error(
f"No grants_waiting_list_update deadline found for conference {grant.conference}"
)
raise ValueError(
f"Conference {grant.conference.code} missing grants_waiting_list_update deadline"
)
_new_send_grant_email(
template_identifier=template_identifier,
grant=grant,
placeholders={
"reply_url": reply_url,
"grants_update_deadline": f"{deadline.start:%-d %B %Y}",
},
)
logger.info("Email sent for Grant %s", grant.id)
def _new_send_grant_email(
template_identifier: EmailTemplateIdentifier, grant: Grant, placeholders
):
conference = grant.conference
user = grant.user
conference_name = grant.conference.name.localize("en")
email_template = EmailTemplate.objects.for_conference(conference).get_by_identifier(
template_identifier
)
email_template.send_email(
recipient=user,
placeholders={
"conference_name": conference_name,
"user_name": get_name(user, "there"),
**placeholders,
},
)
grant.applicant_reply_sent_at = timezone.now()
grant.save()
@app.task
def create_and_send_grant_voucher(grant_id: int):
"""
Creates a voucher for a confirmed grant and sends an email to the grantee.
This is triggered when a grant is confirmed by the user.
"""
grant = Grant.objects.get(id=grant_id)
conference = grant.conference
user = grant.user
# Check if user already has a voucher for this conference
existing_voucher = (
ConferenceVoucher.objects.for_conference(conference).for_user(user).first()
)
if existing_voucher:
# If user has a co-speaker voucher, upgrade it to a grant voucher
if existing_voucher.voucher_type == ConferenceVoucher.VoucherType.CO_SPEAKER:
logger.info(
"User %s already has a co-speaker voucher for conference %s, "
"upgrading to grant voucher",
user.id,
conference.id,
)
existing_voucher.voucher_type = ConferenceVoucher.VoucherType.GRANT
existing_voucher.save(update_fields=["voucher_type"])
send_conference_voucher_email.delay(conference_voucher_id=existing_voucher.id)
else:
logger.info(
"User %s already has a voucher for conference %s, not creating a new one",
user.id,
conference.id,
)
return
conference_voucher = create_conference_voucher(
conference=conference,
user=user,
voucher_type=ConferenceVoucher.VoucherType.GRANT,
)
send_conference_voucher_email.delay(conference_voucher_id=conference_voucher.id)