def _create_comment(request, course_id, thread_id=None, parent_id=None): """ given a course_id, thread_id, and parent_id, create a comment, called from create_comment to do the actual creation """ post = request.POST comment = cc.Comment(**extract(post, ['body'])) course = get_course_with_access(request.user, course_id, 'load') 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.update_attributes(**{ 'anonymous': anonymous, 'anonymous_to_peers': anonymous_to_peers, 'user_id': request.user.id, 'course_id': course_id, 'thread_id': thread_id, 'parent_id': parent_id, }) comment.save() if post.get('auto_subscribe', 'false').lower() == 'true': user = cc.User.from_django_user(request.user) user.follow(comment.thread) if request.is_ajax(): return ajax_content_response(request, course_id, comment.to_dict(), 'discussion/ajax_create_comment.html') else: return JsonResponse(utils.safe_content(comment.to_dict()))
def create_sub_comment(request, course_id, comment_id): """ given a course_id and comment_id, create a response to a comment after checking the max depth allowed, if allowed """ if is_comment_too_deep(parent=cc.Comment(comment_id)): return JsonError(_("Comment level too deep")) return _create_comment(request, CourseKey.from_string(course_id), parent_id=comment_id)
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 mobi_create_comment(request, course_id, thread_id): course_id = course_id.replace('.', '/') if cc_settings.MAX_COMMENT_DEPTH is not None: if cc_settings.MAX_COMMENT_DEPTH < 0: return JsonResponse({ 'success': False, 'errmsg': 'Comment level too deep' }) post = request.POST if 'body' not in post or not post['body'].strip(): return JsonResponse({ "success": False, 'errmsg': 'Body can not be None' }) comment = cc.Comment(**extract(post, ['body'])) try: course = get_course_with_access(request.user, course_id, 'load') except: return JsonResponse({ "success": False, "errmsg": "can not find a course with " + course_id.replace("/", ".") + ' id' }) 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.update_attributes( **{ 'anonymous': anonymous, 'anonymous_to_peers': anonymous_to_peers, 'user_id': request.user.id, 'course_id': course_id, 'thread_id': thread_id, }) comment.save() if post.get('auto_subscribe', 'false').lower() == 'true': user = cc.User.from_django_user(request.user) user.follow(comment.thread) return JsonResponse({"success": True})
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 user = request.user if 'body' not in post or not post['body'].strip(): return JsonError(_("Body can't be empty")) course = get_course_with_access(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=user.id, course_id=course_key.to_deprecated_string(), thread_id=thread_id, parent_id=parent_id, body=post["body"]) comment.save() comment_created.send(sender=None, user=user, post=comment) followed = post.get('auto_subscribe', 'false').lower() == 'true' if followed: cc_user = cc.User.from_django_user(request.user) cc_user.follow(comment.thread) track_comment_created_event(request, course, comment, comment.thread.commentable_id, followed) 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 _create_comment(request, course_id, thread_id=None, parent_id=None): """ given a course_id, thread_id, and parent_id, create a comment, called from create_comment to do the actual creation """ 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, course_id, 'load') 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_id, thread_id=thread_id, parent_id=parent_id, body=post["body"]) comment.save() if post.get('auto_subscribe', 'false').lower() == 'true': user = cc.User.from_django_user(request.user) user.follow(comment.thread) if request.is_ajax(): return ajax_content_response(request, course_id, comment.to_dict()) else: return JsonResponse(utils.safe_content(comment.to_dict()))
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 user = request.user if 'body' not in post or not post['body'].strip(): return JsonError(_("Body can't be empty")) course = get_course_with_access(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=user.id, course_id=course_key.to_deprecated_string(), thread_id=thread_id, parent_id=parent_id, body=post["body"]) comment.save() comment_created.send(sender=None, user=user, post=comment) followed = post.get('auto_subscribe', 'false').lower() == 'true' if followed: cc_user = cc.User.from_django_user(request.user) cc_user.follow(comment.thread) track_comment_created_event(request, course, comment, comment.thread.commentable_id, followed) # # Send notification # # Feature Flag to check that notifications are enabled or not. if settings.FEATURES.get("ENABLE_NOTIFICATIONS", False): action_user_id = request.user.id is_comment = not thread_id and parent_id replying_to_id = None # keep track of who we are replying to if is_comment: # If creating a comment, then we don't have the original thread_id # so we have to get it from the parent comment = cc.Comment.find(parent_id) thread_id = comment.thread_id replying_to_id = comment.user_id thread = cc.Thread.find(thread_id) # IMPORTANT: we have to use getattr here as # otherwise the property will not get fetched # from cs_comment_service thread_user_id = int(getattr(thread, 'user_id', 0)) if not replying_to_id: # we must be creating a Reponse on a thread, # so the original poster is the author of the thread replying_to_id = thread_user_id # # IMPORTANT: We have to use getattr() here so that the # object is fully hydrated. This is a known limitation. # group_id = getattr(thread, 'group_id') if group_id: # We always send a notification to the whole cohort # when someone posts a comment, except the poster _send_discussion_notification( 'open-edx.lms.discussions.cohorted-comment-added', unicode(course_key), thread, request.user, excerpt=_get_excerpt(post["body"]), recipient_group_id=thread.get('group_id'), recipient_exclude_user_ids=[request.user.id], is_anonymous_user=anonymous or anonymous_to_peers) elif parent_id is None and action_user_id != replying_to_id: # we have to only send the notifications when # the user commenting the thread is not # the same user who created the thread # parent_id is None: publish notification only when creating the comment on # the thread not replying on the comment. When the user replied on the comment # the parent_id is not None at that time _send_discussion_notification( 'open-edx.lms.discussions.reply-to-thread', unicode(course_key), thread, request.user, excerpt=_get_excerpt(post["body"]), recipient_user_id=replying_to_id, is_anonymous_user=anonymous or anonymous_to_peers) if request.is_ajax(): return ajax_content_response(request, course_key, comment.to_dict()) else: return JsonResponse(prepare_content(comment.to_dict(), course.id))