Commits

Adam Knight committed 07250b7

Basic threaded comments

Comments (0)

Files changed (7)

comments/__init__.py

+# Based on an idea posted at http://root.abl.es/methods/1524/django-threaded-comments/
+
+from models import MPTTComment
+from forms import MPTTCommentForm
+
+def get_model():
+    return MPTTComment
+
+def get_form():
+    return MPTTCommentForm

comments/forms.py

+from django import forms
+from django.contrib.admin import widgets        
+from django.contrib.comments.forms import CommentForm                            
+from models import MPTTComment
+
+class MPTTCommentForm(CommentForm):
+    parent = forms.ModelChoiceField(queryset=MPTTComment.objects.all(), required=False, widget=forms.HiddenInput)
+
+    def get_comment_model(self):
+        # Use our custom comment model instead of the built-in one.
+        return MPTTComment
+
+    def get_comment_create_data(self):
+        # Use the data of the superclass, and add in the parent field field
+        data = super(MPTTCommentForm, self).get_comment_create_data()
+        data['parent'] = self.cleaned_data['parent']
+        return data

comments/models.py

+# from django.db import models
+from django.contrib.comments.models import Comment
+from mptt.models import MPTTModel, TreeForeignKey
+
+
+### COMMENTS ###
+
+class MPTTComment(MPTTModel, Comment):
+    """ Threaded comments - Add support for the parent comment store and MPTT traversal"""
+    # a link to comment that is being replied, if one exists
+    parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
+
+    class MPTTMeta:
+        # comments on one level will be ordered by date of creation
+        order_insertion_by=['submit_date']
+
+    class Meta:
+        ordering=['tree_id','lft']

comments/templates/comments.html

+{% load comments %}
+{% load mptt_tags %}
+
+{% get_comment_list for object as comments %}
+
+{% if comments %}
+{% recursetree comments %}
+            <a name="c{{ node.id }}"></a>
+            {{ node.comment }}
+            {{ node.user }}
+            {{ node.submit_date|timesince }} ago
+            <a href="{{ object.get_absolute_url }}#c{{ node.id }}">#</a>
+        {% render_comment_form for object %}
+
+    {# recursion! children of a given comment #}
+    {% if not node.is_leaf_node %}
+        {{ children }}
+    {% endif %}
+{% endrecursetree %}
+{% endif %}
+
+{% render_comment_form for object %}

comments/templates/form.html

+{% load comments i18n %}
+<form action="{% comment_form_target %}" method="post">{% csrf_token %}
+  {% if next %}<div><input type="hidden" name="next" value="{{ next }}" /></div>{% endif %}
+  {% for field in form %}
+    {% if field.is_hidden %}
+      <div>{{ field }}</div>
+    {% else %}
+      {% if field.errors %}{{ field.errors }}{% endif %}
+      <p
+        {% if field.errors %} class="error"{% endif %}
+        {% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
+        {{ field.label_tag }} {{ field }}
+      </p>
+    {% endif %}
+  {% endfor %}
+  {% if node.id %}    
+      <input type="hidden" name="parent" id="parent_id" value="{{ node.id }}" />
+  {% endif %}
+  <p class="submit">
+    <input type="submit" name="post" class="submit-post" value="{% trans "Post" %}" />
+    <input type="submit" name="preview" class="submit-preview" value="{% trans "Preview" %}" />
+  </p>
+</form>

comments/tests.py

+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.assertEqual(1 + 1, 2)

comments/views.py

+# Create your views here.