From 42348ef0e86b61f0ee4cb317570ce9b2ac452921 Mon Sep 17 00:00:00 2001
From: Flavio Tarsetti <flavio.tarsetti@idiap.ch>
Date: Mon, 19 Jun 2017 11:03:49 +0200
Subject: [PATCH] [accounts] patch: on godfather validation, activate
 supervisee account and send confirmation email to supervisee

---
 beat/web/accounts/api.py                      | 28 +++++++++++++++++++
 .../mail.godfather_validated.message.txt      |  8 ++++++
 .../mail.godfather_validated.subject.txt      |  1 +
 3 files changed, 37 insertions(+)
 create mode 100644 beat/web/ui/registration/templates/registration/mail.godfather_validated.message.txt
 create mode 100644 beat/web/ui/registration/templates/registration/mail.godfather_validated.subject.txt

diff --git a/beat/web/accounts/api.py b/beat/web/accounts/api.py
index fca48e617..ae449df78 100644
--- a/beat/web/accounts/api.py
+++ b/beat/web/accounts/api.py
@@ -32,6 +32,8 @@ from django.http import Http404
 from django.db import models
 from django.db.models import Q
 from django.core.urlresolvers import reverse
+from django.template import loader
+from django.template import Context
 
 from rest_framework import generics
 from rest_framework import views
@@ -61,6 +63,7 @@ from ..common.responses import BadRequestResponse, ForbiddenResponse
 
 import datetime
 import re
+from urlparse import urlparse
 
 import simplejson as json
 
@@ -134,7 +137,32 @@ class GodfatherAddSuperviseeView(BaseUpdateSupervisionTrackView):
 
         supervisiontrack.save()
         supervisee.profile.save()
+        supervisee.is_active = True
+        supervisee.save()
 
+        from django.core.mail import send_mail
+
+        parsed_url = urlparse(settings.URL_PREFIX)
+        server_address = '%s://%s' % (parsed_url.scheme, parsed_url.hostname)
+
+        c = Context({ 'supervisor': supervisiontrack.godfather,
+                      'supervisee': supervisee,
+                      'prefix': server_address,
+                    })
+
+        try:
+            t = loader.get_template('registration/mail.godfather_validated.subject.txt')
+            subject = t.render(c)
+
+            # Note: e-mail subject *must not* contain newlines
+            subject = settings.EMAIL_SUBJECT_PREFIX + ''.join(subject.splitlines())
+
+            t = loader.get_template('registration/mail.godfather_validated.message.txt')
+            message = t.render(c)
+
+            send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [supervisee.email])
+        except:
+            pass
 
         return Response(status=status.HTTP_204_NO_CONTENT)
 
diff --git a/beat/web/ui/registration/templates/registration/mail.godfather_validated.message.txt b/beat/web/ui/registration/templates/registration/mail.godfather_validated.message.txt
new file mode 100644
index 000000000..a79195657
--- /dev/null
+++ b/beat/web/ui/registration/templates/registration/mail.godfather_validated.message.txt
@@ -0,0 +1,8 @@
+Dear {{ supervisee.first_name }},
+
+Your account was successfully activated by your supervisor {{ supervisor.username }}.
+
+Welcome {{ supervisee.username }},
+
+You can now <a href="{% url 'login' %}">sign in</a> and start your own
+experiments!</p>
diff --git a/beat/web/ui/registration/templates/registration/mail.godfather_validated.subject.txt b/beat/web/ui/registration/templates/registration/mail.godfather_validated.subject.txt
new file mode 100644
index 000000000..af97067a6
--- /dev/null
+++ b/beat/web/ui/registration/templates/registration/mail.godfather_validated.subject.txt
@@ -0,0 +1 @@
+Account successfully activated - Supervision Confirmed
-- 
GitLab