David Chambers avatar David Chambers committed d1bd40c

Added templates for comment notification e-mail.

Comments (0)

Files changed (5)

templates/email.dhtml

+{% load mango_extras %}
+<ul style="padding:0;list-style:none">
+<li><a style="text-decoration:none" href="{{ base_url }}/{{ path }}/#respond">
+<img style="border:none;padding-right:5px" src="http://mango.io/static/e/respond.png" />
+Respond</a></li>
+<li><a style="text-decoration:none" href="{{ base_url }}/moderate/approve?api_key={{ api_key }}&amp;post_id={{ post_id }}">
+<img style="border:none;padding-right:5px" src="http://mango.io/static/e/approve.png" />
+Approve comment</a></li>
+<li><a style="text-decoration:none" href="{{ base_url }}/moderate/delete?api_key={{ api_key }}&amp;post_id={{ post_id }}">
+<img style="border:none;padding-right:5px" src="http://mango.io/static/e/delete.png" />
+Delete comment</a></li>
+<li><a style="text-decoration:none" href="{{ base_url }}/moderate/spam?api_key={{ api_key }}&amp;post_id={{ post_id }}">
+<img style="border:none;padding-right:5px" src="http://mango.io/static/e/spam.png" />
+Mark comment as spam (and delete it)</a></li>
+<li><a style="text-decoration:none" href="{{ base_url }}/moderate/close?api_key={{ api_key }}&amp;thread_id={{ thread_id }}">
+<img style="border:none;padding-right:5px" src="http://mango.io/static/e/close.png" />
+Close thread</a></li>
+</ul>
+<p>{{ commenter|convert_html_chars|safe }} wrote:</p>
+<blockquote><p>{{ comment|convert_comment|safe }}</p></blockquote>

templates/email.dtext

+* Respond: {{ base_url }}/{{ path }}/#respond
+* Approve comment: {{ base_url }}/moderate/approve?api_key={{ api_key }}&post_id={{ post_id }}
+* Delete comment: {{ base_url }}/moderate/delete?api_key={{ api_key }}&post_id={{ post_id }}
+* Mark comment as spam (and delete it): {{ base_url }}/moderate/spam?api_key={{ api_key }}&post_id={{ post_id }}
+* Close thread: {{ base_url }}/moderate/close?api_key={{ api_key }}&thread_id={{ thread_id }}
+
+{{ commenter|safe }} wrote:
+
+{{ comment|safe }}

templatetags/mango_extras.py

 @stringfilter
 def convert(string):
     return md.convert(string)
+
+@register.filter
+@stringfilter
+def convert_html_chars(string):
+    return string.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
+
+@register.filter
+@stringfilter
+def convert_comment(string):
+    return re.sub(r'([ \t]*\n){2,}', '</p><p>', convert_html_chars(string))
     if name and email:
         email = '%s <%s>' % (name, email)
     return email
-
-def convert_html_chars(string):
-    return string.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
 from mango.models import Document, RE
 from mango.settings import *
 from mango.templatetags.mango_extras import slugify
-from mango.utils import convert_html_chars
-
-MESSAGES = {
-    'approve': {'do': 'Approve comment', 'done': 'Comment approved.'},
-    'delete': {'do': 'Delete comment', 'done': 'Comment deleted.'},
-    'spam': {'do': 'Mark comment as spam (and delete it)', 'done': 'Comment marked as spam and deleted.'},
-    'close': {'do': 'Close thread', 'done': 'Thread closed.'},
-}
 
 def redirect(request, path):
     return HttpResponseRedirect(reverse('mango.views.post', args=[path]))
                     # send e-mail notification
                     author = u'%s <%s>' % (author_name, author_email)
                     subject = u'[%s] Comment: "%s"' % (SITE_TITLE, document.title)
-
-                    hashed_api_key = hashlib.sha1(DISQUS['api_key']).hexdigest()
-                    query_string = u'api_key=%s&post_id=%s' % (hashed_api_key, comment.id)
-
-                    text_content = u'%s wrote:\n\n%s' % (author, message)
-                    html_content = u'<p>%s wrote:</p>' % convert_html_chars(author)
-                    html_content += u'<blockquote>%s</blockquote><ul>' % re.sub(r'([ \t]*\n){2,}', '</p><p>',
-                            convert_html_chars(message))
-                    m = dict(MESSAGES)
-                    del m['close']
-                    for action, messages in sorted(m.items()):
-                        text_content += u'\n\n* %s: %s/moderate/%s?%s' % (
-                                messages['do'], BASE_URL, action, query_string)
-                        html_content += u'<li><a href="%s/moderate/%s?%s">%s</a></li>' % (
-                                BASE_URL, action, convert_html_chars(query_string), messages['do'])
-                    text_content += u'\n\n* Close thread: %s/moderate/close?api_key=%s&thread_id=%s' % (
-                            BASE_URL, hashed_api_key, thread.id)
-                    html_content += u'<li><a href="%s/moderate/close?%s">Close thread</a></li>' % (
-                            BASE_URL, convert_html_chars(u'api_key=%s&thread_id=%s' % (hashed_api_key, thread.id)))
-                    text_content += u'\n\n* Respond: %s/%s/#respond' % (BASE_URL, path)
-                    html_content += u'<li><a href="%s/%s/#respond">Respond</a></li></ul>' % (BASE_URL, path)
-
-                    msg = EmailMultiAlternatives(subject, text_content, author, [utils.primary_author_email()])
-                    msg.attach_alternative(html_content, 'text/html')
+                    context = {
+                        'commenter': author,
+                        'comment': message,
+                        'base_url': BASE_URL,
+                        'path': path,
+                        'api_key': hashlib.sha1(DISQUS['api_key']).hexdigest(),
+                        'post_id': comment.id,
+                        'thread_id': thread.id,
+                    }
+                    msg = EmailMultiAlternatives(subject, render_to_string('email.dtext', context),
+                            to=[utils.primary_author_email()], headers={'Reply-To': author})
+                    msg.attach_alternative(render_to_string('email.dhtml', context), 'text/html')
                     msg.send(fail_silently=False)
 
                     return HttpResponseRedirect('redirect/')
             return HttpResponseBadRequest('Invalid thread id.')
 
         DSQ.update_thread(FORUM, thread, allow_comments=False)
+        message = 'Thread closed.'
 
     elif action == 'approve':
         DSQ.moderate_post(post_id, 'approve')
+        message = 'Comment approved.'
     else:
         DSQ.moderate_post(post_id, 'kill')
+        message = 'Comment deleted.'
         if action == 'spam':
             DSQ.moderate_post(post_id, 'spam')
+            message = 'Comment marked as spam and deleted.'
 
-    return HttpResponse(MESSAGES[action]['done'], content_type='text/plain; charset=utf-8')
+    return HttpResponse(message, content_type='text/plain; charset=utf-8')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.