From 21d690ce9dc4f473f32b3babcac1a7f5e4ad8d5e Mon Sep 17 00:00:00 2001 From: Graham Gilbert Date: Wed, 20 Apr 2016 15:49:38 +0100 Subject: [PATCH] Send an email when a request is approved --- server/templates/server/approve.html | 2 +- server/views.py | 23 +++++++++++++++++++---- smtp.sh | 2 ++ 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100755 smtp.sh diff --git a/server/templates/server/approve.html b/server/templates/server/approve.html index 9e80cc7..ff5fc7c 100755 --- a/server/templates/server/approve.html +++ b/server/templates/server/approve.html @@ -3,7 +3,7 @@ {% load bootstrap3 %} {% block content %}

Approve Request

-

{{ the_request.computer.computername }} ({{ the_request.computer.serial }})

+

{{ the_request.secret.computer.computername }} ({{ the_request.secret.computer.serial }})

{% if error_message %}

{{ error_message }}

{% endif %}
{% csrf_token %} diff --git a/server/views.py b/server/views.py index 9109e07..989035a 100644 --- a/server/views.py +++ b/server/views.py @@ -120,9 +120,7 @@ def request(request, secret_id): """ % (request.user.username, server_name, reverse('server.views.approve', args=[new_request.id])) email_sender = 'requests@%s' % request.META['SERVER_NAME'] send_mail('Crypt Key Request', email_message, email_sender, - [user.email], fail_silently=False) - else: - print 'not using email' + [user.email], fail_silently=True) ##if we're an approver, we'll redirect to the retrieve view if approver: @@ -147,7 +145,7 @@ def retrieve(request, request_id): else: raise Http404 -##approve key view +## approve key view @permission_required('server.can_approve', login_url='/login/') def approve(request, request_id): the_request = get_object_or_404(Request, pk=request_id) @@ -160,6 +158,23 @@ def approve(request, request_id): new_request.auth_user = request.user new_request.date_approved = datetime.now() new_request.save() + + # Send an email to the requester with a link to retrieve (or not) + if hasattr(settings, 'HOST_NAME'): + server_name = settings.HOST_NAME.rstrip('/') + else: + server_name = 'http://crypt' + if new_request.approved == True: + request_status = 'approved' + elif new_request.approved == False: + request_status = 'denied' + if hasattr(settings, 'EMAIL_HOST'): + if new_request.requesting_user.email: + email_message = """ Your key request has been %s by %s. %s%s + """ % (request_status, request.user.username, server_name, reverse('server.views.secret_info', args=[new_request.id])) + email_sender = 'requests@%s' % request.META['SERVER_NAME'] + send_mail('Crypt Key Request', email_message, email_sender, +[new_request.requesting_user.email], fail_silently=True) return redirect('server.views.managerequests') else: form = ApproveForm(instance=the_request) diff --git a/smtp.sh b/smtp.sh new file mode 100755 index 0000000..a6be8ab --- /dev/null +++ b/smtp.sh @@ -0,0 +1,2 @@ +#!/bin/bash +sudo python -m smtpd -n -c DebuggingServer localhost:25