Send helpdesk notification on account activation
authorSofia Papagiannaki <papagian@gmail.com>
Tue, 31 Jul 2012 11:00:33 +0000 (14:00 +0300)
committerSofia Papagiannaki <papagian@gmail.com>
Tue, 31 Jul 2012 11:00:33 +0000 (14:00 +0300)
Refs: #2674

snf-astakos-app/astakos/im/functions.py
snf-astakos-app/astakos/im/templates/im/helpdesk_notification.txt [new file with mode: 0644]
snf-astakos-app/astakos/im/views.py

index 877acd4..3de191d 100644 (file)
@@ -128,6 +128,29 @@ def send_admin_notification(user, template_name='im/admin_notification.txt'):
         msg = 'Sent admin notification for user %s' % user.email
         logger._log(LOGGING_LEVEL, msg, [])
 
+def send_helpdesk_notification(user, template_name='im/helpdesk_notification.txt'):
+    """
+    Send email to DEFAULT_CONTACT_EMAIL to notify for a new user activation.
+    
+    Raises SendNotificationError
+    """
+    if not DEFAULT_CONTACT_EMAIL:
+        return
+    message = render_to_string(template_name, {
+            'user': user,
+            'baseurl': BASEURL,
+            'site_name': SITENAME,
+            'support': DEFAULT_ADMIN_EMAIL})
+    sender = DEFAULT_FROM_EMAIL
+    try:
+        send_mail('%s alpha2 testing account notification' % SITENAME, message, sender, [DEFAULT_CONTACT_EMAIL])
+    except (SMTPException, socket.error) as e:
+        logger.exception(e)
+        raise SendNotificationError()
+    else:
+        msg = 'Sent helpdesk admin notification for user %s' % user.email
+        logger._log(LOGGING_LEVEL, msg, [])
+
 def send_invitation(invitation, template_name='im/invitation.txt'):
     """
     Send invitation email.
@@ -273,3 +296,8 @@ class ChangeEmailError(SendMailError):
     def __init__(self):
         self.message = _('Failed to send change email')
         super(ChangeEmailError, self).__init__()
+
+class SendNotificationError(SendMailError):
+    def __init__(self):
+        self.message = _('Failed to send notification email')
+        super(SendNotificationError, self).__init__()
diff --git a/snf-astakos-app/astakos/im/templates/im/helpdesk_notification.txt b/snf-astakos-app/astakos/im/templates/im/helpdesk_notification.txt
new file mode 100644 (file)
index 0000000..13cf7e3
--- /dev/null
@@ -0,0 +1,21 @@
+--- A translation in English follows ---
+
+Έχει ενεργοποιηθεί ο παρακάτω λογαριασμός:
+
+Email:          {{user.email}}
+First name:     {{user.first_name}}
+Last name:      {{user.last_name}}
+Is active:      {{user.is_active}}
+Level:          {{user.level}}
+Invitations:    {{user.invitations}}
+
+--
+
+The following account has been activated:
+
+Email:          {{user.email}}
+First name:     {{user.first_name}}
+Last name:      {{user.last_name}}
+Is active:      {{user.is_active}}
+Level:          {{user.level}}
+Invitations:    {{user.invitations}}
\ No newline at end of file
index 2f8b897..e538a22 100644 (file)
@@ -59,7 +59,7 @@ from astakos.im.activation_backends import get_backend, SimpleBackend
 from astakos.im.util import get_context, prepare_response, set_cookie, get_query
 from astakos.im.forms import *
 from astakos.im.functions import send_greeting, send_feedback, SendMailError, \
-    invite as invite_func, logout as auth_logout
+    invite as invite_func, logout as auth_logout, send_helpdesk_notification
 from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, COOKIE_NAME, COOKIE_DOMAIN, IM_MODULES, SITENAME, LOGOUT_NEXT, LOGGING_LEVEL
 
 logger = logging.getLogger(__name__)
@@ -431,7 +431,8 @@ def logout(request, template='registration/logged_out.html', extra_context={}):
     return response
 
 @transaction.commit_manually
-def activate(request, email_template_name='im/welcome_email.txt', on_failure='im/signup.html'):
+def activate(request, email_template_name='im/welcome_email.txt', on_failure='im/signup.html', 
+                helpdesk_email_template_name='im/helpdesk_notification.txt'):
     """
     Activates the user identified by the ``auth`` request parameter, sends a welcome email
     and renews the user token.
@@ -451,6 +452,7 @@ def activate(request, email_template_name='im/welcome_email.txt', on_failure='im
         messages.add_message(request, messages.ERROR, message)
         return render_response(on_failure)
     
+    notify_helpdesk = False
     try:
         local_user = AstakosUser.objects.get(~Q(id = user.id), email=user.email, is_active=True)
     except AstakosUser.DoesNotExist:
@@ -460,6 +462,7 @@ def activate(request, email_template_name='im/welcome_email.txt', on_failure='im
             user.save()
         except ValidationError, e:
             return HttpResponseBadRequest(e)
+        notify_helpdesk = True
     else:
         # switch the existing account to shibboleth one
         if user.provider == 'shibboleth':
@@ -474,6 +477,8 @@ def activate(request, email_template_name='im/welcome_email.txt', on_failure='im
             user = local_user
         
     try:
+        if notify_helpdesk:
+            send_helpdesk_notification(user, helpdesk_email_template_name)
         send_greeting(user, email_template_name)
         response = prepare_response(request, user, next, renew=True)
         transaction.commit()