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 = LeadCommentForm(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") lead_comment = get_object_or_404(Comment, id=comment_id) form = LeadCommentForm(request.POST) if request.user == lead_comment.commented_by: if form.is_valid(): lead_comment.comment = comment lead_comment.save() data = {"comment": lead_comment.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': lead = get_object_or_404(Lead, id=request.POST.get('leadid')) if request.user in lead.assigned_to.all() or request.user == lead.created_by: form = LeadCommentForm(request.POST) if form.is_valid(): lead_comment = form.save(commit=False) lead_comment.comment = request.POST.get('comment') lead_comment.commented_by = request.user lead_comment.lead = lead lead_comment.save() data = { "comment_id": lead_comment.id, "comment": lead_comment.comment, "commented_on": lead_comment.commented_on, "commented_by": lead_comment.commented_by.user.email } return JsonResponse(data) else: return JsonResponse({"error": form['comment'].errors}) else: data = {'error': "You Dont Have permissions to Comment"} return JsonResponse(data)