From b9099b7e32e73d7d1e2f3e76fefb4d4ea9e0200f Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Fri, 27 Aug 2010 10:17:42 -0400 Subject: [PATCH] Added a comment delete view --- dialogos/models.py | 2 ++ dialogos/tests.py | 23 +++++++++++++++++++++++ dialogos/urls.py | 2 ++ dialogos/views.py | 11 +++++++++++ 4 files changed, 38 insertions(+) diff --git a/dialogos/models.py b/dialogos/models.py index 69f2a16..2bb9463 100644 --- a/dialogos/models.py +++ b/dialogos/models.py @@ -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() diff --git a/dialogos/tests.py b/dialogos/tests.py index bf64f77..9ee21af 100644 --- a/dialogos/tests.py +++ b/dialogos/tests.py @@ -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={ diff --git a/dialogos/urls.py b/dialogos/urls.py index 433e266..d994cf2 100644 --- a/dialogos/urls.py +++ b/dialogos/urls.py @@ -4,4 +4,6 @@ urlpatterns = patterns("dialogos.views", url(r"^comment/(?P\d+)/(?P\d+)/$", "post_comment", name="post_comment"), + url(r"^comment/(?P\d+)/delete/$", "delete_comment", + name="delete_comment") ) diff --git a/dialogos/views.py b/dialogos/views.py index 799a07a..b6f5ab4 100644 --- a/dialogos/views.py +++ b/dialogos/views.py @@ -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)