def create(validated_data): comment = Comment( name = validated_data['name'], email = validated_data['email'], likes =0, text = validated_data['text'], post = validated_data['post'] ) comment.save() return comment
def new_comment(request, id_post): if not request.user.is_authenticated(): return HttpResponse(json.dumps({"error": "You need be logged to comment."}), mimetype="application/json") if request.is_ajax(): if request.method == 'POST': post = Post.objects.get_or_none(pk=id_post) # get_or_none method is on apps.blog.models into GenericManager class if post: c = Comment(post=post, user=request.user, comment=request.POST.get("comment")) c.save() response = {"sent": True} else: response = {"error": "Error, refresh the page"} else: response = {"error": "Error, the http method is not POST"} else: response = {"error": "You are not allowed to be here"} return HttpResponse(json.dumps(response), mimetype="application/json")
def saveComment(request): if request.method == 'POST' and request.user is not None: commentObject = Comment() commentObject.user = request.user commentObject.post_id = request.POST['post_id'] commentObject.comment = request.POST['comment'] commentObject.replied_on = request.POST['replyOn'] commentObject.save() response = { 'message': "Thank you for your valuable comment", 'code': 200, 'data': {} } return HttpResponse(json.dumps(response), content_type="application/json") else: response = {'message': getApiMsg(201), 'code': 201, 'data': {}} return HttpResponse(json.dumps(response), content_type="application/json")
def blog_detail(request, pk): post = Post.objects.get(pk=pk) form = CommentForm() if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = Comment(author=form.cleaned_data["author"], body=form.cleaned_data["body"], post=post) comment.save() comments = Comment.objects.filter(post=post) context = { "post": post, "comments": comments, "form": form, } return render(request, "blog_detail.html", context)