Factor out the sending of a templated mail to a function, we're sure
authorMagnus Hagander <magnus@hagander.net>
Tue, 12 Jan 2010 18:50:50 +0000 (19:50 +0100)
committerMagnus Hagander <magnus@hagander.net>
Tue, 12 Jan 2010 18:50:50 +0000 (19:50 +0100)
to want to use this in the future.

pgweb/misc/views.py
pgweb/util/misc.py

index 3f5c3ba577df19a7aa07e652bc70256e368a7977..b21007b6268c7d645a61d7683399f197e223df01 100644 (file)
@@ -1,12 +1,11 @@
 from django.shortcuts import render_to_response, get_object_or_404
 from django.http import HttpResponseRedirect, HttpResponse, Http404
 from django.db import connection
-from email.mime.text import MIMEText
 from django.conf import settings
 
 from pgweb.util.contexts import NavContext
 from pgweb.util.helpers import template_to_string
-from pgweb.util.misc import sendmail
+from pgweb.util.misc import send_template_mail
 
 from pgweb.core.models import Version
 
@@ -20,16 +19,16 @@ def submitbug(request):
                        c.execute("SELECT nextval('bug_id_seq')")
                        bugid = c.fetchall()[0][0]
 
-                       msg = MIMEText(
-                               template_to_string('misc/bugmail.txt', {
+                       send_template_mail(
+                               form.cleaned_data['email'],
+                               settings.BUGREPORT_EMAIL,
+                               'BUG #%s: %s' % (bugid, form.cleaned_data['shortdesc']),
+                               'misc/bugmail.txt',
+                               {
                                        'bugid': bugid,
                                        'bug': form.cleaned_data,
-                               }),
-                               _charset='utf-8')
-                       msg['Subject'] = 'BUG #%s: %s' % (bugid, form.cleaned_data['shortdesc'])
-                       msg['To'] = settings.BUGREPORT_EMAIL
-                       msg['From'] = form.cleaned_data['email']
-                       sendmail(msg)
+                               }
+                       )
 
                        return render_to_response('misc/bug_completed.html', {
                                'bugid': bugid,
index e66f29481566935ad40af331a6fdb0c31c7f6b4b..ac839ccb9f5a146ea1c005d7099455680284682a 100644 (file)
@@ -1,4 +1,7 @@
 from subprocess import Popen, PIPE
+from email.mime.text import MIMEText
+
+from pgweb.util.helpers import template_to_string
 
 def prettySize(size):
        if size < 1024:
@@ -15,3 +18,12 @@ def sendmail(msg):
        pipe.write(msg.as_string())
        pipe.close()
 
+def send_template_mail(sender, receiver, subject, templatename, templateattr={}):
+       msg = MIMEText(
+               template_to_string(templatename, templateattr),
+               _charset='utf-8')
+       msg['Subject'] = subject
+       msg['To'] = receiver
+       msg['From'] = sender
+       sendmail(msg)
+