Commits

David Chambers committed 1747480

Added moderation links to comment notification e-mail.

Comments (0)

Files changed (4)

templates/comment.dhtml

 {% load mango_extras %}
 					<article>
 						<div>
-							{{ post.message|html|safe }}
+							{{ comment.message|html|safe }}
 						</div>
 						<footer>
-						{% if post.is_anonymous %}
-							<img alt="" src="{{ post.anonymous_author.email_hash|gravatar }}" />
-							{% if post.anonymous_author.url %}
-							<strong><a href="{{ post.anonymous_author.url }}">{{ post.anonymous_author.name }}</a></strong>
+						{% if comment.is_anonymous %}
+							<img alt="" src="{{ comment.anonymous_author.email_hash|gravatar }}" />
+							{% if comment.anonymous_author.url %}
+							<strong><a href="{{ comment.anonymous_author.url }}">{{ comment.anonymous_author.name }}</a></strong>
 							{% else %}
-							<strong>{{ post.anonymous_author.name }}</strong>
+							<strong>{{ comment.anonymous_author.name }}</strong>
 							{% endif %}
 						{% else %}
-							<img alt="" src="{{ post.author.email_hash|gravatar }}" />
-							{% if post.author.url %}
-							<strong><a href="{{ post.author.url }}">{% firstof post.author.display_name post.author.username %}</a></strong>
+							<img alt="" src="{{ comment.author.email_hash|gravatar }}" />
+							{% if comment.author.url %}
+							<strong><a href="{{ comment.author.url }}">{% firstof comment.author.display_name comment.author.username %}</a></strong>
 							{% else %}
-							<strong>{{ post.author.name }}</strong>
+							<strong>{{ comment.author.name }}</strong>
 							{% endif %}
 						{% endif %}
-							<time datetime="{{ post.created_at|isoformat }}">
-								<span>{{ post.created_at|display_date }}</span>
-								<span>{{ post.created_at|display_time }}</span>
+							<time datetime="{{ comment.created_at|isoformat }}">
+								<span>{{ comment.created_at|display_date }}</span>
+								<span>{{ comment.created_at|display_time }}</span>
 							</time>
 						</footer>
 					</article>
 						{% endfor %}
 						</ol>
 					</footer>
-					{% endif %}{% if num_visible_posts or comment %}
+					{% endif %}{% if comments or new_comment %}
 					<h2 id="comments">Comments</h2>
-					{% for post in thread.posts %}{% if post.shown %}{% include 'comment.dhtml' %}{% endif %}{% endfor %}{% if comment %}{{ comment }}
+					{% for comment in comments %}{% include 'comment.dhtml' %}{% endfor %}{% if new_comment %}{{ new_comment }}
 					<p>Your comment is awaiting moderation.</p>
 					{% endif %}{% endif %}{% if thread.allow_comments %}
 					<h3 id="respond">Respond</h3>
     if name and email:
         email = '%s <%s>' % (name, email)
     return email
+
+def convert_html_chars(string):
+    return string.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
 
 import disqus
 
-from django.core.mail import send_mail
+from django.core.mail import EmailMultiAlternatives, send_mail
 from django.http import Http404, HttpResponse, HttpResponseRedirect
 from django.shortcuts import render_to_response
 from django.template import Context, loader
 from mango.forms import CommentForm, ContactForm
 from mango.settings import *
 from mango.templatetags.mango_extras import slugify
-from mango.utils import RE
+from mango.utils import convert_html_chars, RE
 
 MESSAGES = {
     'approve': {'do': 'Approve comment', 'done': 'Comment approved.'},
         context = utils.parse_markdown(utils.get_contents(filepath))
         form = CommentForm()
         thread = None
-        num_total_posts = 0
-        num_visible_posts = 0
         comment = request.session.get('comment')
         if comment:
             del request.session['comment']
             t = loader.get_template('comment.dhtml')
-            comment = t.render(Context({'post': comment}))
+            comment = t.render(Context({'comment': comment}))
 
         def get_forum(forums):
             shortname = DISQUS['shortname']
                     return forum
             raise disqus.APIError
 
+        comments = []
         #TODO - This will need to be updated, I don't currently have a DISQUS account so can't test, but I've changed the identifier
         if settings.__dict__.get('DISQUS'):
             try:
                 dsq = disqus.DisqusService(DISQUS_API_VERSION)
-                dsq.login(DISQUS['api_key'])
+                dsq.login(DISQUS.get('api_key'))
                 forum = get_forum(dsq.get_forum_list())
                 thread = dsq.get_thread_by_url(forum, BASE_URL.rstrip('/') + canon_url)
                 if not thread:
                     thread = dsq.thread_by_identifier(forum, context['meta']['title'], canon_url)['thread']
-                num_total_posts, num_visible_posts = dsq.get_num_posts(forum, [thread])[thread.id]
 
                 if request.method == 'POST':
                     form = CommentForm(request.POST)
                         comment = dsq.create_post(forum=forum, thread=thread, message=message,
                                 author_name=author_name, author_email=author_email, author_url=author_url)
 
-                        if DISQUS.get('pre-moderation'):
-                            # store comment so that it can be displayed to the author even if withheld for moderation
-                            request.session['comment'] = comment
+                        # store comment so that it can be displayed to the author even if withheld for moderation
+                        request.session['comment'] = comment
 
                         # send e-mail notification
                         author = u'%s <%s>' % (author_name, author_email)
                         subject = u'[%s] Comment: "%s"' % (SITE_TITLE.decode('utf-8'), context['meta']['title'])
-                        message = u'%s wrote:\n\n%s\n\nhttp://disqus.com/comments/moderate/%s/' % (
-                                author, message, DISQUS['shortname'])
-                        send_mail(subject, message, author, [utils.primary_author_email()], fail_silently=False)
+
+                        query_string = u'api_key=%s&post_id=%s' % (
+                                hashlib.sha1(DISQUS['api_key']).hexdigest(), 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))
+                        for action, messages in sorted(MESSAGES.items()):
+                            text_content += u'\n\n* %s: %s/moderate/%s?%s' % (
+                                    messages['do'], BASE_URL.rstrip('/'), action, query_string)
+                            html_content += u'<li><a href="%s/moderate/%s?%s">%s</a></li>' % (
+                                    BASE_URL.rstrip('/'), action, convert_html_chars(query_string), messages['do'])
+                        html_content += u'</ul>'
+
+                        msg = EmailMultiAlternatives(subject, text_content, author, [utils.primary_author_email()])
+                        msg.attach_alternative(html_content, 'text/html')
+                        msg.send(fail_silently=False)
 
                         return HttpResponseRedirect('redirect/')
-            except (disqus.APIError, KeyError):
+
+                for c in dsq.get_thread_posts(forum, thread, limit=0, exclude='killed'):
+                    if c.has_been_moderated:
+                        comments.append(c)
+                if DISQUS.get('sort') == 'oldest_first':
+                    comments.sort(key=lambda comment: comment.created_at)
+            except disqus.APIError:
                 pass
 
-            if DISQUS['sort'] == 'oldest_first':
-                try:
-                    thread.posts.sort(key=lambda post: post.created_at)
-                except AttributeError:
-                    pass 
-
         return render_to_string('post.dhtml', dict(context, **dict({
-                    'comment': comment,
+                    'comments': comments,
                     'form': form,
-                    'num_visible_posts': num_visible_posts,
+                    'new_comment': comment,
                     'short_url': short_url,
                     'thread': thread,
                     'type': 'post' if 'datetime' in context['meta'] else 'page',