From 84fd34aea2b6d86e52cb7b9ca92675756855123d Mon Sep 17 00:00:00 2001 From: Gaurav Goyal Date: Wed, 5 May 2021 19:21:20 +0530 Subject: [PATCH] Added a way to send email notifications for upload task via api --- wazimap_ng/datasets/hooks.py | 28 +++++++++++++++++++ wazimap_ng/datasets/views.py | 6 ++-- .../upload_task_notification.html | 20 +++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 wazimap_ng/general/templates/emailTemplates/upload_task_notification.html diff --git a/wazimap_ng/datasets/hooks.py b/wazimap_ng/datasets/hooks.py index 613ff7fd..839abb45 100644 --- a/wazimap_ng/datasets/hooks.py +++ b/wazimap_ng/datasets/hooks.py @@ -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__) @@ -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)) @@ -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 ' + 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. diff --git a/wazimap_ng/datasets/views.py b/wazimap_ng/datasets/views.py index ba11cd69..85d23d92 100644 --- a/wazimap_ng/datasets/views.py +++ b/wazimap_ng/datasets/views.py @@ -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 @@ -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 diff --git a/wazimap_ng/general/templates/emailTemplates/upload_task_notification.html b/wazimap_ng/general/templates/emailTemplates/upload_task_notification.html new file mode 100644 index 00000000..a634a6a6 --- /dev/null +++ b/wazimap_ng/general/templates/emailTemplates/upload_task_notification.html @@ -0,0 +1,20 @@ + + + + + + {{ subject }} + + + Hello {{user.username}}, + + {% if task.success %} +

Upload process for {{dataset.name}} is finished successfully.

+ {% else %} +

Upload process for {{dataset.name}} has failed.

+ {% endif %} +

Please checkout {{task.id}} for further details of the process.

+ +

Thanks,
Openup Team

+ +