def post_handler(request): # JSON post body of what you post to a posts' comemnts # POST to http://service/posts/{POST_ID}/comments output = { "query": "addComment", } # change body = request.POST to body = request.body.decode('utf-8'), # because request.POST only accepts form, but here is json format. # change new_comment.comment to new_comment.content, # because that's how it defined in comment model. try: body = request.body.decode('utf-8') comment_info = loads(body) comment_info = comment_info['comment'] new_comment = Comment() new_comment.contentType = comment_info['contentType'] new_comment.content = comment_info['comment'] new_comment.published = comment_info['published'] new_comment.author = Author.objects.filter(id=comment_info['author']['id']).first() new_comment.parentPost = Post.objects.filter(id=post_id).first() new_comment.save() output['type'] = True output['message'] = "Comment added" except Exception as e: output['type'] = False output['message'] = "Comment not allowed" output['error'] = str(e) finally: return JsonResponse(output)
def post_handler(request): # JSON post body of what you post to a posts' comemnts # POST to http://service/posts/{POST_ID}/comments output = { "query": "addComment", } # checks if local host if Post.objects.filter(id=post_id).exists(): # checks visibility of the post if not check_get_perm( request, Post.objects.get(id=post_id).to_api_object()): return JsonResponse( { "query": "addComment", "success": False, "message": "Comment not allowed" }, status=403) # change body = request.POST to body = request.body.decode('utf-8'), # because request.POST only accepts form, but here is json format. # change new_comment.comment to new_comment.content, # because that's how it defined in comment model. try: body = request.body.decode('utf-8') comment_info = loads(body) comment_info = comment_info['comment'] new_comment = Comment() new_comment.contentType = comment_info['contentType'] new_comment.content = comment_info['comment'] new_comment.published = comment_info['published'] new_comment.author = url_regex.sub( '', comment_info['author']['id']).rstrip("/") new_comment.parentPost = Post.objects.filter(id=post_id).first() new_comment.save() output['success'] = True output['message'] = "Comment added" except Exception as e: output['success'] = False output['message'] = "Comment not allowed" output['error'] = str(e) finally: if output["success"]: return JsonResponse(output, status=200) else: return JsonResponse(output, status=403)