def flag_abuse_for_thread(request, course_id, thread_id): """ given a course_id and thread_id flag this thread for abuse ajax only """ course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) user = cc.User.from_django_user(request.user) thread = cc.Thread.find(thread_id) thread.flagAbuse(user, thread) return JsonResponse(safe_content(thread.to_dict(), course_key))
def delete_thread(request, course_id, thread_id): # pylint: disable=unused-argument """ given a course_id and thread_id, delete this thread this is ajax only """ course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) thread = cc.Thread.find(thread_id) thread.delete() return JsonResponse(prepare_content(thread.to_dict(), course_key))
def unfollow_thread(request, course_id, thread_id): """ given a course id and thread id, stop following this thread ajax only """ user = cc.User.from_django_user(request.user) thread = cc.Thread.find(thread_id) user.unfollow(thread) thread_unfollowed.send(sender=None, user=request.user, post=thread) return JsonResponse({})
def delete_thread(request, course_id, thread_id): """ given a course_id and thread_id, delete this thread this is ajax only """ course_key = CourseKey.from_string(course_id) thread = cc.Thread.find(thread_id) thread.delete() thread_deleted.send(sender=None, user=request.user, post=thread) return JsonResponse(prepare_content(thread.to_dict(), course_key))
def undo_vote_for_comment(request, course_id, comment_id): """ given a course id and comment id, remove vote ajax only """ course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) user = cc.User.from_django_user(request.user) comment = cc.Comment.find(comment_id) user.unvote(comment) return JsonResponse(prepare_content(comment.to_dict(), course_key))
def delete_comment(request, course_id, comment_id): """ given a course_id and comment_id delete this comment ajax only """ course_key = CourseKey.from_string(course_id) comment = cc.Comment.find(comment_id) comment.delete() comment_deleted.send(sender=None, user=request.user, post=comment) return JsonResponse(prepare_content(comment.to_dict(), course_key))
def un_pin_thread(request, course_id, thread_id): """ given a course id and thread id, remove pin from this thread ajax only """ course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) user = cc.User.from_django_user(request.user) thread = cc.Thread.find(thread_id) thread.un_pin(user, thread_id) return JsonResponse(safe_content(thread.to_dict(), course_key))
def flag_abuse_for_comment(request, course_id, comment_id): """ given a course and comment id, flag comment for abuse ajax only """ course_key = CourseKey.from_string(course_id) user = cc.User.from_django_user(request.user) comment = cc.Comment.find(comment_id) comment.flagAbuse(user, comment) return JsonResponse(prepare_content(comment.to_dict(), course_key))
def ajax_content_response(request, course_key, content): """ Standard AJAX response returning the content hierarchy of the current thread. """ user_info = cc.User.from_django_user(request.user).to_dict() annotated_content_info = get_annotated_content_info(course_key, content, request.user, user_info) return JsonResponse({ 'content': prepare_content(content, course_key), 'annotated_content_info': annotated_content_info, })
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 = cc.User.from_django_user(request.user) thread = cc.Thread.find(thread_id) user.vote(thread, value) return JsonResponse(safe_content(thread.to_dict(), course_key))
def _create_comment(request, course_key, thread_id=None, parent_id=None): """ given a course_key, thread_id, and parent_id, create a comment, called from create_comment to do the actual creation """ assert isinstance(course_key, CourseKey) post = request.POST if 'body' not in post or not post['body'].strip(): return JsonError(_("Body can't be empty")) course = get_course_with_access(request.user, 'load', course_key) if course.allow_anonymous: anonymous = post.get('anonymous', 'false').lower() == 'true' else: anonymous = False if course.allow_anonymous_to_peers: anonymous_to_peers = post.get('anonymous_to_peers', 'false').lower() == 'true' else: anonymous_to_peers = False comment = cc.Comment( anonymous=anonymous, anonymous_to_peers=anonymous_to_peers, user_id=request.user.id, course_id=course_key.to_deprecated_string(), thread_id=thread_id, parent_id=parent_id, body=post["body"] ) comment.save() followed = post.get('auto_subscribe', 'false').lower() == 'true' if followed: user = cc.User.from_django_user(request.user) user.follow(comment.thread) event_data = {'discussion': {'id': comment.thread_id}, 'options': {'followed': followed}} if parent_id: event_data['response'] = {'id': comment.parent_id} event_name = 'edx.forum.comment.created' else: event_name = 'edx.forum.response.created' event_data['commentable_id'] = comment.thread.commentable_id track_forum_event(request, event_name, course, comment, event_data) if request.is_ajax(): return ajax_content_response(request, course_key, comment.to_dict()) else: return JsonResponse(prepare_content(comment.to_dict(), course.id))
def update_thread(request, course_id, thread_id): """ Given a course id and thread id, update a existing thread, used for both static and ajax submissions """ thread = cc.Thread.find(thread_id) thread.update_attributes(**extract(request.POST, ['body', 'title'])) thread.save() if request.is_ajax(): return ajax_content_response(request, course_id, thread.to_dict()) else: return JsonResponse(utils.safe_content(thread.to_dict()))
def un_flag_abuse_for_comment(request, course_id, comment_id): """ given a course_id and comment id, unflag comment for abuse ajax only """ user = cc.User.from_django_user(request.user) course = get_course_by_id(course_id) removeAll = cached_has_permission(request.user, 'openclose_thread', course_id) or has_access(request.user, course, 'staff') comment = cc.Comment.find(comment_id) comment.unFlagAbuse(user, comment, removeAll) return JsonResponse(utils.safe_content(comment.to_dict()))
def un_flag_abuse_for_thread(request, course_id, thread_id): """ given a course id and thread id, remove abuse flag for this thread ajax only """ user = cc.User.from_django_user(request.user) course = get_course_by_id(course_id) thread = cc.Thread.find(thread_id) removeAll = cached_has_permission(request.user, 'openclose_thread', course_id) or has_access(request.user, course, 'staff') thread.unFlagAbuse(user, thread, removeAll) return JsonResponse(utils.safe_content(thread.to_dict()))
def undo_vote_for_thread(request, course_id, thread_id): """ given a course id and thread id, remove users vote for thread ajax only """ course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) user = cc.User.from_django_user(request.user) thread = cc.Thread.find(thread_id) user.unvote(thread) return JsonResponse(prepare_content(thread.to_dict(), course_key))
def vote_for_comment(request, course_id, comment_id, value): """ given a course_id and comment_id, """ course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) user = request.user cc_user = cc.User.from_django_user(user) comment = cc.Comment.find(comment_id) cc_user.vote(comment, value) comment_voted.send(sender=None, user=user, post=comment) return JsonResponse(prepare_content(comment.to_dict(), course_key))
def endorse_comment(request, course_id, comment_id): """ given a course_id and comment_id, toggle the endorsement of this comment, ajax only """ course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) comment = cc.Comment.find(comment_id) comment.endorsed = request.POST.get('endorsed', 'false').lower() == 'true' comment.endorsement_user_id = request.user.id comment.save() return JsonResponse(prepare_content(comment.to_dict(), course_key))
def pin_thread(request, course_id, thread_id): """ given a course id and thread id, pin this thread ajax only """ course_key = CourseKey.from_string(course_id) user = cc.User.from_django_user(request.user) thread = cc.Thread.find(thread_id) thread.pin(user, thread_id) return JsonResponse(prepare_content(thread.to_dict(), course_key))
def un_flag_abuse_for_thread(request, course_id, thread_id): """ given a course id and thread id, remove abuse flag for this thread ajax only """ user = cc.User.from_django_user(request.user) course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course = get_course_by_id(course_key) thread = cc.Thread.find(thread_id) remove_all = cached_has_permission(request.user, 'openclose_thread', course_key) or has_access(request.user, 'staff', course) thread.unFlagAbuse(user, thread, remove_all) return JsonResponse(safe_content(thread.to_dict(), course_key))
def ajax_content_response(request, course_id, content): context = { 'course_id': course_id, 'content': content, } user_info = cc.User.from_django_user(request.user).to_dict() annotated_content_info = utils.get_annotated_content_info( course_id, content, request.user, user_info) return JsonResponse({ 'content': utils.safe_content(content), 'annotated_content_info': annotated_content_info, })
def flag_abuse_for_thread(request, course_id, thread_id): """ given a course_id and thread_id flag this thread for abuse ajax only """ course_key = CourseKey.from_string(course_id) user = cc.User.from_django_user(request.user) thread = cc.Thread.find(thread_id) thread.flagAbuse(user, thread) thread_or_comment_flagged.send(sender=None, user=request.user, post=thread) return JsonResponse(prepare_content(thread.to_dict(), course_key))
def un_flag_abuse_for_comment(request, course_id, comment_id): """ given a course_id and comment id, unflag comment for abuse ajax only """ user = cc.User.from_django_user(request.user) course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course = get_course_by_id(course_key) remove_all = cached_has_permission(request.user, 'openclose_thread', course_key) or has_access(request.user, 'staff', course) comment = cc.Comment.find(comment_id) comment.unFlagAbuse(user, comment, remove_all) return JsonResponse(prepare_content(comment.to_dict(), course_key))
def update_comment(request, course_id, comment_id): """ given a course_id and comment_id, update the comment with payload attributes handles static and ajax submissions """ comment = cc.Comment.find(comment_id) comment.update_attributes(**extract(request.POST, ['body'])) comment.save() if request.is_ajax(): return ajax_content_response(request, course_id, comment.to_dict(), 'discussion/ajax_update_comment.html') else: return JsonResponse(utils.safe_content(comment.to_dict()))
def endorse_comment(request, course_id, comment_id): """ given a course_id and comment_id, toggle the endorsement of this comment, ajax only """ course_key = CourseKey.from_string(course_id) comment = cc.Comment.find(comment_id) user = request.user comment.endorsed = request.POST.get('endorsed', 'false').lower() == 'true' comment.endorsement_user_id = user.id comment.save() comment_endorsed.send(sender=None, user=user, post=comment) return JsonResponse(prepare_content(comment.to_dict(), course_key))
def ajax_content_response(request, course_id, content, template_name): context = { 'course_id': course_id, 'content': content, } html = render_to_string(template_name, context) user_info = cc.User.from_django_user(request.user).to_dict() annotated_content_info = utils.get_annotated_content_info(course_id, content, request.user, user_info) return JsonResponse({ 'html': html, 'content': utils.safe_content(content), 'annotated_content_info': annotated_content_info, })
def openclose_thread(request, course_id, thread_id): """ given a course_id and thread_id, toggle the status of this thread ajax only """ thread = cc.Thread.find(thread_id) thread.closed = request.POST.get('closed', 'false').lower() == 'true' thread.save() thread = thread.to_dict() return JsonResponse({ 'content': utils.safe_content(thread), 'ability': utils.get_ability(course_id, thread, request.user), })
def openclose_thread(request, course_id, thread_id): """ given a course_id and thread_id, toggle the status of this thread ajax only """ course_key = CourseKey.from_string(course_id) thread = cc.Thread.find(thread_id) thread.closed = request.POST.get('closed', 'false').lower() == 'true' thread.save() return JsonResponse({ 'content': prepare_content(thread.to_dict(), course_key), 'ability': get_ability(course_key, thread.to_dict(), request.user), })
def update_comment(request, course_id, comment_id): """ given a course_id and comment_id, update the comment with payload attributes handles static and ajax submissions """ comment = cc.Comment.find(comment_id) if 'body' not in request.POST or not request.POST['body'].strip(): return JsonError(_("Body can't be empty")) comment.update_attributes(**extract(request.POST, ['body'])) comment.save() if request.is_ajax(): return ajax_content_response(request, course_id, comment.to_dict()) else: return JsonResponse(utils.safe_content(comment.to_dict()))
def mobi_reply_list(request, course_id, thread_id): # nr_transaction = newrelic.agent.current_transaction() # course = get_course_with_access(request.user, course_id, 'load_forum') course_id = course_id.replace('.', '/') cc_user = cc.User.from_django_user(request.user) user_info = cc_user.to_dict() try: thread = cc.Thread.find(thread_id) except: return JsonResponse({ "success": False, "errmsg": "can find a thread with " + thread_id + ' id' }) replies_list = [] try: thread_children = thread.children except: thread_children = [] for child in thread_children: if child: child_info = { "id": child["id"], "name": child["username"], "content": child["body"] } replies_list.append(child_info) else: continue return JsonResponse({"replies_list": replies_list, "success": True})
def update_comment(request, course_id, comment_id): """ given a course_id and comment_id, update the comment with payload attributes handles static and ajax submissions """ course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) comment = cc.Comment.find(comment_id) if 'body' not in request.POST or not request.POST['body'].strip(): return JsonError(_("Body can't be empty")) comment.body = request.POST["body"] comment.save() if request.is_ajax(): return ajax_content_response(request, course_key, comment.to_dict()) else: return JsonResponse(prepare_content(comment.to_dict(), course_key))