Commit 72ccb87d by E. Kolpakov

Fixed changing thread type

parent c67fc0d9
...@@ -26,7 +26,8 @@ from django_comment_client.utils import ( ...@@ -26,7 +26,8 @@ from django_comment_client.utils import (
get_ability, get_ability,
JsonError, JsonError,
JsonResponse, JsonResponse,
safe_content safe_content,
get_discussion_categories_ids
) )
from django_comment_client.permissions import check_permissions_by_view, cached_has_permission from django_comment_client.permissions import check_permissions_by_view, cached_has_permission
import lms.lib.comment_client as cc import lms.lib.comment_client as cc
...@@ -164,6 +165,18 @@ def update_thread(request, course_id, thread_id): ...@@ -164,6 +165,18 @@ def update_thread(request, course_id, thread_id):
thread = cc.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
thread.body = request.POST["body"] thread.body = request.POST["body"]
thread.title = request.POST["title"] thread.title = request.POST["title"]
# The following checks should avoid issues we've seen during deploys, where end users are hitting an updated server
# while their browser still has the old client code. This will avoid erasing present values in those cases.
if "thread_type" in request.POST:
thread.thread_type = request.POST["thread_type"]
if "commentable_id" in request.POST:
course = get_course_with_access(request.user, 'load', course_key)
commentable_ids = get_discussion_categories_ids(course)
if request.POST.get("commentable_id") in commentable_ids:
thread.commentable_id = request.POST["commentable_id"]
else:
return JsonError(_("Topic doesn't exist"))
thread.save() thread.save()
if request.is_ajax(): if request.is_ajax():
return ajax_content_response(request, course_key, thread.to_dict()) return ajax_content_response(request, course_key, thread.to_dict())
......
...@@ -42,7 +42,7 @@ class DictionaryTestCase(TestCase): ...@@ -42,7 +42,7 @@ class DictionaryTestCase(TestCase):
@override_settings(MODULESTORE=TEST_DATA_MONGO_MODULESTORE) @override_settings(MODULESTORE=TEST_DATA_MONGO_MODULESTORE)
class AccessUtilsTestCase(TestCase): class AccessUtilsTestCase(ModuleStoreTestCase):
def setUp(self): def setUp(self):
self.course = CourseFactory.create() self.course = CourseFactory.create()
self.course_id = self.course.id self.course_id = self.course.id
...@@ -148,7 +148,7 @@ class CategoryMapTestCase(ModuleStoreTestCase): ...@@ -148,7 +148,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
self.course.discussion_topics = {} self.course.discussion_topics = {}
self.course.save() self.course.save()
self.discussion_num = 0 self.discussion_num = 0
self.maxDiff = None # pylint: disable=C0103 self.maxDiff = None # pylint: disable=C0103
def create_discussion(self, discussion_category, discussion_target, **kwargs): def create_discussion(self, discussion_category, discussion_target, **kwargs):
self.discussion_num += 1 self.discussion_num += 1
...@@ -193,7 +193,7 @@ class CategoryMapTestCase(ModuleStoreTestCase): ...@@ -193,7 +193,7 @@ class CategoryMapTestCase(ModuleStoreTestCase):
} }
) )
check_cohorted_topics([]) # default (empty) cohort config check_cohorted_topics([]) # default (empty) cohort config
self.course.cohort_config = {"cohorted": False, "cohorted_discussions": []} self.course.cohort_config = {"cohorted": False, "cohorted_discussions": []}
check_cohorted_topics([]) check_cohorted_topics([])
...@@ -564,6 +564,46 @@ class CategoryMapTestCase(ModuleStoreTestCase): ...@@ -564,6 +564,46 @@ class CategoryMapTestCase(ModuleStoreTestCase):
} }
) )
def test_ids_empty(self):
self.assertEqual(utils.get_discussion_categories_ids(self.course), [])
def test_ids_configured_topics(self):
self.course.discussion_topics = {
"Topic A": {"id": "Topic_A"},
"Topic B": {"id": "Topic_B"},
"Topic C": {"id": "Topic_C"}
}
self.assertItemsEqual(
utils.get_discussion_categories_ids(self.course),
["Topic_A", "Topic_B", "Topic_C"]
)
def test_ids_inline(self):
self.create_discussion("Chapter 1", "Discussion 1")
self.create_discussion("Chapter 1", "Discussion 2")
self.create_discussion("Chapter 2", "Discussion")
self.create_discussion("Chapter 2 / Section 1 / Subsection 1", "Discussion")
self.create_discussion("Chapter 2 / Section 1 / Subsection 2", "Discussion")
self.create_discussion("Chapter 3 / Section 1", "Discussion")
self.assertItemsEqual(
utils.get_discussion_categories_ids(self.course),
["discussion1", "discussion2", "discussion3", "discussion4", "discussion5", "discussion6"]
)
def test_ids_mixed(self):
self.course.discussion_topics = {
"Topic A": {"id": "Topic_A"},
"Topic B": {"id": "Topic_B"},
"Topic C": {"id": "Topic_C"}
}
self.create_discussion("Chapter 1", "Discussion 1")
self.create_discussion("Chapter 2", "Discussion")
self.create_discussion("Chapter 2 / Section 1 / Subsection 1", "Discussion")
self.assertItemsEqual(
utils.get_discussion_categories_ids(self.course),
["Topic_A", "Topic_B", "Topic_C", "discussion1", "discussion2", "discussion3"]
)
class JsonResponseTestCase(TestCase, UnicodeTestMixin): class JsonResponseTestCase(TestCase, UnicodeTestMixin):
def _test_unicode_data(self, text): def _test_unicode_data(self, text):
......
...@@ -203,6 +203,23 @@ def get_discussion_category_map(course): ...@@ -203,6 +203,23 @@ def get_discussion_category_map(course):
return _filter_unstarted_categories(category_map) return _filter_unstarted_categories(category_map)
def get_discussion_categories_ids(course):
"""
Returns a list of available ids of categories for the course.
"""
ids = []
queue = [get_discussion_category_map(course)]
while queue:
category_map = queue.pop()
for child in category_map["children"]:
if child in category_map["entries"]:
ids.append(category_map["entries"][child]["id"])
else:
queue.append(category_map["subcategories"][child])
return ids
class JsonResponse(HttpResponse): class JsonResponse(HttpResponse):
def __init__(self, data=None): def __init__(self, data=None):
content = json.dumps(data, cls=i4xEncoder) content = json.dumps(data, cls=i4xEncoder)
......
...@@ -22,7 +22,7 @@ class Thread(models.Model): ...@@ -22,7 +22,7 @@ class Thread(models.Model):
updatable_fields = [ updatable_fields = [
'title', 'body', 'anonymous', 'anonymous_to_peers', 'course_id', 'title', 'body', 'anonymous', 'anonymous_to_peers', 'course_id',
'closed', 'user_id', 'commentable_id', 'group_id', 'group_name', 'pinned' 'closed', 'user_id', 'commentable_id', 'group_id', 'group_name', 'pinned', 'thread_type'
] ]
metric_tag_fields = [ metric_tag_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