コード例 #1
0
ファイル: views.py プロジェクト: q-cheng/Social-Network
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, 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')
コード例 #3
0
ファイル: views.py プロジェクト: sunnyp06/webapps-hw7
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')
コード例 #4
0
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')