David Chambers avatar David Chambers committed 7350557

Added "close thread" to actions list in comment notification e-mail. Made `settings/__init__.py` responsible for creating the `disqus.DisqusService` object.

Comments (0)

Files changed (3)

settings/__init__.py

 import os
 
+import disqus
+
 dirname = os.path.dirname(__file__)
 
 execfile(os.path.join(dirname, 'defaults.py'))
 DISPLAY_DATE_FORMAT = unicode(DISPLAY_DATE_FORMAT, 'utf-8')
 DISPLAY_TIME_FORMAT = unicode(DISPLAY_TIME_FORMAT, 'utf-8')
 
-DISQUS_API_VERSION = u'1.1'
-
 MARKDOWN_DATE_FORMAT = unicode(MARKDOWN_DATE_FORMAT, 'utf-8')
 MARKDOWN_TIME_FORMAT = unicode(MARKDOWN_TIME_FORMAT, 'utf-8')
 
     UNIX_PATH_TO_POSTS = u'/%s' % '/'.join(fragments)
     del fragments, head, tail
 
-del dirname
+DSQ = disqus.DisqusService('1.1')
+try:
+    DSQ.login(DISQUS['api_key'])
+except NameError: # don't suppress KeyError
+    DSQ = None
+
+try:
+    shortname = DISQUS['shortname']
+except NameError: # don't suppress KeyError
+    shortname = None
+
+FORUM = None
+if shortname:
+    for FORUM in DSQ.get_forum_list():
+        if FORUM.shortname == shortname:
+            break
+        FORUM = None
+
+del dirname, shortname
     (r'^feed/$', atom),
     (r'^archives/$', archives),
     (r'^archive/$', lambda request: HttpResponsePermanentRedirect(reverse('mango.views.archives'))),
-    (r'^moderate/(approve|delete|spam)$', moderate),
+    (r'^moderate/(approve|delete|spam|close)$', moderate),
     (r'^(.+?)/redirect/$', redirect),
     (r'^(.+?)/comment/$', post),
     (r'^(.+?)/(m/)?$', post),
     '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):
         del request.session['comment']
         comment = render_to_string('comment.dhtml', {'comment': comment})
 
-    def get_forum(forums):
-        shortname = DISQUS['shortname']
-        for forum in forums:
-            if forum.shortname == shortname:
-                return forum
-        raise disqus.APIError
-
     comments = []
-    if getattr(settings, 'DISQUS', None):
+    if DSQ and FORUM:
         try:
-            dsq = disqus.DisqusService(DISQUS_API_VERSION)
-            dsq.login(DISQUS.get('api_key'))
-            forum = get_forum(dsq.get_forum_list())
             # This call is only here for legacy and migration support, it will never return a value on Mango made threads
-            thread = dsq.get_thread_by_url(forum, document.urls['canon']['abs'])
+            thread = DSQ.get_thread_by_url(FORUM, document.urls['canon']['abs'])
             if not thread:
-                thread = dsq.thread_by_identifier(forum, document.title, document.urls['canon']['rel'])['thread']
+                thread = DSQ.thread_by_identifier(FORUM, document.title, document.urls['canon']['rel'])['thread']
 
             if request.method == 'POST':
                 form = CommentForm(request.POST)
                         ip_address = None
 
                     # send request to Disqus
-                    comment = dsq.create_post(forum, thread, message=message, ip_address=ip_address,
+                    comment = DSQ.create_post(FORUM, thread, message=message, ip_address=ip_address,
                             author_name=author_name, author_email=author_email, author_url=author_url)
 
                     # store comment so that it can be displayed to the author even if withheld for moderation
                     author = u'%s <%s>' % (author_name, author_email)
                     subject = u'[%s] Comment: "%s"' % (SITE_TITLE, document.title)
 
-                    query_string = u'api_key=%s&post_id=%s' % (
-                            hashlib.sha1(DISQUS['api_key']).hexdigest(), comment.id)
+                    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)
                                 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)
 
 
                     return HttpResponseRedirect('redirect/')
 
-            for c in dsq.get_thread_posts(forum, thread, limit=9999, exclude='killed'):
+            for c in DSQ.get_thread_posts(FORUM, thread, limit=9999, exclude='killed'):
                 if c.has_been_moderated:
                     comments.append(c)
             if DISQUS.get('sort') == 'oldest_first':
     return render_to_response('contact.dhtml', dict({'form': form}, **context_defaults(request)))
 
 def moderate(request, action):
+    if not DSQ:
+        return HttpResponseServerError('Invalid DISQUS settings.')
+
     if request.GET.get('api_key') != hashlib.sha1(DISQUS['api_key']).hexdigest():
         return HttpResponseBadRequest('Invalid API key.')
 
     post_id = request.GET.get('post_id')
-    dsq = disqus.DisqusService(DISQUS_API_VERSION)
-    dsq.login(DISQUS['api_key'])
 
-    if action == 'approve':
-        dsq.moderate_post(post_id, 'approve')
+    if action == 'close':
+        if not FORUM:
+            return HttpResponseServerError('Invalid DISQUS settings.')
+
+        thread_id = request.GET.get('thread_id')
+        if not thread_id:
+            return HttpResponseBadRequest('Invalid thread id.')
+
+        thread = None
+        for thread in DSQ.get_thread_list(FORUM, limit=9999):
+            if thread.id == thread_id:
+                break
+            thread = None
+            
+        if not thread:
+            return HttpResponseBadRequest('Invalid thread id.')
+
+        DSQ.update_thread(FORUM, thread, allow_comments=False)
+
+    elif action == 'approve':
+        DSQ.moderate_post(post_id, 'approve')
     else:
-        dsq.moderate_post(post_id, 'kill')
+        DSQ.moderate_post(post_id, 'kill')
         if action == 'spam':
-            dsq.moderate_post(post_id, 'spam')
+            DSQ.moderate_post(post_id, 'spam')
 
     return HttpResponse(MESSAGES[action]['done'], 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.