Commit 18956af0 by wajeeha-khalid

jia/MA-1815 retrieve count for child comments

parent 34cecaee
...@@ -290,6 +290,7 @@ class CommentSerializer(_ContentSerializer): ...@@ -290,6 +290,7 @@ class CommentSerializer(_ContentSerializer):
endorsed_by = serializers.SerializerMethodField() endorsed_by = serializers.SerializerMethodField()
endorsed_by_label = serializers.SerializerMethodField() endorsed_by_label = serializers.SerializerMethodField()
endorsed_at = serializers.SerializerMethodField() endorsed_at = serializers.SerializerMethodField()
child_count = serializers.IntegerField(read_only=True)
children = serializers.SerializerMethodField(required=False) children = serializers.SerializerMethodField(required=False)
non_updatable_fields = NON_UPDATABLE_COMMENT_FIELDS non_updatable_fields = NON_UPDATABLE_COMMENT_FIELDS
......
...@@ -1136,6 +1136,7 @@ class GetCommentListTest(CommentsServiceMockMixin, SharedModuleStoreTestCase): ...@@ -1136,6 +1136,7 @@ class GetCommentListTest(CommentsServiceMockMixin, SharedModuleStoreTestCase):
"endorsed": False, "endorsed": False,
"abuse_flaggers": [], "abuse_flaggers": [],
"votes": {"up_count": 4}, "votes": {"up_count": 4},
"child_count": 0,
"children": [], "children": [],
}, },
{ {
...@@ -1152,6 +1153,7 @@ class GetCommentListTest(CommentsServiceMockMixin, SharedModuleStoreTestCase): ...@@ -1152,6 +1153,7 @@ class GetCommentListTest(CommentsServiceMockMixin, SharedModuleStoreTestCase):
"endorsed": False, "endorsed": False,
"abuse_flaggers": [str(self.user.id)], "abuse_flaggers": [str(self.user.id)],
"votes": {"up_count": 7}, "votes": {"up_count": 7},
"child_count": 0,
"children": [], "children": [],
} }
] ]
...@@ -1174,6 +1176,7 @@ class GetCommentListTest(CommentsServiceMockMixin, SharedModuleStoreTestCase): ...@@ -1174,6 +1176,7 @@ class GetCommentListTest(CommentsServiceMockMixin, SharedModuleStoreTestCase):
"voted": False, "voted": False,
"vote_count": 4, "vote_count": 4,
"editable_fields": ["abuse_flagged", "voted"], "editable_fields": ["abuse_flagged", "voted"],
"child_count": 0,
"children": [], "children": [],
}, },
{ {
...@@ -1194,6 +1197,7 @@ class GetCommentListTest(CommentsServiceMockMixin, SharedModuleStoreTestCase): ...@@ -1194,6 +1197,7 @@ class GetCommentListTest(CommentsServiceMockMixin, SharedModuleStoreTestCase):
"voted": False, "voted": False,
"vote_count": 7, "vote_count": 7,
"editable_fields": ["abuse_flagged", "voted"], "editable_fields": ["abuse_flagged", "voted"],
"child_count": 0,
"children": [], "children": [],
}, },
] ]
...@@ -1688,7 +1692,8 @@ class CreateCommentTest( ...@@ -1688,7 +1692,8 @@ class CreateCommentTest(
"voted": False, "voted": False,
"vote_count": 0, "vote_count": 0,
"children": [], "children": [],
"editable_fields": ["abuse_flagged", "raw_body", "voted"] "editable_fields": ["abuse_flagged", "raw_body", "voted"],
"child_count": 0,
} }
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
expected_url = ( expected_url = (
...@@ -2377,7 +2382,8 @@ class UpdateCommentTest( ...@@ -2377,7 +2382,8 @@ class UpdateCommentTest(
"voted": False, "voted": False,
"vote_count": 0, "vote_count": 0,
"children": [], "children": [],
"editable_fields": ["abuse_flagged", "raw_body", "voted"] "editable_fields": ["abuse_flagged", "raw_body", "voted"],
"child_count": 0,
} }
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
self.assertEqual( self.assertEqual(
......
...@@ -312,6 +312,7 @@ class CommentSerializerTest(SerializerTestMixin, SharedModuleStoreTestCase): ...@@ -312,6 +312,7 @@ class CommentSerializerTest(SerializerTestMixin, SharedModuleStoreTestCase):
"abuse_flaggers": [], "abuse_flaggers": [],
"votes": {"up_count": 4}, "votes": {"up_count": 4},
"children": [], "children": [],
"child_count": 0,
} }
expected = { expected = {
"id": "test_comment", "id": "test_comment",
...@@ -332,6 +333,7 @@ class CommentSerializerTest(SerializerTestMixin, SharedModuleStoreTestCase): ...@@ -332,6 +333,7 @@ class CommentSerializerTest(SerializerTestMixin, SharedModuleStoreTestCase):
"vote_count": 4, "vote_count": 4,
"children": [], "children": [],
"editable_fields": ["abuse_flagged", "voted"], "editable_fields": ["abuse_flagged", "voted"],
"child_count": 0,
} }
self.assertEqual(self.serialize(comment), expected) self.assertEqual(self.serialize(comment), expected)
......
...@@ -884,6 +884,34 @@ class CommentViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): ...@@ -884,6 +884,34 @@ class CommentViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
overrides.setdefault("course_id", unicode(self.course.id)) overrides.setdefault("course_id", unicode(self.course.id))
return make_minimal_cs_thread(overrides) return make_minimal_cs_thread(overrides)
def expected_response_comment(self, overrides=None):
"""
create expected response data
"""
response_data = {
"id": "test_comment",
"thread_id": self.thread_id,
"parent_id": None,
"author": self.author.username,
"author_label": None,
"created_at": "1970-01-01T00:00:00Z",
"updated_at": "1970-01-01T00:00:00Z",
"raw_body": "dummy",
"rendered_body": "<p>dummy</p>",
"endorsed": False,
"endorsed_by": None,
"endorsed_by_label": None,
"endorsed_at": None,
"abuse_flagged": False,
"voted": False,
"vote_count": 0,
"children": [],
"editable_fields": ["abuse_flagged", "voted"],
"child_count": 0,
}
response_data.update(overrides or {})
return response_data
def test_thread_id_missing(self): def test_thread_id_missing(self):
response = self.client.get(self.url) response = self.client.get(self.url)
self.assert_response_correct( self.assert_response_correct(
...@@ -918,27 +946,17 @@ class CommentViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): ...@@ -918,27 +946,17 @@ class CommentViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
"endorsed": False, "endorsed": False,
"abuse_flaggers": [], "abuse_flaggers": [],
"votes": {"up_count": 4}, "votes": {"up_count": 4},
"child_count": 0,
"children": [],
}] }]
expected_comments = [{ expected_comments = [self.expected_response_comment(overrides={
"id": "test_comment",
"thread_id": self.thread_id,
"parent_id": None,
"author": self.author.username,
"author_label": None,
"created_at": "2015-05-11T00:00:00Z",
"updated_at": "2015-05-11T11:11:11Z",
"raw_body": "Test body",
"rendered_body": "<p>Test body</p>",
"endorsed": False,
"endorsed_by": None,
"endorsed_by_label": None,
"endorsed_at": None,
"abuse_flagged": False,
"voted": True, "voted": True,
"vote_count": 4, "vote_count": 4,
"editable_fields": ["abuse_flagged", "voted"], "raw_body": "Test body",
"children": [], "rendered_body": "<p>Test body</p>",
}] "created_at": "2015-05-11T00:00:00Z",
"updated_at": "2015-05-11T11:11:11Z",
})]
self.register_get_thread_response({ self.register_get_thread_response({
"id": self.thread_id, "id": self.thread_id,
"course_id": unicode(self.course.id), "course_id": unicode(self.course.id),
...@@ -979,7 +997,6 @@ class CommentViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): ...@@ -979,7 +997,6 @@ class CommentViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
"id": self.thread_id, "id": self.thread_id,
"course_id": unicode(self.course.id), "course_id": unicode(self.course.id),
"thread_type": "discussion", "thread_type": "discussion",
"children": [],
"resp_total": 10, "resp_total": 10,
})) }))
response = self.client.get( response = self.client.get(
...@@ -1060,6 +1077,52 @@ class CommentViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): ...@@ -1060,6 +1077,52 @@ class CommentViewSetListTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
}} }}
) )
def test_child_comments_count(self):
self.register_get_user_response(self.user)
response_1 = make_minimal_cs_comment({
"id": "test_response_1",
"thread_id": self.thread_id,
"user_id": str(self.author.id),
"username": self.author.username,
"child_count": 2,
})
response_2 = make_minimal_cs_comment({
"id": "test_response_2",
"thread_id": self.thread_id,
"user_id": str(self.author.id),
"username": self.author.username,
"child_count": 3,
})
thread = self.make_minimal_cs_thread({
"id": self.thread_id,
"course_id": unicode(self.course.id),
"thread_type": "discussion",
"children": [response_1, response_2],
"resp_total": 2,
"comments_count": 8,
"unread_comments_count": 0,
})
self.register_get_thread_response(thread)
response = self.client.get(self.url, {"thread_id": self.thread_id})
expected_comments = [
self.expected_response_comment(overrides={"id": "test_response_1", "child_count": 2}),
self.expected_response_comment(overrides={"id": "test_response_2", "child_count": 3}),
]
self.assert_response_correct(
response,
200,
{
"results": expected_comments,
"pagination": {
"count": 2,
"next": None,
"num_pages": 1,
"previous": None,
}
}
)
@httpretty.activate @httpretty.activate
@disable_signal(api, 'comment_deleted') @disable_signal(api, 'comment_deleted')
...@@ -1139,6 +1202,7 @@ class CommentViewSetCreateTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): ...@@ -1139,6 +1202,7 @@ class CommentViewSetCreateTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
"vote_count": 0, "vote_count": 0,
"children": [], "children": [],
"editable_fields": ["abuse_flagged", "raw_body", "voted"], "editable_fields": ["abuse_flagged", "raw_body", "voted"],
"child_count": 0,
} }
response = self.client.post( response = self.client.post(
self.url, self.url,
...@@ -1227,6 +1291,7 @@ class CommentViewSetPartialUpdateTest(DiscussionAPIViewTestMixin, ModuleStoreTes ...@@ -1227,6 +1291,7 @@ class CommentViewSetPartialUpdateTest(DiscussionAPIViewTestMixin, ModuleStoreTes
"vote_count": 0, "vote_count": 0,
"children": [], "children": [],
"editable_fields": [], "editable_fields": [],
"child_count": 0,
} }
response_data.update(overrides or {}) response_data.update(overrides or {})
return response_data return response_data
...@@ -1429,7 +1494,8 @@ class CommentViewSetRetrieveTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase ...@@ -1429,7 +1494,8 @@ class CommentViewSetRetrieveTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase
"voted": False, "voted": False,
"vote_count": 0, "vote_count": 0,
"abuse_flagged": False, "abuse_flagged": False,
"editable_fields": ["abuse_flagged", "raw_body", "voted"] "editable_fields": ["abuse_flagged", "raw_body", "voted"],
"child_count": 0,
} }
response = self.client.get(self.url) response = self.client.get(self.url)
......
...@@ -371,6 +371,7 @@ def make_minimal_cs_comment(overrides=None): ...@@ -371,6 +371,7 @@ def make_minimal_cs_comment(overrides=None):
"abuse_flaggers": [], "abuse_flaggers": [],
"votes": {"up_count": 0}, "votes": {"up_count": 0},
"endorsed": False, "endorsed": False,
"child_count": 0,
"children": [], "children": [],
} }
ret.update(overrides or {}) ret.update(overrides or {})
......
...@@ -12,6 +12,7 @@ class Comment(models.Model): ...@@ -12,6 +12,7 @@ class Comment(models.Model):
'endorsed', 'parent_id', 'thread_id', 'username', 'votes', 'user_id', 'endorsed', 'parent_id', 'thread_id', 'username', 'votes', 'user_id',
'closed', 'created_at', 'updated_at', 'depth', 'at_position_list', 'closed', 'created_at', 'updated_at', 'depth', 'at_position_list',
'type', 'commentable_id', 'abuse_flaggers', 'endorsement', 'type', 'commentable_id', 'abuse_flaggers', 'endorsement',
'child_count',
] ]
updatable_fields = [ updatable_fields = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment