Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added support fro authenticated commmenting

  • Loading branch information...
commit 828deca17c0591f93ba85c7b3d43f982d64965a1 1 parent ac5782f
Alex Gaynor authored August 20, 2010
15  dialogos/forms.py
@@ -28,3 +28,18 @@ class Meta:
28 28
         fields = [
29 29
             "name", "email", "website", "comment"
30 30
         ]
  31
+
  32
+
  33
+class AuthenticatedCommentForm(BaseCommentForm):
  34
+    class Meta:
  35
+        model = Comment
  36
+        fields = [
  37
+            "comment"
  38
+        ]
  39
+    
  40
+    def save(self, commit=True):
  41
+        comment = super(AuthenticatedCommentForm, self).save(commit=False)
  42
+        comment.author = self.request.user
  43
+        if commit:
  44
+            comment.save()
  45
+        return comment
33  dialogos/tests.py
@@ -9,20 +9,37 @@
9 9
 
10 10
 
11 11
 class CommentTests(TestCase):
  12
+    def setUp(self):
  13
+        self.user = User.objects.create_user("gimli", "myaxe@dwarf.org", "gloin")
  14
+    
  15
+    def post_comment(self, obj, data):
  16
+        return self.post("post_comment",
  17
+            content_type_id=ContentType.objects.get_for_model(obj).pk,
  18
+            object_id=obj.pk,
  19
+            data=data
  20
+        )
  21
+    
12 22
     def test_post_comment(self):
13 23
         g = User.objects.create(username="Gandalf")
14 24
         
15  
-        response = self.post("post_comment", 
16  
-            content_type_id=ContentType.objects.get_for_model(g).pk,
17  
-            object_id=g.pk,
18  
-            data={
19  
-                "name": "Frodo Baggins",
20  
-                "comment": "Where'd you go?",
21  
-            }
22  
-        )
  25
+        response = self.post_comment(g, data={
  26
+            "name": "Frodo Baggins",
  27
+            "comment": "Where'd you go?",
  28
+        })
23 29
         self.assertEqual(response.status_code, 302)
24 30
         
25 31
         self.assertEqual(Comment.objects.count(), 1)
26 32
         c = Comment.objects.get()
27 33
         self.assertEqual(c.author, None)
28 34
         self.assertEqual(c.name, "Frodo Baggins")
  35
+        
  36
+        with self.login("gimli", "gloin"):
  37
+            response = self.post_comment(g, data={
  38
+                "comment": "I thought you were watching the hobbits?"
  39
+            })
  40
+            self.assertEqual(response.status_code, 302)
  41
+            self.assertEqual(Comment.objects.count(), 2)
  42
+            
  43
+            c = Comment.objects.order_by("id")[1]
  44
+            self.assertEqual(c.comment, "I thought you were watching the hobbits?")
  45
+            self.assertEqual(c.author, self.user)
6  dialogos/views.py
@@ -3,7 +3,7 @@
3 3
 
4 4
 from django.contrib.contenttypes.models import ContentType
5 5
 
6  
-from dialogos.forms import UnauthenticatedCommentForm
  6
+from dialogos.forms import UnauthenticatedCommentForm, AuthenticatedCommentForm
7 7
 
8 8
 
9 9
 @require_POST
@@ -11,11 +11,11 @@ def post_comment(request, content_type_id, object_id):
11 11
     content_type = get_object_or_404(ContentType, pk=content_type_id)
12 12
     obj = get_object_or_404(content_type.model_class(), pk=object_id)
13 13
     if request.user.is_authenticated():
14  
-        raise NotImplemented
  14
+        form_class = AuthenticatedCommentForm
15 15
     else:
16 16
         form_class = UnauthenticatedCommentForm
17 17
     form = form_class(request.POST, request=request, obj=obj)
18 18
     if not form.is_valid():
19  
-        raise NotImplemented
  19
+        raise NotImplementedError
20 20
     form.save()
21 21
     return redirect(obj)
3  runtests.py
@@ -10,8 +10,9 @@
10 10
         DATABASE_ENGINE="sqlite3",
11 11
         ROOT_URLCONF="dialogos.urls",
12 12
         INSTALLED_APPS=[
13  
-            "django.contrib.contenttypes",
14 13
             "django.contrib.auth",
  14
+            "django.contrib.contenttypes",
  15
+            "django.contrib.sessions",
15 16
             "dialogos",
16 17
         ]
17 18
     )

0 notes on commit 828deca

Please sign in to comment.
Something went wrong with that request. Please try again.