-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a way to send email notifications for upload task via api
- Loading branch information
1 parent
f18e0bb
commit 84fd34a
Showing
3 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__) | ||
|
@@ -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 <[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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
wazimap_ng/general/templates/emailTemplates/upload_task_notification.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |