def add_comment(request): if request.method == 'POST': opportunity = get_object_or_404(Opportunity, id=request.POST.get('opportunityid')) if request.user in opportunity.assigned_to.all( ) or request.user == opportunity.created_by: form = OpportunityCommentForm(request.POST) if form.is_valid(): opportunity_comment = form.save(commit=False) opportunity_comment.comment = request.POST.get('comment') opportunity_comment.commented_by = request.user opportunity_comment.opportunity = opportunity opportunity_comment.save() data = { "comment_id": opportunity_comment.id, "comment": opportunity_comment.comment, "commented_on": opportunity_comment.commented_on, "commented_by": opportunity_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)
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 = OpportunityCommentForm(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") comment_object = get_object_or_404(Comment, id=comment_id) form = OpportunityCommentForm(request.POST) if request.user == comment_object.commented_by: if form.is_valid(): comment_object.comment = comment comment_object.save() data = { "comment": comment_object.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")