Commit 600beb28 by Rocky Duan

ajax vote / unvote comments

parent 5d30c2de
...@@ -10,6 +10,7 @@ urlpatterns = patterns('django_comment_client.base.views', ...@@ -10,6 +10,7 @@ urlpatterns = patterns('django_comment_client.base.views',
url(r'threads/(?P<thread_id>[\w\-]+)/delete', 'delete_thread', name='delete_thread'), url(r'threads/(?P<thread_id>[\w\-]+)/delete', 'delete_thread', name='delete_thread'),
url(r'threads/(?P<thread_id>[\w\-]+)/upvote$', 'vote_for_thread', {'value': 'up'}, name='upvote_thread'), url(r'threads/(?P<thread_id>[\w\-]+)/upvote$', 'vote_for_thread', {'value': 'up'}, name='upvote_thread'),
url(r'threads/(?P<thread_id>[\w\-]+)/downvote$', 'vote_for_thread', {'value': 'down'}, name='downvote_thread'), url(r'threads/(?P<thread_id>[\w\-]+)/downvote$', 'vote_for_thread', {'value': 'down'}, name='downvote_thread'),
url(r'threads/(?P<thread_id>[\w\-]+)/unvote$', 'undo_vote_for_thread', name='undo_vote_for_thread'),
url(r'threads/(?P<thread_id>[\w\-]+)/follow$', 'follow_thread', name='follow_thread'), url(r'threads/(?P<thread_id>[\w\-]+)/follow$', 'follow_thread', name='follow_thread'),
url(r'threads/(?P<thread_id>[\w\-]+)/unfollow$', 'unfollow_thread', name='unfollow_thread'), url(r'threads/(?P<thread_id>[\w\-]+)/unfollow$', 'unfollow_thread', name='unfollow_thread'),
...@@ -19,6 +20,7 @@ urlpatterns = patterns('django_comment_client.base.views', ...@@ -19,6 +20,7 @@ urlpatterns = patterns('django_comment_client.base.views',
url(r'comments/(?P<comment_id>[\w\-]+)/delete$', 'delete_comment', name='delete_comment'), url(r'comments/(?P<comment_id>[\w\-]+)/delete$', 'delete_comment', name='delete_comment'),
url(r'comments/(?P<comment_id>[\w\-]+)/upvote$', 'vote_for_comment', {'value': 'up'}, name='upvote_comment'), url(r'comments/(?P<comment_id>[\w\-]+)/upvote$', 'vote_for_comment', {'value': 'up'}, name='upvote_comment'),
url(r'comments/(?P<comment_id>[\w\-]+)/downvote$', 'vote_for_comment', {'value': 'down'}, name='downvote_comment'), url(r'comments/(?P<comment_id>[\w\-]+)/downvote$', 'vote_for_comment', {'value': 'down'}, name='downvote_comment'),
url(r'comments/(?P<comment_id>[\w\-]+)/unvote$', 'undo_vote_for_comment', name='undo_vote_for_comment'),
url(r'(?P<commentable_id>[\w\-]+)/threads/create$', 'create_thread', name='create_thread'), url(r'(?P<commentable_id>[\w\-]+)/threads/create$', 'create_thread', name='create_thread'),
url(r'(?P<commentable_id>[\w\-]+)/follow$', 'follow_commentable', name='follow_commentable'), url(r'(?P<commentable_id>[\w\-]+)/follow$', 'follow_commentable', name='follow_commentable'),
......
...@@ -174,6 +174,13 @@ def vote_for_comment(request, course_id, comment_id, value): ...@@ -174,6 +174,13 @@ def vote_for_comment(request, course_id, comment_id, value):
@login_required @login_required
@require_POST @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): def vote_for_thread(request, course_id, thread_id, value):
user_id = request.user.id user_id = request.user.id
response = comment_client.vote_for_thread(thread_id, user_id, value) response = comment_client.vote_for_thread(thread_id, user_id, value)
...@@ -181,6 +188,13 @@ def vote_for_thread(request, course_id, thread_id, value): ...@@ -181,6 +188,13 @@ def vote_for_thread(request, course_id, thread_id, value):
@login_required @login_required
@require_POST @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): def follow_thread(request, course_id, thread_id):
user_id = request.user.id user_id = request.user.id
response = comment_client.subscribe_thread(user_id, thread_id) response = comment_client.subscribe_thread(user_id, thread_id)
......
...@@ -66,13 +66,13 @@ def vote_for_comment(comment_id, user_id, value, *args, **kwargs): ...@@ -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) 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): 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): 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) 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): 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): def get_notifications(user_id, *args, **kwargs):
return _perform_request('get', _url_for_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): ...@@ -108,8 +108,6 @@ def unsubscribe_thread(user_id, thread_id, *args, **kwargs):
def unsubscribe_commentable(user_id, commentable_id, *args, **kwargs): def unsubscribe_commentable(user_id, commentable_id, *args, **kwargs):
return unsubscribe(user_id, {'source_type': 'other', 'source_id': commentable_id}) return unsubscribe(user_id, {'source_type': 'other', 'source_id': commentable_id})
def _perform_request(method, url, data_or_params=None, *args, **kwargs): def _perform_request(method, url, data_or_params=None, *args, **kwargs):
if method in ['post', 'put', 'patch']: if method in ['post', 'put', 'patch']:
response = requests.request(method, url, data=data_or_params) response = requests.request(method, url, data=data_or_params)
......
...@@ -71,13 +71,34 @@ Discussion = @Discussion ...@@ -71,13 +71,34 @@ Discussion = @Discussion
$discussionContent.attr("status", "normal") $discussionContent.attr("status", "normal")
) )
handleUnvote = (elem) ->
handleVote = (elem, value) -> handleVote = (elem, value) ->
contentType = if $content.hasClass("thread") then "thread" else "comment" contentType = if $content.hasClass("thread") then "thread" else "comment"
url = Discussion.urlFor("#{value}vote_#{contentType}", id) url = Discussion.urlFor("#{value}vote_#{contentType}", id)
$.post url, {}, (response, textStatus) -> Discussion.safeAjax
$elem: $local(".discussion-vote")
url: url
type: "POST"
dataType: "json"
success: (response, textStatus) ->
if textStatus == "success" if textStatus == "success"
Discussion.handleAnchorAndReload(response) $local(".discussion-vote").removeClass("voted")
, 'json' $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) -> handleCancelEdit = (elem) ->
$local(".discussion-content-edit").hide() $local(".discussion-content-edit").hide()
...@@ -213,10 +234,18 @@ Discussion = @Discussion ...@@ -213,10 +234,18 @@ Discussion = @Discussion
handleCancelReply(this) handleCancelReply(this)
"click .discussion-vote-up": -> "click .discussion-vote-up": ->
handleVote(this, "up") $elem = $(this)
if $elem.hasClass("voted")
handleUnvote($elem)
else
handleVote($elem, "up")
"click .discussion-vote-down": -> "click .discussion-vote-down": ->
handleVote(this, "down") $elem = $(this)
if $elem.hasClass("voted")
handleUnvote($elem)
else
handleVote($elem, "down")
"click .discussion-endorse": -> "click .discussion-endorse": ->
handleEndorse(this) handleEndorse(this)
......
...@@ -76,7 +76,7 @@ initializeFollowThread = (index, thread) -> ...@@ -76,7 +76,7 @@ initializeFollowThread = (index, thread) ->
$discussion = $(discussion) $discussion = $(discussion)
$discussionNonContent = $discussion.children(".discussion-non-content") $discussionNonContent = $discussion.children(".discussion-non-content")
$local = Discussion.generateLocal($discussionNonContent)#(selector) -> $discussionNonContent.find(selector) $local = Discussion.generateLocal($discussionNonContent)
id = $discussion.attr("_id") id = $discussion.attr("_id")
......
...@@ -26,6 +26,7 @@ wmdEditors = {} ...@@ -26,6 +26,7 @@ wmdEditors = {}
delete_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/delete" delete_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/delete"
upvote_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/upvote" upvote_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/upvote"
downvote_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/downvote" 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" follow_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/follow"
unfollow_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/unfollow" unfollow_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/unfollow"
update_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/update" update_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/update"
...@@ -34,6 +35,7 @@ wmdEditors = {} ...@@ -34,6 +35,7 @@ wmdEditors = {}
delete_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/delete" delete_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/delete"
upvote_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/upvote" upvote_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/upvote"
downvote_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/downvote" 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" upload : "/courses/#{$$course_id}/discussion/upload"
search : "/courses/#{$$course_id}/discussion/forum/search" search : "/courses/#{$$course_id}/discussion/forum/search"
tags_autocomplete : "/courses/#{$$course_id}/discussion/threads/tags/autocomplete" tags_autocomplete : "/courses/#{$$course_id}/discussion/threads/tags/autocomplete"
......
...@@ -108,7 +108,7 @@ ...@@ -108,7 +108,7 @@
<%def name="render_vote(content)"> <%def name="render_vote(content)">
<div class="discussion-votes"> <div class="discussion-votes">
${render_link("discussion-vote discussion-vote-up", "&#x2C4;")} ${render_link("discussion-vote discussion-vote-up", "&#x2C4;")}
${content['votes']['point']} <div class="discussion-votes-point">${content['votes']['point']}</div>
${render_link("discussion-vote discussion-vote-down", "&#x2C5;")} ${render_link("discussion-vote discussion-vote-down", "&#x2C5;")}
</div> </div>
</%def> </%def>
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