Implement our own commenting system for simplicity
This commit is contained in:
7
news/forms.py
Normal file
7
news/forms.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django import forms
|
||||
import models
|
||||
|
||||
class CommentForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = models.Comment
|
||||
exclude = ('author', 'post', 'parent', 'created', 'updated')
|
@@ -1,5 +1,6 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
from mptt.models import MPTTModel, TreeForeignKey
|
||||
|
||||
def unique_slug(item,slug_source,slug_field):
|
||||
"""Ensures a unique slug field by appending an integer counter to duplicate slugs.
|
||||
@@ -49,3 +50,19 @@ class Post(models.Model):
|
||||
def save(self, *args, **kwargs):
|
||||
unique_slug(self, slug_source='title', slug_field='slug')
|
||||
super(Post, self).save(*args, **kwargs)
|
||||
|
||||
class Comment(MPTTModel):
|
||||
post = models.ForeignKey(Post, blank=True, null=True, related_name='comments')
|
||||
parent = TreeForeignKey('self', blank=True, null=True, related_name='children')
|
||||
author = models.ForeignKey(User)
|
||||
body = models.TextField()
|
||||
created = models.DateTimeField(editable=False, auto_now_add=True)
|
||||
updated = models.DateTimeField(editable=False, auto_now=True)
|
||||
|
||||
def news_post(self):
|
||||
if self.parent is None:
|
||||
return self.post
|
||||
return self.get_root().post
|
||||
|
||||
def __unicode__(self):
|
||||
return self.body
|
||||
|
@@ -23,6 +23,8 @@ class NewsFeed(Feed):
|
||||
|
||||
urlpatterns = patterns('news',
|
||||
url('^$', 'views.index'),
|
||||
url('^comment/p/(?P<id>[0-9]+)$', 'views.comment'),
|
||||
url('^comment/c/(?P<parent>[0-9]+)$', 'views.comment'),
|
||||
url('^feed$', NewsFeed()),
|
||||
url('^(?P<page>[0-9]+)$', 'views.index'),
|
||||
url('^(?P<slug>.*)$', 'views.view')
|
||||
|
@@ -2,6 +2,10 @@ import models
|
||||
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
||||
from django.shortcuts import render_to_response
|
||||
from django.template import RequestContext
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.contrib.auth.decorators import login_required
|
||||
import forms
|
||||
|
||||
def index(request, page=0):
|
||||
all_news = models.Post.objects.all()
|
||||
@@ -14,4 +18,22 @@ def index(request, page=0):
|
||||
|
||||
def view(request, slug):
|
||||
item = models.Post.objects.get(slug__exact=slug)
|
||||
return render_to_response('news/view.html', {'item':item}, context_instance = RequestContext(request))
|
||||
form = forms.CommentForm()
|
||||
return render_to_response('news/view.html', {'commentForm': form, 'item':item}, context_instance = RequestContext(request))
|
||||
|
||||
@login_required
|
||||
def comment(request, id=None, parent=None):
|
||||
form = forms.CommentForm(request.POST)
|
||||
if form.is_valid():
|
||||
c = models.Comment()
|
||||
c.author = request.user
|
||||
c.body = form.cleaned_data['body']
|
||||
if parent:
|
||||
parentPost = models.Comment.objects.get(id__exact=parent)
|
||||
c.parent = parentPost
|
||||
elif id:
|
||||
newsPost = models.Post.objects.get(id__exact=id)
|
||||
c.post = newsPost
|
||||
c.save()
|
||||
return HttpResponseRedirect(reverse('news.views.view', kwargs={'slug': c.news_post().slug})+"#c"+str(c.id))
|
||||
|
||||
|
@@ -1,7 +1,6 @@
|
||||
{% extends 'base.html' %}
|
||||
{% load static %}
|
||||
{% load markup %}
|
||||
{% load comments %}
|
||||
|
||||
{% block title %}News{% endblock %}
|
||||
|
||||
@@ -10,10 +9,9 @@
|
||||
<h2><a href="{{post.get_absolute_url}}">{{post.title}}</a></h2>
|
||||
<div class="byline">By {{post.author}}</div>
|
||||
<div class="content">
|
||||
{{post.body|markdown}}
|
||||
{{post.body|truncatewords:"45"|markdown}}
|
||||
</div>
|
||||
{% get_comment_count for post as comment_count %}
|
||||
<div class="commentcount"><a href="{{post.get_absolute_url}}">{{comment_count}} comments »</a></div>
|
||||
<div class="commentcount"><a href="{{post.get_absolute_url}}">{{post.comments.all|length}} comment{{post.comments.all|length|pluralize}} »</a></div>
|
||||
{% endfor %}
|
||||
|
||||
{% get_static_prefix as STATIC_PREFIX %}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
{% extends 'base.html' %}
|
||||
{% load markup %}
|
||||
{% load comments %}
|
||||
{% load mptt_tags %}
|
||||
|
||||
{% block title %}News - {{item.title}}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
@@ -9,26 +10,27 @@
|
||||
<div class="content">
|
||||
{{item.body|markdown}}
|
||||
</div>
|
||||
<div class="commentcount">{% get_comment_count for item as comment_count %}{{comment_count}} comments.</div>
|
||||
<div class="comments">
|
||||
{% if comment_count > 0 %}
|
||||
<h3>Comments</h3>
|
||||
{% get_comment_list for item as comment_list %}
|
||||
{% for comment in comment_list %}
|
||||
<a name="c{{ comment.id }}" href="{% get_comment_permalink comment %}">#{{comment.id}}</a> by {{comment.user}}
|
||||
{% recursetree item.comments %}
|
||||
<div class="comment-tree">
|
||||
<a name="c{{ node.id }}" href="#c{{node.id}}">#{{node.id}}</a> by {{node.author}}
|
||||
<div class="comment">
|
||||
{{comment.comment|markdown:"safe"}}
|
||||
{{node.body|markdown:"safe"}}
|
||||
<form action="{% url news.views.comment parent=node.id %}" method="post">
|
||||
{{ commentForm.as_p }}
|
||||
{% csrf_token %}
|
||||
<input type="submit" name="submit" value="Post">
|
||||
</form>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% get_comment_form for item as form %}
|
||||
{{children}}
|
||||
</div>
|
||||
{% endrecursetree %}
|
||||
<h4>Add a comment</h4>
|
||||
<form action="{% comment_form_target %}" method="post">
|
||||
<form action="{% url news.views.comment id=item.id %}" method="post">
|
||||
{{ commentForm.as_p }}
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<input type="submit" name="submit" value="Post">
|
||||
<input type="submit" name="preview" value="Preview">
|
||||
<input type="hidden" name="next" value="{{post.get_absolute_url}}" />
|
||||
</form>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
Reference in New Issue
Block a user