Commit a3c6faef by wajeeha-khalid

MA-1359 & MA-1457 DiscussionAPI: return response_count for thread GET/POST

parent 450e625d
...@@ -341,7 +341,7 @@ def get_thread_list( ...@@ -341,7 +341,7 @@ def get_thread_list(
if result_page != page: if result_page != page:
raise Http404 raise Http404
results = [ThreadSerializer(thread, remove_fields=['response_count'], context=context).data for thread in threads] results = [ThreadSerializer(thread, context=context).data for thread in threads]
ret = get_paginated_data(request, results, page, num_pages) ret = get_paginated_data(request, results, page, num_pages)
ret["text_search_rewrite"] = text_search_rewrite ret["text_search_rewrite"] = text_search_rewrite
return ret return ret
...@@ -555,7 +555,7 @@ def create_thread(request, thread_data): ...@@ -555,7 +555,7 @@ def create_thread(request, thread_data):
): ):
thread_data = thread_data.copy() thread_data = thread_data.copy()
thread_data["group_id"] = get_cohort_id(user, course_key) thread_data["group_id"] = get_cohort_id(user, course_key)
serializer = ThreadSerializer(data=thread_data, remove_fields=['response_count'], context=context) serializer = ThreadSerializer(data=thread_data, context=context)
actions_form = ThreadActionsForm(thread_data) actions_form = ThreadActionsForm(thread_data)
if not (serializer.is_valid() and actions_form.is_valid()): if not (serializer.is_valid() and actions_form.is_valid()):
raise ValidationError(dict(serializer.errors.items() + actions_form.errors.items())) raise ValidationError(dict(serializer.errors.items() + actions_form.errors.items()))
...@@ -642,8 +642,7 @@ def update_thread(request, thread_id, update_data): ...@@ -642,8 +642,7 @@ def update_thread(request, thread_id, update_data):
""" """
cc_thread, context = _get_thread_and_context(request, thread_id) cc_thread, context = _get_thread_and_context(request, thread_id)
_check_editable_fields(cc_thread, update_data, context) _check_editable_fields(cc_thread, update_data, context)
serializer = ThreadSerializer(cc_thread, remove_fields=['response_count'], data=update_data, partial=True, serializer = ThreadSerializer(cc_thread, data=update_data, partial=True, context=context)
context=context)
actions_form = ThreadActionsForm(update_data) actions_form = ThreadActionsForm(update_data)
if not (serializer.is_valid() and actions_form.is_valid()): if not (serializer.is_valid() and actions_form.is_valid()):
raise ValidationError(dict(serializer.errors.items() + actions_form.errors.items())) raise ValidationError(dict(serializer.errors.items() + actions_form.errors.items()))
......
...@@ -197,24 +197,16 @@ class ThreadSerializer(_ContentSerializer): ...@@ -197,24 +197,16 @@ class ThreadSerializer(_ContentSerializer):
non_endorsed_comment_list_url = serializers.SerializerMethodField() non_endorsed_comment_list_url = serializers.SerializerMethodField()
read = serializers.BooleanField(read_only=True) read = serializers.BooleanField(read_only=True)
has_endorsed = serializers.BooleanField(read_only=True, source="endorsed") has_endorsed = serializers.BooleanField(read_only=True, source="endorsed")
response_count = serializers.IntegerField(source="resp_total", read_only=True) response_count = serializers.IntegerField(source="resp_total", read_only=True, required=False)
non_updatable_fields = NON_UPDATABLE_THREAD_FIELDS non_updatable_fields = NON_UPDATABLE_THREAD_FIELDS
# TODO: https://openedx.atlassian.net/browse/MA-1359
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
remove_fields = kwargs.pop('remove_fields', None)
super(ThreadSerializer, self).__init__(*args, **kwargs) super(ThreadSerializer, self).__init__(*args, **kwargs)
# Compensate for the fact that some threads in the comments service do # Compensate for the fact that some threads in the comments service do
# not have the pinned field set # not have the pinned field set
if self.instance and self.instance.get("pinned") is None: if self.instance and self.instance.get("pinned") is None:
self.instance["pinned"] = False self.instance["pinned"] = False
if self.instance and self.instance.get("resp_total") is None:
self.instance["resp_total"] = None
if remove_fields:
# for multiple fields in a list
for field_name in remove_fields:
self.fields.pop(field_name)
def get_pinned(self, obj): def get_pinned(self, obj):
""" """
...@@ -286,7 +278,7 @@ class CommentSerializer(_ContentSerializer): ...@@ -286,7 +278,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()
children = serializers.SerializerMethodField() children = serializers.SerializerMethodField(required=False)
non_updatable_fields = NON_UPDATABLE_COMMENT_FIELDS non_updatable_fields = NON_UPDATABLE_COMMENT_FIELDS
......
...@@ -1368,12 +1368,13 @@ class CreateThreadTest( ...@@ -1368,12 +1368,13 @@ class CreateThreadTest(
@mock.patch("eventtracking.tracker.emit") @mock.patch("eventtracking.tracker.emit")
def test_basic(self, mock_emit): def test_basic(self, mock_emit):
self.register_post_thread_response({ cs_thread = make_minimal_cs_thread({
"id": "test_id", "id": "test_id",
"username": self.user.username, "username": self.user.username,
"created_at": "2015-05-19T00:00:00Z", "created_at": "2015-05-19T00:00:00Z",
"updated_at": "2015-05-19T00:00:00Z", "updated_at": "2015-05-19T00:00:00Z",
}) })
self.register_post_thread_response(cs_thread)
with self.assert_signal_sent(api, 'thread_created', sender=None, user=self.user, exclude_args=('post',)): with self.assert_signal_sent(api, 'thread_created', sender=None, user=self.user, exclude_args=('post',)):
actual = create_thread(self.request, self.minimal_data) actual = create_thread(self.request, self.minimal_data)
expected = { expected = {
...@@ -1403,7 +1404,8 @@ class CreateThreadTest( ...@@ -1403,7 +1404,8 @@ class CreateThreadTest(
"non_endorsed_comment_list_url": None, "non_endorsed_comment_list_url": None,
"editable_fields": ["abuse_flagged", "following", "raw_body", "title", "topic_id", "type", "voted"], "editable_fields": ["abuse_flagged", "following", "raw_body", "title", "topic_id", "type", "voted"],
'read': False, 'read': False,
'has_endorsed': False 'has_endorsed': False,
'response_count': 0,
} }
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
self.assertEqual( self.assertEqual(
...@@ -1949,6 +1951,7 @@ class UpdateThreadTest( ...@@ -1949,6 +1951,7 @@ class UpdateThreadTest(
"editable_fields": ["abuse_flagged", "following", "raw_body", "title", "topic_id", "type", "voted"], "editable_fields": ["abuse_flagged", "following", "raw_body", "title", "topic_id", "type", "voted"],
'read': False, 'read': False,
'has_endorsed': False, 'has_endorsed': False,
'response_count': 0
} }
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
self.assertEqual( self.assertEqual(
......
...@@ -211,7 +211,6 @@ class ThreadSerializerSerializationTest(SerializerTestMixin, SharedModuleStoreTe ...@@ -211,7 +211,6 @@ class ThreadSerializerSerializationTest(SerializerTestMixin, SharedModuleStoreTe
"editable_fields": ["abuse_flagged", "following", "voted"], "editable_fields": ["abuse_flagged", "following", "voted"],
"read": False, "read": False,
"has_endorsed": False, "has_endorsed": False,
"response_count": None,
} }
self.assertEqual(self.serialize(thread), expected) self.assertEqual(self.serialize(thread), expected)
...@@ -262,7 +261,7 @@ class ThreadSerializerSerializationTest(SerializerTestMixin, SharedModuleStoreTe ...@@ -262,7 +261,7 @@ class ThreadSerializerSerializationTest(SerializerTestMixin, SharedModuleStoreTe
del thread_data["resp_total"] del thread_data["resp_total"]
self.register_get_thread_response(thread_data) self.register_get_thread_response(thread_data)
serialized = self.serialize(Thread(id=thread_data["id"])) serialized = self.serialize(Thread(id=thread_data["id"]))
self.assertIsNone(serialized["response_count"], None) self.assertNotIn("response_count", serialized)
@ddt.ddt @ddt.ddt
......
...@@ -400,12 +400,13 @@ class ThreadViewSetCreateTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): ...@@ -400,12 +400,13 @@ class ThreadViewSetCreateTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
def test_basic(self): def test_basic(self):
self.register_get_user_response(self.user) self.register_get_user_response(self.user)
self.register_post_thread_response({ cs_thread = make_minimal_cs_thread({
"id": "test_thread", "id": "test_thread",
"username": self.user.username, "username": self.user.username,
"created_at": "2015-05-19T00:00:00Z", "created_at": "2015-05-19T00:00:00Z",
"updated_at": "2015-05-19T00:00:00Z", "updated_at": "2015-05-19T00:00:00Z",
}) })
self.register_post_thread_response(cs_thread)
request_data = { request_data = {
"course_id": unicode(self.course.id), "course_id": unicode(self.course.id),
"topic_id": "test_topic", "topic_id": "test_topic",
...@@ -441,6 +442,7 @@ class ThreadViewSetCreateTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): ...@@ -441,6 +442,7 @@ class ThreadViewSetCreateTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
"editable_fields": ["abuse_flagged", "following", "raw_body", "title", "topic_id", "type", "voted"], "editable_fields": ["abuse_flagged", "following", "raw_body", "title", "topic_id", "type", "voted"],
"read": False, "read": False,
"has_endorsed": False, "has_endorsed": False,
"response_count": 0,
} }
response = self.client.post( response = self.client.post(
self.url, self.url,
...@@ -536,6 +538,7 @@ class ThreadViewSetPartialUpdateTest(DiscussionAPIViewTestMixin, ModuleStoreTest ...@@ -536,6 +538,7 @@ class ThreadViewSetPartialUpdateTest(DiscussionAPIViewTestMixin, ModuleStoreTest
"editable_fields": ["abuse_flagged", "following", "raw_body", "title", "topic_id", "type", "voted"], "editable_fields": ["abuse_flagged", "following", "raw_body", "title", "topic_id", "type", "voted"],
"read": False, "read": False,
"has_endorsed": False, "has_endorsed": False,
"response_count": 0,
} }
response = self.client.patch( # pylint: disable=no-member response = self.client.patch( # pylint: disable=no-member
self.url, self.url,
......
...@@ -329,9 +329,9 @@ def make_minimal_cs_thread(overrides=None): ...@@ -329,9 +329,9 @@ def make_minimal_cs_thread(overrides=None):
"comments_count": 0, "comments_count": 0,
"unread_comments_count": 0, "unread_comments_count": 0,
"children": [], "children": [],
"resp_total": 0,
"read": False, "read": False,
"endorsed": False, "endorsed": False,
"resp_total": 0,
} }
ret.update(overrides or {}) ret.update(overrides or {})
return ret return ret
......
...@@ -229,6 +229,8 @@ class ThreadViewSet(_ViewMixin, DeveloperErrorViewMixin, ViewSet): ...@@ -229,6 +229,8 @@ class ThreadViewSet(_ViewMixin, DeveloperErrorViewMixin, ViewSet):
* has_endorsed: Boolean indicating whether this thread has been answered * has_endorsed: Boolean indicating whether this thread has been answered
* response_count: The number of direct responses for a thread
**DELETE response values: **DELETE response values:
No content is returned for a DELETE request No content is returned for a DELETE request
......
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