Skip to content

Commit

Permalink
Added a way to send email notifications for upload task via api
Browse files Browse the repository at this point in the history
  • Loading branch information
goyal1092 authored and milafrerichs committed May 5, 2021
1 parent f18e0bb commit 84fd34a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
28 changes: 28 additions & 0 deletions wazimap_ng/datasets/hooks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import logging

from django.contrib.sessions.models import Session
from django.contrib.auth.models import User
from django.urls import reverse

from django_q.tasks import async_task
from .models import Indicator

from django.core import mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags

import json

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -84,6 +89,8 @@ def process_task_info(task):
assign_task = task.kwargs.get("assign", False)
update_indicators = task.kwargs.get("update_indicators", False)
session_key = task.kwargs.get("key", False)
email_notification = task.kwargs.get("email", False)
user_id = task.kwargs.get("user_id", False)
results = task.result or {}
obj = next(iter(task.args))

Expand Down Expand Up @@ -118,6 +125,27 @@ def process_task_info(task):
type="data_extraction", assign=False, notify=False
)

if email_notification and user_id:
user = User.objects.filter(id=user_id).first()

if user and user.email:
dataset = task.args[1]
complete_type = "Success" if task.success else "Failure"
subject = F"{complete_type} Report: Upload complete for dataset {dataset.name}"

context = {
"dataset": dataset,
"task": task,
"user": user,
"subject": "subject"
}
html_message = render_to_string('emailTemplates/upload_task_notification.html', context)
plain_message = strip_tags(html_message)
from_email = 'From <[email protected]>'
to = user.email
mail.send_mail(subject, plain_message, from_email, [to], html_message=html_message)


def notify_user(notification_type, session_key, message, task_id=None):
"""
Call back function after the task has been executed.
Expand Down
6 changes: 4 additions & 2 deletions wazimap_ng/datasets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def post(self, request, *args, **kwargs):
task_name=f"Uploading data: {dataset_obj.name}",
hook="wazimap_ng.datasets.hooks.process_task_info",
key=request.session.session_key,
type="upload", assign=True, notify=True
type="upload", assign=True, notify=True, email=True,
user_id=request.user.id
)
response.data["upload_task_id"] = task

Expand Down Expand Up @@ -104,7 +105,8 @@ def dataset_upload(request, dataset_id):
hook="wazimap_ng.datasets.hooks.process_task_info",
key=request.session.session_key,
type="upload", assign=True, notify=False,
update_indicators=update_indicators
update_indicators=update_indicators, email=True,
user_id=request.user.id
)

response["upload_task_id"] = upload_task
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{ subject }}</title>
</head>
<body>
Hello {{user.username}},

{% if task.success %}
<p>Upload process for {{dataset.name}} is finished successfully.</p>
{% else %}
<p> Upload process for {{dataset.name}} has failed.</p>
{% endif %}
<p>Please checkout <a href="">{{task.id}}</a> for further details of the process.</p>

<p>Thanks,<br /> Openup Team</p>
</body>
</html>

0 comments on commit 84fd34a

Please sign in to comment.