def show_tickets(request, pk): """ These view is for show the details of issues and also it keeping comment form and comments detail as well """ ticket = get_object_or_404(add_tickets_form , pk=pk) ticket.views += 1 ticket.save() print(ticket) #ticket_id = request.POST.get('ticket_id', None) #issue = add_tickets_form.objects.filter(id=ticket_id).first() if request.method == "POST": commentbox = CommentForm(request.POST) if commentbox.is_valid(): comment_data = commentbox.cleaned_data add_issue = Comment_form.objects.create( comment = comment_data['comment'], user = request.user, ticket = get_object_or_404(add_tickets_form, pk=pk) ) add_issue.save() return redirect(show_tickets,ticket.pk) print(commentbox) else: commentbox = CommentForm() comment = Comment_form.objects.all #like = like_posts.objects.all() return render(request, 'show_tickets.html', {'ticket':ticket, 'commentbox':commentbox, 'comment':comment})
def test_comment_form_valid_data(self): """ Test CommentForm with valid data. """ form = CommentForm(data={'comment': 'I am a comment.'}) self.assertTrue(form.is_valid())
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 test_comment_form_no_data(self): """ Test CommentForm with no data. """ form = CommentForm(data={}) self.assertFalse(form.is_valid()) self.assertEquals(len(form.errors), 1)
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)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['commentForm'] = CommentForm() context['ticket'] = Ticket.objects.get(id=self.kwargs['ticket_id']) context['is_support'] = self.request.user.has_perm('users.support') context['user_name'] = str(self.request.user) return context
def show_feature(request, id): feature = get_object_or_404(Feature, pk=id) user = auth.get_user(request) if request.method == 'POST': form = FeatureForm(request.POST, instance=feature) if form.is_valid(): form.save() messages.success(request, "The feature has been updated") form = FeatureForm(instance=feature) has_votes = True if user.is_authenticated and user.featurevote_set.filter( feature_id__exact=None).count() > 0 else False has_voted = True if user.is_authenticated and feature.featurevote_set.filter( voter_id__exact=user.id).count() > 0 else False is_staff = True if user.is_staff else False return render( request, 'feature_show.html', { 'feature': feature, 'form': form, 'comment_form': CommentForm(), 'user_has_voted': has_voted, 'user_has_votes': has_votes, 'user_is_staff': is_staff })
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['ticketForm'] = TicketForm() context['commentForm'] = CommentForm() context['is_support'] = is_member(self.request.user, 'supports') if (context['is_support']): context['clients'] = [] context['clients'] = get_user_model().objects.filter(groups__name='clients') context['user_name'] = str(self.request.user) return context
def get_context_data(self, **kwargs): ''' Add comment form and vote form where necessary. ''' context = super(TicketView, self).get_context_data(**kwargs) if self.request.user.is_authenticated and self.object.approved: context['comment_form'] = CommentForm() context['has_voted'] = self.object.has_voted(self.request.user) if self.object.ticket_type == 'Feature': context['vote_form'] = VoteForm() return 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 add_comment(request, id): url_name = resolve(request.path_info).url_name if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): if url_name == 'comment_bug': comment = BugComment() comment.bug_id = id elif url_name == 'comment_feature': comment = FeatureComment() comment.feature_id = id comment.commenter = auth.get_user(request) comment.content = form.cleaned_data['content'] comment.save() messages.success(request, "Thank you for your comment!") else: messages.error(request, "There was a problem... please try again!") return redirect(reverse(url_name.replace('comment', 'show'), args=(id)))
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 show_bug(request, id): bug = get_object_or_404(Bug, pk=id) user = auth.get_user(request) if request.method == 'POST': form = BugForm(request.POST, instance=bug) if form.is_valid(): form.save() messages.success(request, "The bug has been updated") form = BugForm(instance=bug) has_voted = True if user.is_authenticated and bug.bugvote_set.filter( voter_id__exact=user.id).count() > 0 else False is_staff = True if user.is_staff else False return render( request, 'bug_show.html', { 'bug': bug, 'form': form, 'comment_form': CommentForm(), 'user_has_voted': has_voted, 'user_is_staff': is_staff })
def post(self, request, *args, **kwargs): form = CommentForm(request.POST) if form.is_valid(): self.form_valid(form) return super(TicketDetailView, self).get(request, *args, **kwargs)
def test_comment_is_long_enough(self): form = CommentForm({"comment": "test comment"}) self.assertTrue(form.is_valid())
def test_comment_is_not_long_enough(self): form = CommentForm({"comment": "test"}) self.assertFalse(form.is_valid())