Permalink
Browse files

Added a comment delete view

  • Loading branch information...
1 parent e691301 commit b9099b7e32e73d7d1e2f3e76fefb4d4ea9e0200f @alex alex committed Aug 27, 2010
Showing with 38 additions and 0 deletions.
  1. +2 −0 dialogos/models.py
  2. +23 −0 dialogos/tests.py
  3. +2 −0 dialogos/urls.py
  4. +11 −0 dialogos/views.py
View
@@ -3,6 +3,7 @@
from django.db import models
from django.contrib.auth.models import User
+from django.contrib.contenttypes.generic import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
@@ -15,6 +16,7 @@ class Comment(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.IntegerField()
+ content_object = GenericForeignKey()
comment = models.TextField()
View
@@ -13,6 +13,7 @@
class CommentTests(TestCase):
def setUp(self):
self.user = User.objects.create_user("gimli", "myaxe@dwarf.org", "gloin")
+ self.user2 = User.objects.create_user("aragorn", "theking@gondor.gov", "strider")
def assert_renders(self, tmpl, context, value):
tmpl = Template(tmpl)
@@ -57,6 +58,28 @@ def test_post_comment(self):
self.assertEqual(c.comment, "I thought you were watching the hobbits?")
self.assertEqual(c.author, self.user)
+ def test_delete_comment(self):
+ g = User.objects.create(username="Boromir")
+ with self.login("gimli", "gloin"):
+ response = self.post_comment(g, data={
+ "comment": "Wow, you're a jerk.",
+ })
+ comment = Comment.objects.get()
+
+ response = self.post("delete_comment", comment_id=comment.pk)
+ self.assertEqual(response.status_code, 302)
+ self.assertEqual(Comment.objects.count(), 1)
+
+ with self.login("aragorn", "strider"):
+ response = self.post("delete_comment", comment_id=comment.pk)
+ self.assertEqual(response.status_code, 302)
+ self.assertEqual(Comment.objects.count(), 1)
+
+ with self.login("gimli", "gloin"):
+ response = self.post("delete_comment", comment_id=comment.pk)
+ self.assertEqual(response.status_code, 302)
+ self.assertEqual(Comment.objects.count(), 0)
+
def test_ttag_comment_count(self):
g = User.objects.create(username="Sauron")
self.post_comment(g, data={
View
@@ -4,4 +4,6 @@
urlpatterns = patterns("dialogos.views",
url(r"^comment/(?P<content_type_id>\d+)/(?P<object_id>\d+)/$", "post_comment",
name="post_comment"),
+ url(r"^comment/(?P<comment_id>\d+)/delete/$", "delete_comment",
+ name="delete_comment")
)
View
@@ -1,9 +1,11 @@
from django.views.decorators.http import require_POST
from django.shortcuts import get_object_or_404, redirect
+from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType
from dialogos.forms import UnauthenticatedCommentForm, AuthenticatedCommentForm
+from dialogos.models import Comment
@require_POST
@@ -18,3 +20,12 @@ def post_comment(request, content_type_id, object_id):
if form.is_valid():
form.save()
return redirect(obj)
+
+@login_required
+@require_POST
+def delete_comment(request, comment_id):
+ comment = get_object_or_404(Comment, pk=comment_id)
+ obj = comment.content_object
+ if comment.author == request.user:
+ comment.delete()
+ return redirect(obj)

0 comments on commit b9099b7

Please sign in to comment.