Example #1
0
def comment_list(request):
    if request.method == 'GET':
        comments = Comment.objects.all()
        for comment in comments:
            comment.to_json()
        return JsonResponse(Comment.objects.first().to_json(), safe=False)
    elif request.method == 'POST':
        data = json.loads(request.body)
        comment = Comment(
            name=data['name'], 
            created=data['created'], 
            due_on=data['due_on'], 
            owner=data['due_on'], 
            mark=data['mark'], 
            list_id=data['list_id']
        )
        comment.save()
        return JsonResponse(comment.to_json())
Example #2
0
def comment(request, username=None, post_id=None):
    post = Post.objects.filter(Q(id=post_id))
    user = Users.objects.filter(Q(username=username))

    if not post.exists() or not user.exists():
        return JsonResponse(
            {"error": "Cannot comment for unknown user or post"}, status=400)
    user = user.first()
    post = post.first()

    data = json.loads(request.body)
    for key in ['comment']:
        if data.get(key) is None:
            return JsonResponse({"error": "%s is required" % key}, status=400)

    comm = Comment(comment=data['comment'], user=user, post=post)
    comm.save()

    return JsonResponse({"result": comm.to_json(keys=['user', 'post'])})