def post(self, request, *args, **kwargs): self.comment_obj = get_object_or_404(Comment, id=request.POST.get("commentid")) if request.user == self.comment_obj.commented_by: form = AccountCommentForm(request.POST, instance=self.comment_obj) if form.is_valid(): return self.form_valid(form) return self.form_invalid(form) data = {"error": "You don't have permission to edit this comment."} return JsonResponse(data)
def edit_comment(request): if request.method == "POST": comment = request.POST.get('comment') comment_id = request.POST.get("commentid") com = get_object_or_404(Comment, id=comment_id) form = AccountCommentForm(request.POST) if request.user == com.commented_by: if form.is_valid(): com.comment = comment com.save() data = {"comment": com.comment, "commentid": comment_id} return JsonResponse(data) else: return JsonResponse({"error": form['comment'].errors}) else: return JsonResponse( {"error": "You dont have authentication to edit"}) else: return render(request, "404.html")
def add_comment(request): if request.method == 'POST': account = get_object_or_404(Account, id=request.POST.get('accountid')) if request.user in account.assigned_to.all( ) or request.user == account.created_by: form = AccountCommentForm(request.POST) if form.is_valid(): account_comment = form.save(commit=False) account_comment.comment = request.POST.get('comment') account_comment.commented_by = request.user account_comment.account = account account_comment.save() data = { "comment_id": account_comment.id, "comment": account_comment.comment, "commented_on": account_comment.commented_on, "commented_by": account_comment.commented_by.email } return JsonResponse(data) else: return JsonResponse({"error": form['comment'].errors}) else: data = {'error': "You Dont Have permissions to Comment"} return JsonResponse(data)