def post(self, request, *args, **kwargs): commentForm = CommentForm(request.POST) if commentForm.is_valid(): commentForm.save(request, Ticket.objects.get(id=kwargs['ticket_id'])) return self.get(request, kwargs) self.get(request, *args, **kwargs) context = self.get_context_data(**kwargs) #check for error on empty message context['commentForm'] = commentForm return render(request, self.template_name, context)
def edit_comments(request,id): comments = get_object_or_404(Comment_form,id=id) if request.method == "POST": form = CommentForm(request.POST, instance=comments) if form.is_valid(): form.save() return redirect(index) else: form = CommentForm(instance=comments) return render(request,"edit_comments.html",{'form':form})
def add_comment(request, pk, uid): """ Create a view to allow adding comments to a ticket """ ticket = get_object_or_404(Ticket, pk=pk) if pk else None if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): form.save() messages.success(request, "Comment added") return redirect('show_comments', pk) else: form = CommentForm(initial={'ticket_id': pk, 'user_name': uid}) return render(request, "commentform.html", {'ticket': ticket, 'form': form})
def tickets_view_one(request, pk): """ View a Single Ticket """ # get ticket details ticket = get_object_or_404(Ticket, pk=pk) # increase number of views by 1 ticket.views += 1 ticket.save() # filter comments on specific ticket comments = Comment.objects.filter(ticket_id=ticket.pk) # filter upvotes on specific ticket by user ID upvotes = Upvote.objects.filter(ticket_id=ticket.pk).values("user_id") voters = [vote["user_id"] for vote in upvotes] donors = User.objects.filter(id__in=voters) # render drop-down list for superusers to update status ticket_status_list = TicketStatus.objects.all() tkt_status = ticket.ticket_status.id # POST methods if request.method == "POST": comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment_form.instance.commenter = request.user comment_form.instance.ticket = ticket comment_form.save() # remove two ticket views to avoid duplicates on POST ticket.views -= 2 ticket.save() messages.success(request, f"Comment successfully added!") return redirect(tickets_view_one, ticket.pk) else: comment_form = CommentForm() donation_form = DonationForm() context = { "comment_form": comment_form, "comments": comments, "donation_form": donation_form, "donors": donors, "publishable": settings.STRIPE_PUBLISHABLE, "ticket": ticket, "ticket_status_list": ticket_status_list, "tkt_status": tkt_status, "voters": voters, } return render(request, "tickets_view_one.html", context)
def post(self, request): ticketForm = TicketForm(request.POST, request=request) commentForm = CommentForm(request.POST) if ticketForm.is_valid() and commentForm.is_valid(): ticket = ticketForm.save(request) comment = commentForm.save(request, ticket) return redirect('ticket details', ticket_id=ticket.id) context = self.get_context_data() context['ticketForm'] = ticketForm context['commentForm'] = commentForm return render(request, 'tickets/add-ticket.html', context)