diff --git a/lms/djangoapps/django_comment_client/base/urls.py b/lms/djangoapps/django_comment_client/base/urls.py index ec3ba7c625..a22ec401c3 100644 --- a/lms/djangoapps/django_comment_client/base/urls.py +++ b/lms/djangoapps/django_comment_client/base/urls.py @@ -2,16 +2,23 @@ from django.conf.urls.defaults import url, patterns import django_comment_client.base.views urlpatterns = patterns('django_comment_client.base.views', - url(r'(?P[\w\-]+)/threads/create$', 'create_thread', name='create_thread'), + url(r'threads/(?P[\w\-]+)/update$', 'update_thread', name='update_thread'), url(r'threads/(?P[\w\-]+)/reply$', 'create_comment', name='create_comment'), url(r'threads/(?P[\w\-]+)/delete', 'delete_thread', name='delete_thread'), + url(r'threads/(?P[\w\-]+)/upvote$', 'vote_for_thread', {'value': 'up'}, name='upvote_thread'), + url(r'threads/(?P[\w\-]+)/downvote$', 'vote_for_thread', {'value': 'down'}, name='downvote_thread'), + url(r'threads/(?P[\w\-]+)/watch$', 'watch_thread', name='watch_thread'), + url(r'threads/(?P[\w\-]+)/unwatch$', 'unwatch_thread', name='unwatch_thread'), + url(r'comments/(?P[\w\-]+)/update$', 'update_comment', name='update_comment'), url(r'comments/(?P[\w\-]+)/endorse$', 'endorse_comment', name='endorse_comment'), url(r'comments/(?P[\w\-]+)/reply$', 'create_sub_comment', name='create_sub_comment'), url(r'comments/(?P[\w\-]+)/delete$', 'delete_comment', name='delete_comment'), url(r'comments/(?P[\w\-]+)/upvote$', 'vote_for_comment', {'value': 'up'}, name='upvote_comment'), url(r'comments/(?P[\w\-]+)/downvote$', 'vote_for_comment', {'value': 'down'}, name='downvote_comment'), - url(r'threads/(?P[\w\-]+)/upvote$', 'vote_for_thread', {'value': 'up'}, name='upvote_thread'), - url(r'threads/(?P[\w\-]+)/downvote$', 'vote_for_thread', {'value': 'down'}, name='downvote_thread'), + + url(r'(?P[\w\-]+)/threads/create$', 'create_thread', name='create_thread'), + url(r'(?P[\w\-]+)/watch$', 'watch_commentable', name='watch_commentable'), + url(r'(?P[\w\-]+)/unwatch$', 'unwatch_commentable', name='unwatch_commentable'), ) diff --git a/lms/djangoapps/django_comment_client/base/views.py b/lms/djangoapps/django_comment_client/base/views.py index 291bb4c732..518751162d 100644 --- a/lms/djangoapps/django_comment_client/base/views.py +++ b/lms/djangoapps/django_comment_client/base/views.py @@ -129,8 +129,47 @@ def vote_for_thread(request, thread_id, value): response = comment_client.vote_for_thread(thread_id, user_id, value) return JsonResponse(response) -#undo vote: disabled for now +@login_required +@require_POST +def watch_thread(request, thread_id): + user_id = request.user.id + response = comment_client.subscribe_thread(user_id, thread_id) + return JsonResponse(response) +@login_required +@require_POST +def watch_commentable(request, commentable_id): + user_id = request.user.id + response = comment_client.subscribe_commentable(user_id, commentable_id) + return JsonResponse(response) + +@login_required +@require_POST +def follow(request, followed_user_id): + user_id = request.user.id + response = comment_client.follow(user_id, followed_user_id) + return JsonResponse(response) + +@login_required +@require_POST +def unwatch_thread(request, thread_id): + user_id = request.user.id + response = comment_client.unsubscribe_thread(user_id, thread_id) + return JsonResponse(response) + +@login_required +@require_POST +def unwatch_commentable(request, commentable_id): + user_id = request.user.id + response = comment_client.unsubscribe_commentable(user_id, commentable_id) + return JsonResponse(response) + +@login_required +@require_POST +def unfollow(request, followed_user_id): + user_id = request.user.id + response = comment_client.unfollow(user_id, followed_user_id) + return JsonResponse(response) @login_required @require_GET diff --git a/lms/djangoapps/django_comment_client/forum/views.py b/lms/djangoapps/django_comment_client/forum/views.py index e32fda2f5a..a7502bac49 100644 --- a/lms/djangoapps/django_comment_client/forum/views.py +++ b/lms/djangoapps/django_comment_client/forum/views.py @@ -22,6 +22,7 @@ from datehelper import time_ago_in_words import operator import itertools +import json _FULLMODULES = None _DISCUSSIONINFO = None @@ -80,11 +81,12 @@ def get_categorized_discussion_info(request, user, course, course_name, url_cour return _DISCUSSIONINFO def render_accordion(request, course, discussion_info, discussion_id): - context = dict([ - ('course', course), - ('discussion_info', discussion_info), - ('active', discussion_id), # TODO change this later - ('csrf', csrf(request)['csrf_token'])]) + context = { + 'course': course, + 'discussion_info': discussion_info, + 'active': discussion_id, + 'csrf': csrf(request)['csrf_token'], + } return render_to_string('discussion/accordion.html', context) @@ -147,6 +149,7 @@ def single_thread(request, thread_id): 'init': '', 'content': render_single_thread(request, thread_id), 'accordion': '', + 'user_info': json.dumps(comment_client.get_user_info(request.user.id)), } return render_to_response('discussion/index.html', context) diff --git a/lms/static/coffee/src/discussion.coffee b/lms/static/coffee/src/discussion.coffee index b3ee41203c..fe0007a8f0 100644 --- a/lms/static/coffee/src/discussion.coffee +++ b/lms/static/coffee/src/discussion.coffee @@ -9,38 +9,93 @@ $ -> $('#open_close_accordion a').click @toggle $('#accordion').show() - $("section.discussion").each (index, discussion) -> Discussion.bindDiscussionEvents(discussion) + Discussion.initializeDiscussion(discussion) + +generateLocal = (elem) -> + (selector) -> $(elem).find(selector) + +generateDiscussionLink = (cls, txt, handler) -> + $("").addClass("discussion-link"). + attr("href", "javascript:void(0)"). + addClass(cls).html(txt). + click(-> handler(this)) Discussion = urlFor: (name, param) -> { - create_thread : "/discussions/#{param}/threads/create" - update_thread : "/discussions/threads/#{param}/update" - create_comment : "/discussions/threads/#{param}/reply" - delete_thread : "/discussions/threads/#{param}/delete" - update_comment : "/discussions/comments/#{param}/update" - endorse_comment : "/discussions/comments/#{param}/endorse" - create_sub_comment : "/discussions/comments/#{param}/reply" - delete_comment : "/discussions/comments/#{param}/delete" - upvote_comment : "/discussions/comments/#{param}/upvote" - downvote_comment : "/discussions/comments/#{param}/downvote" - upvote_thread : "/discussions/threads/#{param}/upvote" - downvote_thread : "/discussions/threads/#{param}/downvote" - search : "/discussions/forum/search" + watch_commentable : "/discussions/#{param}/watch" + unwatch_commentable : "/discussions/#{param}/unwatch" + create_thread : "/discussions/#{param}/threads/create" + update_thread : "/discussions/threads/#{param}/update" + create_comment : "/discussions/threads/#{param}/reply" + delete_thread : "/discussions/threads/#{param}/delete" + upvote_thread : "/discussions/threads/#{param}/upvote" + downvote_thread : "/discussions/threads/#{param}/downvote" + watch_thread : "/discussions/threads/#{param}/watch" + unwatch_thread : "/discussions/threads/#{param}/unwatch" + update_comment : "/discussions/comments/#{param}/update" + endorse_comment : "/discussions/comments/#{param}/endorse" + create_sub_comment : "/discussions/comments/#{param}/reply" + delete_comment : "/discussions/comments/#{param}/delete" + upvote_comment : "/discussions/comments/#{param}/upvote" + downvote_comment : "/discussions/comments/#{param}/downvote" + search : "/discussions/forum/search" }[name] handleAnchorAndReload: (response) -> #window.location = window.location.pathname + "#" + response['id'] window.location.reload() + initializeDiscussion: (discussion) -> + initializeVote = (index, content) -> + $content = $(content) + $local = generateLocal($content.children(".discussion-content")) + id = $content.attr("_id") + if id in user_info.upvoted_ids + $local(".discussion-vote-up").addClass("voted") + else if id in user_info.downvoted_ids + $local(".discussion-vote-down").addClass("voted") + + + initializeWatchThreads = (index, thread) -> + $thread = $(thread) + id = $thread.attr("_id") + $local = generateLocal($thread.children(".discussion-content")) + + handleWatchThread = (elem) -> + url = Discussion.urlFor('watch_thread', id) + console.log url + $.post url, {}, (response, textStatus) -> + if textStatus == "success" + Discussion.handleAnchorAndReload(response) + , 'json' + + handleUnwatchThread = (elem) -> + url = Discussion.urlFor('unwatch_thread', id) + $.post url, {}, (response, textStatus) -> + if textStatus == "success" + Discussion.handleAnchorAndReload(response) + , 'json' + + if id in user_info.subscribed_thread_ids + unwatchThread = generateDiscussionLink("discussion-unwatch-thread", "Unwatch", handleUnwatchThread) + $local(".info").append(unwatchThread) + else + watchThread = generateDiscussionLink("discussion-watch-thread", "Watch", handleWatchThread) + $local(".info").append(watchThread) + + if user_info? + $(discussion).find(".comment").each(initializeVote) + $(discussion).find(".thread").each(initializeVote).each(initializeWatchThreads) + bindContentEvents: (content) -> $content = $(content) $discussionContent = $content.children(".discussion-content") - $local = (selector) -> $discussionContent.find(selector) + $local = generateLocal($discussionContent) discussionContentHoverIn = -> status = $discussionContent.attr("status") || "normal" @@ -58,11 +113,7 @@ Discussion = $discussionContent.hover(discussionContentHoverIn, discussionContentHoverOut) - generateDiscussionLink = (cls, txt, handler) -> - $("").addClass("discussion-link"). - attr("href", "javascript:void(0)"). - addClass(cls).html(txt). - click(-> handler(this)) + handleReply = (elem) -> editView = $local(".discussion-content-edit") diff --git a/lms/static/sass/_discussion.scss b/lms/static/sass/_discussion.scss index a5c2b5ed4f..724d7b15cf 100644 --- a/lms/static/sass/_discussion.scss +++ b/lms/static/sass/_discussion.scss @@ -66,6 +66,9 @@ $discussion_input_width: 60%; &.discussion-vote-down { margin-top: 3px; } + &.voted { + color: #1d9dd9; + } } } .discussion-right-wrapper { diff --git a/lms/templates/discussion/index.html b/lms/templates/discussion/index.html index d150dc7ddf..c970bfbcf4 100644 --- a/lms/templates/discussion/index.html +++ b/lms/templates/discussion/index.html @@ -8,11 +8,10 @@ <%block name="js_extra"> - + ##<%include file="../course_navigation.html" args="active_page='discussion'" /> diff --git a/lms/templates/discussion/thread.html b/lms/templates/discussion/thread.html index daaffb2939..f76d3de8df 100644 --- a/lms/templates/discussion/thread.html +++ b/lms/templates/discussion/thread.html @@ -2,8 +2,6 @@ <%! from datehelper import time_ago_in_words %> <%! from dateutil.parser import parse %> - - <%def name="render_thread(thread, edit_thread=False, show_comments=False)"> <% if show_comments: @@ -24,7 +22,6 @@ % if edit_thread: ${render_reply()} ${render_edit()} - ${render_watch_thread()} % endif