ticket_detail.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import bleach
  2. import os
  3. from django.contrib import messages
  4. from django.contrib.auth.mixins import LoginRequiredMixin
  5. from django.shortcuts import redirect
  6. from django.db.models import F
  7. from django.views.generic import DetailView
  8. from tickets.forms import TicketForm
  9. from tickets.models import Attachment, Comment, Ticket
  10. from tickets.utils import UserCanReadTicketMixin
  11. class TicketDetailView(LoginRequiredMixin, UserCanReadTicketMixin, DetailView):
  12. model = Ticket
  13. template_name = 'tickets/ticket_detail.html'
  14. context_object_name = 'ticket'
  15. def get_queryset(self):
  16. queryset = super().get_queryset()
  17. return queryset.select_related("created_by", "assigned_to")
  18. def get_context_data(self, **kwargs):
  19. context = super().get_context_data(**kwargs)
  20. context['form'] = TicketForm(self.request.user, instance=self.object)
  21. context['comment_list'] = Comment.objects.filter(ticket=self.object.pk).order_by("-date").annotate(
  22. author_username=F("author__username"), author_email=F("author__email")
  23. )
  24. context['attachments'] = Attachment.objects.filter(ticket=self.object.pk).select_related("added_by")
  25. # List with statuses codes & their names
  26. available_statuses_with_names = []
  27. available_statuses = self.object.get_available_statuses()
  28. if available_statuses:
  29. for status_code in available_statuses:
  30. status_name = Ticket.TICKET_STATUSES_NAMES.get(status_code)
  31. available_statuses_with_names.append([status_code, status_name])
  32. context["available_statuses"] = available_statuses_with_names
  33. return context
  34. def post(self, request, *args, **kwargs):
  35. ticket = self.get_object()
  36. if request.POST.get("ticket-status"):
  37. status = request.POST.get("ticket-status")
  38. if int(status) in ticket.get_available_statuses():
  39. ticket.status = int(status)
  40. ticket.save()
  41. messages.success(request, f"Status successfully changed.")
  42. else:
  43. messages.error(request, f"Status change error.")
  44. if request.POST.get("comment-body"):
  45. Comment.objects.create(
  46. author=request.user,
  47. ticket=ticket,
  48. body=bleach.clean(request.POST.get("comment-body"), strip=True)
  49. )
  50. messages.success(request, "Comment posted. Notification email sent to thread participants.")
  51. if request.FILES.get("attachment_file_input"):
  52. uploaded_file = request.FILES.get("attachment_file_input")
  53. name, extension = os.path.splitext(uploaded_file.name)
  54. if uploaded_file.size > 5000000:
  55. messages.error(request, f"File exceeds maximum attachment size.")
  56. elif extension not in [".jpg", ".gif", ".png", ".csv", ".pdf", ".zip", ".txt"]:
  57. messages.error(request, f"This site does not allow upload of {extension} files.")
  58. else:
  59. Attachment.objects.create(ticket=ticket, added_by=request.user, file=uploaded_file)
  60. messages.success(request, f"File attached successfully.")
  61. return redirect("tickets:ticket_detail", pk=ticket.pk)