Skip to content

Commit 84fd34a

Browse files
goyal1092milafrerichs
authored andcommitted
Added a way to send email notifications for upload task via api
1 parent f18e0bb commit 84fd34a

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

wazimap_ng/datasets/hooks.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import logging
22

33
from django.contrib.sessions.models import Session
4+
from django.contrib.auth.models import User
45
from django.urls import reverse
56

67
from django_q.tasks import async_task
78
from .models import Indicator
89

10+
from django.core import mail
11+
from django.template.loader import render_to_string
12+
from django.utils.html import strip_tags
13+
914
import json
1015

1116
logger = logging.getLogger(__name__)
@@ -84,6 +89,8 @@ def process_task_info(task):
8489
assign_task = task.kwargs.get("assign", False)
8590
update_indicators = task.kwargs.get("update_indicators", False)
8691
session_key = task.kwargs.get("key", False)
92+
email_notification = task.kwargs.get("email", False)
93+
user_id = task.kwargs.get("user_id", False)
8794
results = task.result or {}
8895
obj = next(iter(task.args))
8996

@@ -118,6 +125,27 @@ def process_task_info(task):
118125
type="data_extraction", assign=False, notify=False
119126
)
120127

128+
if email_notification and user_id:
129+
user = User.objects.filter(id=user_id).first()
130+
131+
if user and user.email:
132+
dataset = task.args[1]
133+
complete_type = "Success" if task.success else "Failure"
134+
subject = F"{complete_type} Report: Upload complete for dataset {dataset.name}"
135+
136+
context = {
137+
"dataset": dataset,
138+
"task": task,
139+
"user": user,
140+
"subject": "subject"
141+
}
142+
html_message = render_to_string('emailTemplates/upload_task_notification.html', context)
143+
plain_message = strip_tags(html_message)
144+
from_email = 'From <[email protected]>'
145+
to = user.email
146+
mail.send_mail(subject, plain_message, from_email, [to], html_message=html_message)
147+
148+
121149
def notify_user(notification_type, session_key, message, task_id=None):
122150
"""
123151
Call back function after the task has been executed.

wazimap_ng/datasets/views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def post(self, request, *args, **kwargs):
6060
task_name=f"Uploading data: {dataset_obj.name}",
6161
hook="wazimap_ng.datasets.hooks.process_task_info",
6262
key=request.session.session_key,
63-
type="upload", assign=True, notify=True
63+
type="upload", assign=True, notify=True, email=True,
64+
user_id=request.user.id
6465
)
6566
response.data["upload_task_id"] = task
6667

@@ -104,7 +105,8 @@ def dataset_upload(request, dataset_id):
104105
hook="wazimap_ng.datasets.hooks.process_task_info",
105106
key=request.session.session_key,
106107
type="upload", assign=True, notify=False,
107-
update_indicators=update_indicators
108+
update_indicators=update_indicators, email=True,
109+
user_id=request.user.id
108110
)
109111

110112
response["upload_task_id"] = upload_task
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>{{ subject }}</title>
7+
</head>
8+
<body>
9+
Hello {{user.username}},
10+
11+
{% if task.success %}
12+
<p>Upload process for {{dataset.name}} is finished successfully.</p>
13+
{% else %}
14+
<p> Upload process for {{dataset.name}} has failed.</p>
15+
{% endif %}
16+
<p>Please checkout <a href="">{{task.id}}</a> for further details of the process.</p>
17+
18+
<p>Thanks,<br /> Openup Team</p>
19+
</body>
20+
</html>

0 commit comments

Comments
 (0)