Skip to content

Commit

Permalink
Send an email when a request is approved
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamclarkphoto committed Apr 20, 2016
1 parent 2a4aa85 commit 21d690c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion server/templates/server/approve.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% load bootstrap3 %}
{% block content %}
<h1>Approve Request</h1>
<p>{{ the_request.computer.computername }} ({{ the_request.computer.serial }})</p>
<p>{{ the_request.secret.computer.computername }} ({{ the_request.secret.computer.serial }})</p>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'approve' the_request.id %}" method="post">{% csrf_token %}

Expand Down
23 changes: 19 additions & 4 deletions server/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions smtp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
sudo python -m smtpd -n -c DebuggingServer localhost:25

0 comments on commit 21d690c

Please sign in to comment.