Browse Source

add support for child posts

Ivan Dyedov 12 years ago
parent
commit
77c7de21c5
2 changed files with 54 additions and 21 deletions
  1. 11 13
      pelicanext/disqus_static/README.rst
  2. 43 8
      pelicanext/disqus_static/disqus_static.py

+ 11 - 13
pelicanext/disqus_static/README.rst

@@ -28,11 +28,11 @@ Usage
 
     {% if article.disqus_comments %}
     <div id="disqus_static_comments">
-        <h4>{{ article.disqus_comments|length }} comments</h4>
+        <h4>{{ article.disqus_comment_count }} comments</h4>
         <ul class="post-list">
-            {% for comment in article.disqus_comments %}
+            {% for comment in article.disqus_comments recursive %}
             <li class="post">
-                <div data-role="post-content" class="post-content">
+                <div class="post-content">
                     <div class="avatar hovercard">
                         <img alt="Avatar" src="{{ comment.author.avatar.small.cache }}">
                     </div>
@@ -41,22 +41,20 @@ Usage
                             <span class="publisher-anchor-color">{{ comment.author.name }}</span>
                             <span class="time-ago" title="{{ comment.createdAt }}">{{ comment.createdAt }}</span>
                         </header>
-                        <div class="post-message-container" data-role="message-container">
-                            <div data-role="message-content">
-                                <div class="post-message publisher-anchor-color " data-role="message">
-                                    {{ comment.message }}
-                                </div>
+                        <div class="post-message-container">
+                            <div class="post-message publisher-anchor-color ">
+                                {{ comment.message }}
                             </div>
                         </div>
                     </div>
                 </div>
+                {% if comment.children %}
+                <ul class="children">
+                    {{ loop(comment.children) }}
+                </ul>
+                {% endif %}
             </li>
             {% endfor %}
         </ul>
     </div>
     {% endif %}
-
-TODO
------
-
- - handle replies to comments properly and maintain parent-child relationships

+ 43 - 8
pelicanext/disqus_static/disqus_static.py

@@ -20,24 +20,59 @@ def initialized(pelican):
 def disqus_static(generator):
     disqus = DisqusAPI(generator.settings['DISQUS_SECRET_KEY'], 
                        generator.settings['DISQUS_PUBLIC_KEY'])
+    # first retrieve the threads
     threads = Paginator(disqus.threads.list, 
                         forum=generator.settings['DISQUS_SITENAME'])
-    thread_dict = {} # disqus thread id => title
+    # build a {thread_id: title} dict
+    thread_dict = {}
     for thread in threads:
         thread_dict[thread['id']] = thread['title']
 
-    posts = Paginator(disqus.posts.list, forum=generator.settings['DISQUS_SITENAME'])
-    post_dict = {} # title => [post1, post2, ...]
+    # now retrieve the posts
+    posts = Paginator(disqus.posts.list, 
+                      forum=generator.settings['DISQUS_SITENAME'])
+
+    # build a {post_id: [child_post1, child_post2, ...]} dict
+    child_dict = {}
+    for post in posts:
+        if post['id'] not in child_dict.keys():
+            child_dict[post['id']] = []
+        if post['parent'] is not None:
+            if str(post['parent']) not in child_dict.keys():
+                child_dict[str(post['parent'])] = []
+            child_dict[str(post['parent'])].append(post)
+
+    # build a {title: [post1, post2, ...]} dict
+    post_dict = {}
     for post in posts:
-        if post['thread'] not in thread_dict.keys():
-            continue
-        if thread_dict[post['thread']] not in post_dict.keys():
-            post_dict[thread_dict[post['thread']]] = []
-        post_dict[thread_dict[post['thread']]].append(post)
+        build_post_dict(post_dict, child_dict, thread_dict, post)
 
     for article in generator.articles:
         if article.title in post_dict:
             article.disqus_comments = post_dict[article.title]
+            article.disqus_comment_count = sum([
+                postcounter(post) for post in post_dict[article.title]])
+
+def postcounter(node):
+    return 1 + sum([postcounter(n) for n in node['children']])
+
+def build_post_dict(post_dict, child_dict, thread_dict, post):
+    if post['thread'] not in thread_dict.keys():
+        return # invalid thread, should never happen
+
+    build_child_dict(child_dict, post)
+
+    if post['parent'] is not None:
+        return # this is a child post, don't want to display it here
+
+    if thread_dict[post['thread']] not in post_dict.keys():
+        post_dict[thread_dict[post['thread']]] = []
+    post_dict[thread_dict[post['thread']]].append(post)
+
+def build_child_dict(child_dict, post):
+    post['children'] = child_dict[post['id']]
+    for child in child_dict[post['id']]:
+        build_child_dict(child_dict, child)
 
 def register():
     signals.initialized.connect(initialized)