def vote_for_thread(request, course_id, thread_id, value): """ given a course id and thread id vote for this thread ajax only """ thread = cc.Thread.find(thread_id) result = _vote_or_unvote(request, course_id, thread, value) thread_voted.send(sender=None, user=request.user, post=thread) return result
def vote_for_thread(request, course_id, thread_id, value): """ given a course id and thread id vote for this thread ajax only """ course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) user = request.user cc_user = cc.User.from_django_user(user) thread = cc.Thread.find(thread_id) cc_user.vote(thread, value) thread_voted.send(sender=None, user=user, post=thread) return JsonResponse(prepare_content(thread.to_dict(), course_key))
def _vote_or_unvote(request, course_id, obj, value='up', undo_vote=False): """ Vote or unvote for a thread or a response. """ course_key = CourseKey.from_string(course_id) course = get_course_with_access(request.user, 'load', course_key) user = cc.User.from_django_user(request.user) if undo_vote: user.unvote(obj) # TODO(smarnach): Determine the value of the vote that is undone. Currently, you can # only cast upvotes in the user interface, so it is assumed that the vote value is 'up'. # (People could theoretically downvote by handcrafting AJAX requests.) else: user.vote(obj, value) thread_voted.send(sender=None, user=request.user, post=obj) track_voted_event(request, course, obj, value, undo_vote) return JsonResponse(prepare_content(obj.to_dict(), course_key))