From 600beb284967d0f86d1f6dad8edda3503dcf67f4 Mon Sep 17 00:00:00 2001 From: Rocky Duan Date: Fri, 3 Aug 2012 14:52:39 -0400 Subject: [PATCH] ajax vote / unvote comments --- .../django_comment_client/base/urls.py | 2 + .../django_comment_client/base/views.py | 14 +++++++ lms/lib/comment_client.py | 6 +-- .../coffee/src/discussion/content.coffee | 41 ++++++++++++++++--- .../coffee/src/discussion/discussion.coffee | 2 +- lms/static/coffee/src/discussion/utils.coffee | 2 + lms/templates/discussion/_thread.html | 2 +- 7 files changed, 57 insertions(+), 12 deletions(-) diff --git a/lms/djangoapps/django_comment_client/base/urls.py b/lms/djangoapps/django_comment_client/base/urls.py index 29e2dc310d..0c3fc24513 100644 --- a/lms/djangoapps/django_comment_client/base/urls.py +++ b/lms/djangoapps/django_comment_client/base/urls.py @@ -10,6 +10,7 @@ urlpatterns = patterns('django_comment_client.base.views', 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\-]+)/unvote$', 'undo_vote_for_thread', name='undo_vote_for_thread'), url(r'threads/(?P[\w\-]+)/follow$', 'follow_thread', name='follow_thread'), url(r'threads/(?P[\w\-]+)/unfollow$', 'unfollow_thread', name='unfollow_thread'), @@ -19,6 +20,7 @@ urlpatterns = patterns('django_comment_client.base.views', 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'comments/(?P[\w\-]+)/unvote$', 'undo_vote_for_comment', name='undo_vote_for_comment'), url(r'(?P[\w\-]+)/threads/create$', 'create_thread', name='create_thread'), url(r'(?P[\w\-]+)/follow$', 'follow_commentable', name='follow_commentable'), diff --git a/lms/djangoapps/django_comment_client/base/views.py b/lms/djangoapps/django_comment_client/base/views.py index ee545cf389..399d396964 100644 --- a/lms/djangoapps/django_comment_client/base/views.py +++ b/lms/djangoapps/django_comment_client/base/views.py @@ -172,6 +172,13 @@ def vote_for_comment(request, course_id, comment_id, value): response = comment_client.vote_for_comment(comment_id, user_id, value) return JsonResponse(response) +@login_required +@require_POST +def undo_vote_for_comment(request, course_id, comment_id): + user_id = request.user.id + response = comment_client.undo_vote_for_comment(comment_id, user_id) + return JsonResponse(response) + @login_required @require_POST def vote_for_thread(request, course_id, thread_id, value): @@ -179,6 +186,13 @@ def vote_for_thread(request, course_id, thread_id, value): response = comment_client.vote_for_thread(thread_id, user_id, value) return JsonResponse(response) +@login_required +@require_POST +def undo_vote_for_thread(request, course_id, thread_id): + user_id = request.user.id + response = comment_client.undo_vote_for_thread(thread_id, user_id) + return JsonResponse(response) + @login_required @require_POST def follow_thread(request, course_id, thread_id): diff --git a/lms/lib/comment_client.py b/lms/lib/comment_client.py index 47e76224e1..18699bf841 100644 --- a/lms/lib/comment_client.py +++ b/lms/lib/comment_client.py @@ -66,13 +66,13 @@ def vote_for_comment(comment_id, user_id, value, *args, **kwargs): return _perform_request('put', _url_for_vote_comment(comment_id), {'user_id': user_id, 'value': value}, *args, **kwargs) def undo_vote_for_comment(comment_id, user_id, *args, **kwargs): - return _perform_request('delete', _url_for_vote_comment(comment_id), *args, **kwargs) + return _perform_request('delete', _url_for_vote_comment(comment_id), {'user_id': user_id}, *args, **kwargs) def vote_for_thread(thread_id, user_id, value, *args, **kwargs): return _perform_request('put', _url_for_vote_thread(thread_id), {'user_id': user_id, 'value': value}, *args, **kwargs) def undo_vote_for_thread(thread_id, user_id, *args, **kwargs): - return _perform_request('delete', _url_for_vote_thread(thread_id), *args, **kwargs) + return _perform_request('delete', _url_for_vote_thread(thread_id), {'user_id': user_id}, *args, **kwargs) def get_notifications(user_id, *args, **kwargs): return _perform_request('get', _url_for_notifications(user_id), *args, **kwargs) @@ -108,8 +108,6 @@ def unsubscribe_thread(user_id, thread_id, *args, **kwargs): def unsubscribe_commentable(user_id, commentable_id, *args, **kwargs): return unsubscribe(user_id, {'source_type': 'other', 'source_id': commentable_id}) - - def _perform_request(method, url, data_or_params=None, *args, **kwargs): if method in ['post', 'put', 'patch']: response = requests.request(method, url, data=data_or_params) diff --git a/lms/static/coffee/src/discussion/content.coffee b/lms/static/coffee/src/discussion/content.coffee index ea9f27d737..93f4842519 100644 --- a/lms/static/coffee/src/discussion/content.coffee +++ b/lms/static/coffee/src/discussion/content.coffee @@ -71,13 +71,34 @@ Discussion = @Discussion $discussionContent.attr("status", "normal") ) + handleUnvote = (elem) -> + handleVote = (elem, value) -> contentType = if $content.hasClass("thread") then "thread" else "comment" url = Discussion.urlFor("#{value}vote_#{contentType}", id) - $.post url, {}, (response, textStatus) -> - if textStatus == "success" - Discussion.handleAnchorAndReload(response) - , 'json' + Discussion.safeAjax + $elem: $local(".discussion-vote") + url: url + type: "POST" + dataType: "json" + success: (response, textStatus) -> + if textStatus == "success" + $local(".discussion-vote").removeClass("voted") + $local(".discussion-vote-#{value}").addClass("voted") + $local(".discussion-votes-point").html response.votes.point + + handleUnvote = (elem, value) -> + contentType = if $content.hasClass("thread") then "thread" else "comment" + url = Discussion.urlFor("undo_vote_for_#{contentType}", id) + Discussion.safeAjax + $elem: $local(".discussion-vote") + url: url + type: "POST" + dataType: "json" + success: (response, textStatus) -> + if textStatus == "success" + $local(".discussion-vote").removeClass("voted") + $local(".discussion-votes-point").html response.votes.point handleCancelEdit = (elem) -> $local(".discussion-content-edit").hide() @@ -213,10 +234,18 @@ Discussion = @Discussion handleCancelReply(this) "click .discussion-vote-up": -> - handleVote(this, "up") + $elem = $(this) + if $elem.hasClass("voted") + handleUnvote($elem) + else + handleVote($elem, "up") "click .discussion-vote-down": -> - handleVote(this, "down") + $elem = $(this) + if $elem.hasClass("voted") + handleUnvote($elem) + else + handleVote($elem, "down") "click .discussion-endorse": -> handleEndorse(this) diff --git a/lms/static/coffee/src/discussion/discussion.coffee b/lms/static/coffee/src/discussion/discussion.coffee index 8141cf9c36..247f65b469 100644 --- a/lms/static/coffee/src/discussion/discussion.coffee +++ b/lms/static/coffee/src/discussion/discussion.coffee @@ -76,7 +76,7 @@ initializeFollowThread = (index, thread) -> $discussion = $(discussion) $discussionNonContent = $discussion.children(".discussion-non-content") - $local = Discussion.generateLocal($discussionNonContent)#(selector) -> $discussionNonContent.find(selector) + $local = Discussion.generateLocal($discussionNonContent) id = $discussion.attr("_id") diff --git a/lms/static/coffee/src/discussion/utils.coffee b/lms/static/coffee/src/discussion/utils.coffee index 424ab6be85..c60682b232 100644 --- a/lms/static/coffee/src/discussion/utils.coffee +++ b/lms/static/coffee/src/discussion/utils.coffee @@ -26,6 +26,7 @@ wmdEditors = {} delete_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/delete" upvote_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/upvote" downvote_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/downvote" + undo_vote_for_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/unvote" follow_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/follow" unfollow_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/unfollow" update_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/update" @@ -34,6 +35,7 @@ wmdEditors = {} delete_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/delete" upvote_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/upvote" downvote_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/downvote" + undo_vote_for_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/unvote" upload : "/courses/#{$$course_id}/discussion/upload" search : "/courses/#{$$course_id}/discussion/forum/search" tags_autocomplete : "/courses/#{$$course_id}/discussion/threads/tags/autocomplete" diff --git a/lms/templates/discussion/_thread.html b/lms/templates/discussion/_thread.html index e033519eb7..1b9fcbadc3 100644 --- a/lms/templates/discussion/_thread.html +++ b/lms/templates/discussion/_thread.html @@ -108,7 +108,7 @@ <%def name="render_vote(content)">
${render_link("discussion-vote discussion-vote-up", "˄")} - ${content['votes']['point']} +
${content['votes']['point']}
${render_link("discussion-vote discussion-vote-down", "˅")}