Skip to content
Snippets Groups Projects
Commit c0da85ec authored by Stefano Alberto Russo's avatar Stefano Alberto Russo
Browse files

Added task ready notification emails, minor fixes in the send_email utility.

parent f71a7dee
No related branches found
No related tags found
No related merge requests found
...@@ -3,10 +3,11 @@ from django.http import HttpResponse ...@@ -3,10 +3,11 @@ from django.http import HttpResponse
from django.utils import timezone from django.utils import timezone
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from django.conf import settings
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import status, serializers, viewsets from rest_framework import status, serializers, viewsets
from rest_framework.views import APIView from rest_framework.views import APIView
from .utils import format_exception from .utils import format_exception, send_email
from .models import Profile, Task, TaskStatuses from .models import Profile, Task, TaskStatuses
# Setup logging # Setup logging
...@@ -308,6 +309,15 @@ print(port) ...@@ -308,6 +309,15 @@ print(port)
task.ip = task_ip task.ip = task_ip
task.port = int(task_port) task.port = int(task_port)
task.save() task.save()
# Notify the user that the task called back home
logger.info('Sending task ready mail notification to "{}"'.format(task.user.email))
mail_subject = 'Your Task "{}" is up and running'.format(task.container.name)
mail_text = 'Hello,\n\nyour Task "{}" on {} is up and running: {}/tasks/?uuid={}\n\nThe Rosetta notifications bot.'.format(task.container.name, task.computing, settings.DJANGO_PUBLIC_HTTP_HOST, task.uuid)
try:
send_email(to=task.user.email, subject=mail_subject, text=mail_text)
except Exception as e:
logger.error('Cannot send task ready email: "{}"'.format(e))
return HttpResponse('OK') return HttpResponse('OK')
......
...@@ -59,8 +59,17 @@ def send_email(to, subject, text): ...@@ -59,8 +59,17 @@ def send_email(to, subject, text):
subject = subject subject = subject
content = Content('text/plain', text) content = Content('text/plain', text)
mail = Mail(from_email, subject, to_email, content) mail = Mail(from_email, subject, to_email, content)
try:
response = sg.client.mail.send.post(request_body=mail.get()) response = sg.client.mail.send.post(request_body=mail.get())
logger.debug(response)
#logger.debug(response.status_code)
#logger.debug(response.body)
#logger.debug(response.headers)
except Exception as e:
logger.error(e)
#logger.debug(response)
def format_exception(e, debug=False): def format_exception(e, debug=False):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment