-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtasks.py
More file actions
176 lines (134 loc) · 5.06 KB
/
tasks.py
File metadata and controls
176 lines (134 loc) · 5.06 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
from datetime import timedelta
from urllib.parse import urljoin
from django.conf import settings
from django.utils import timezone
from notifications.models import EmailTemplate, EmailTemplateIdentifier
from users.models import User
from grants.models import Grant
from integrations import slack
import logging
from pycon.celery import app
logger = logging.getLogger(__name__)
def get_name(user: User | None, fallback: str = "<no name specified>"):
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, is_reminder):
logger.info("Sending Reply APPROVED email for Grant %s", grant_id)
grant = Grant.objects.get(id=grant_id)
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"),
"has_approved_travel": grant.has_approved_travel(),
"has_approved_accommodation": grant.has_approved_accommodation(),
"is_reminder": is_reminder,
}
if grant.has_approved_travel():
if not grant.travel_amount:
raise ValueError(
"Grant travel amount is set to Zero, can't send the email!"
)
variables["travel_amount"] = f"{grant.travel_amount:.0f}"
_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="custom", name__contains={"en": "Update Grants in Waiting List"}
).first()
_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
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()