Commit 2294baa3 by Scott Duckworth

generate email from template

Also add ability to send HTML emails.
parent 31372fd7
...@@ -105,20 +105,25 @@ class UserKey(models.Model): ...@@ -105,20 +105,25 @@ class UserKey(models.Model):
def send_email_add_key(sender, instance, **kwargs): def send_email_add_key(sender, instance, **kwargs):
if not settings.SSHKEY_EMAIL_ADD_KEY or instance.pk: if not settings.SSHKEY_EMAIL_ADD_KEY or instance.pk:
return return
meta = getattr(instance, 'META', None) from django.template.loader import render_to_string
remote_addr = None from django.core.mail import EmailMultiAlternatives
if meta: from django.core.urlresolvers import reverse
remote_addr = meta.get('REMOTE_ADDR') context_dict = {
body = settings.SSHKEY_EMAIL_ADD_KEY_BODY.format( 'key': instance,
user_first_name = instance.user.first_name, 'subject': settings.SSHKEY_EMAIL_ADD_KEY_SUBJECT,
user_last_name = instance.user.last_name, }
user_full_name = instance.user.get_full_name(), request = getattr(instance, 'request', None)
key_name = instance.name, if request:
key_fingerprint = instance.fingerprint, context_dict['request'] = request
remote_addr = remote_addr or "<unknown>", context_dict['userkey_list_uri'] = request.build_absolute_uri(reverse('django_sshkey.views.userkey_list'))
) text_content = render_to_string('sshkey/add_key.txt', context_dict)
instance.user.email_user( msg = EmailMultiAlternatives(
settings.SSHKEY_EMAIL_ADD_KEY_SUBJECT, settings.SSHKEY_EMAIL_ADD_KEY_SUBJECT,
body, text_content,
settings.SSHKEY_FROM_EMAIL, settings.SSHKEY_FROM_EMAIL,
[instance.user.email],
) )
if settings.SSHKEY_SEND_HTML_EMAIL:
html_content = render_to_string('sshkey/add_key.html', context_dict)
msg.attach_alternative(html_content, 'text/html')
msg.send()
...@@ -34,11 +34,5 @@ SSHKEY_EMAIL_ADD_KEY = getattr(settings, 'SSHKEY_EMAIL_ADD_KEY', True) ...@@ -34,11 +34,5 @@ SSHKEY_EMAIL_ADD_KEY = getattr(settings, 'SSHKEY_EMAIL_ADD_KEY', True)
SSHKEY_EMAIL_ADD_KEY_SUBJECT = getattr(settings, 'SSHKEY_EMAIL_ADD_KEY_SUBJECT', SSHKEY_EMAIL_ADD_KEY_SUBJECT = getattr(settings, 'SSHKEY_EMAIL_ADD_KEY_SUBJECT',
"A new public key was added to your account" "A new public key was added to your account"
) )
SSHKEY_EMAIL_ADD_KEY_BODY = getattr(settings, 'SSHKEY_EMAIL_ADD_KEY_BODY',
"{user_first_name},\n\n"
"The following SSH public key was added to your account from {remote_addr}:\n\n"
"{key_name}\n"
"{key_fingerprint}\n\n"
"If you believe this key was added in error, you should delete the key."
)
SSHKEY_FROM_EMAIL = getattr(settings, 'SSHKEY_FROM_EMAIL', settings.DEFAULT_FROM_EMAIL) SSHKEY_FROM_EMAIL = getattr(settings, 'SSHKEY_FROM_EMAIL', settings.DEFAULT_FROM_EMAIL)
SSHKEY_SEND_HTML_EMAIL = getattr(settings, 'SSHKEY_SEND_HTML_EMAIL', False)
<html>
<body>
<p>{{ key.user.first_name }},</p>
<p>The following SSH public key was added to your account
{% if request.META.SERVER_NAME %}
on {{ request.META.SERVER_NAME }}
{% endif %}
{% if request.META.REMOTE_ADDR %}
from {{ request.META.REMOTE_ADDR }}{% if request.META.REMOTE_HOST %}
({{ request.META.REMOTE_HOST }}){% endif %}{% endif %}:</p>
<p>
Name: {{ key.name }}<br/>
Fingerprint: {{ key.fingerprint }}
</p>
<p><b>If you believe this key was added in error then you should
<a href="{{ userkey_list_uri }}">click here</a> and delete the key.</b></p>
</body>
</html>
{{ key.user.first_name }},
The following SSH public key was added to your account{% if request.META.SERVER_NAME %} on {{ request.META.SERVER_NAME }}{% endif %}{% if request.META.REMOTE_ADDR %}
from {{ request.META.REMOTE_ADDR }}{% if request.META.REMOTE_HOST %} ({{ request.META.REMOTE_HOST }}){% endif %}{% endif %}:
Name: {{ key.name }}
Fingerprint: {{ key.fingerprint }}
If you believe this key was added in error then you should go to
{{ userkey_list_uri }} and delete the key.
...@@ -77,7 +77,7 @@ def userkey_list(request): ...@@ -77,7 +77,7 @@ def userkey_list(request):
def userkey_add(request): def userkey_add(request):
if request.method == 'POST': if request.method == 'POST':
userkey = UserKey(user=request.user) userkey = UserKey(user=request.user)
userkey.META = request.META userkey.request = request
form = UserKeyForm(request.POST, instance=userkey) form = UserKeyForm(request.POST, instance=userkey)
if form.is_valid(): if form.is_valid():
form.save() form.save()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment