ticket_list_detail.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from django.contrib import messages
  2. from django.contrib.auth.decorators import login_required
  3. from django.core.exceptions import PermissionDenied
  4. from django.shortcuts import get_object_or_404, redirect, render
  5. from django.db.models import F, Count
  6. from tickets.forms import TicketForm
  7. from tickets.models import Ticket, TicketList
  8. @login_required
  9. def ticket_list_detail(request, pk=None, my_tickets=False, assignments=False):
  10. form = None
  11. ticket_list = None
  12. if my_tickets:
  13. tickets = Ticket.objects.filter(assigned_to=request.user)
  14. elif assignments:
  15. tickets = Ticket.objects.filter(created_by=request.user)
  16. else:
  17. ticket_list = get_object_or_404(TicketList.objects.select_related('group'), id=pk)
  18. if ticket_list.group not in request.user.groups.all():
  19. raise PermissionDenied
  20. tickets = Ticket.objects.filter(ticket_list=ticket_list)
  21. form = TicketForm(
  22. request.user,
  23. initial={
  24. "ticket_list": ticket_list
  25. }
  26. )
  27. tickets = tickets.annotate(created_by_username=F("created_by__username"))
  28. tickets = tickets.annotate(assigned_to_username=F("assigned_to__username"))
  29. context = {
  30. "ticket_list": ticket_list,
  31. "tickets": tickets,
  32. "form": form,
  33. "my_tickets": my_tickets,
  34. "assignments": assignments
  35. }
  36. return render(request, "tickets/ticket_list_detail.html", context)