def add_comment(request, post_id): if request.method != 'POST': raise Http404 if 'new_comment'not in request.POST or not request.POST['new_comment']: message = 'You must enter an comment to post.' json_error = '{ "error": "'+message+'" }' return HttpResponse(json_error, content_type='application/json') new_comment = Comment(comment_content=request.POST['new_comment'], comment_by=User.objects.get(id=request.user.id), comment_time=timezone.now(), post=Post.objects.get(id=post_id)) new_comment.save() response_text = serializers.serialize('json', Comment.objects.filter(post__id=post_id)) return HttpResponse(response_text, content_type='application/json')
def comment(request): context={} if request.method == 'GET': return redirect(reverse('stream')) form = CommentForm(request.POST) if not form.is_valid(): return redirect(reverse('stream')) new_comment = Comment(content=form.cleaned_data['content'], user=request.user, post=form.cleaned_data['post']) new_comment.save() on_comment(new_comment) return redirect(reverse('stream'))
def addComment(request): if request.method != 'POST': return _my_json_error_response( "You must use a POST request for this operation", status=404) if 'comment_text' not in request.POST or not request.POST['comment_text']: return _my_json_error_response("You must enter text to add comment.") if 'post_id' not in request.POST or not request.POST['post_id']: return _my_json_error_response("Invalid post id.", status=404) post_id = request.POST['post_id'] try: int(post_id) except ValueError: return _my_json_error_response("Invalid post id format.", status=404) post = Post.objects.get(id=post_id) newComment = Comment(comment_text=request.POST['comment_text'], comment_date_time=timezone.now(), post=post, user=request.user) newComment.save() response_data = [] for comment in Comment.objects.all().order_by('comment_date_time'): newComment = { 'id': comment.id, 'comment_text': comment.comment_text, 'comment_date_time': comment.comment_date_time.isoformat(), 'post_id': comment.post.id, 'name': comment.user.first_name + ' ' + comment.user.last_name, 'user_id': comment.user.id } response_data.append(newComment) response_json = json.dumps(response_data) return HttpResponse(response_json, content_type='application/json')
def add_comment(request): if ((request.method != "POST") or ('comment_text' not in request.POST) or ('post_id' not in request.POST)): return HttpResponseNotFound('<h1>Missing parameters</h1>') p = get_object_or_404(Profile, profile_user = request.user) post = get_object_or_404(Post, id = request.POST['post_id']) # create comment new_comment = Comment() new_comment.comment_date_time = timezone.now() new_comment.comment_profile = p new_comment.comment_input_text = request.POST['comment_text'] new_comment.comment_post = post new_comment.save() return HttpResponse("", content_type='application/json')
def handle_response(response, user, errorlogger): if 'data' in response: new_data = response['data'] script = user.userprofile.script script.data = new_data script.save() if 'posts' in response: for post_info in response['posts']: if 'content' not in post_info: errorlogger.log_error("Error: Invalid post: "+json.dumps(post_info)) break content = post_info['content'] post_form = PostForm({'content':content}) if not post_form.is_valid(): errorlogger.log_error("Error: Invalid post: "+json.dumps(post_info)) break new_post = Post(content=post_form.cleaned_data['content'], user=user) new_post.save() if 'comments' in response: for comment_info in response['comments']: if ('content' not in comment_info) or ('post_id' not in comment_info): errorlogger.log_error("Error: Invalid comment: "+json.dumps(comment_info)) break post_id = comment_info['post_id'] content = comment_info['content'] comment_form = CommentForm({'post':post_id,'content':content}) if not comment_form.is_valid(): errorlogger.log_error("Error: Invalid comment: "+json.dumps(comment_info)) break new_comment = Comment(content=comment_form.cleaned_data['content'], post=comment_form.cleaned_data['post'], user=user) new_comment.save() if 'follow' in response: for followee_username in response['follow']: follow_form = FollowForm({'username':followee_username}) if not follow_form.is_valid(): errorlogger.log_error("Error: Invalid followee: "+str(followee_username)) break followee = User.objects.get(username=follow_form.cleaned_data['username']) user.userprofile.following.add(followee) user.userprofile.save() if 'unfollow' in response: for target in response['unfollow']: unfollow_form = UnfollowForm({'username':target}) if not unfollow_form.is_valid(): errorlogger.log_error("Error: Invalid unfollow target: "+str(target)) break target_user = User.objects.get(username=unfollow_form.cleaned_data['username']) user.userprofile.following.remove(target_user) user.userprofile.save() if 'log' in response: for entry in response['log']: errorlogger.log_error(str(entry))
def add_comment(request, post_id): if request.method != 'POST': raise Http404 if not 'comment_text' in request.POST or not request.POST['comment_text']: message = 'You must enter an comment to add.' json_error = '{ "error": "' + message + '" }' return HttpResponse(json_error, content_type='application/json') # print("Comment: ", request.POST.get('comment_text')) # print("post_id: ", request.POST.get('post_ref')) new_comment = Comment() new_comment.comment_text = request.POST.get('comment_text') new_comment.post_ref = request.POST.get('post_ref') new_comment.post = Post.objects.get(id=post_id) new_comment.comment_profile = Profile.objects.get(user=request.user) new_comment.user_name = new_comment.comment_profile.user.username new_comment.user_id = new_comment.comment_profile.user.id new_comment.comment_date_time = parse_datetime(timezone.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-4]) # new_comment.comment_date_time = parse_datetime(datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-4]) print("NEW_COMMENT_DATE:", new_comment.comment_date_time) new_comment.comment_user = request.user new_comment.save() response_text = serializers.serialize('json', Comment.objects.filter(id=new_comment.id)) return HttpResponse(response_text, content_type='application/json')