예제 #1
0
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')
예제 #2
0
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')