search.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from django.contrib.auth.decorators import login_required
  2. from django.db.models import Q, F
  3. from django.shortcuts import render
  4. from django.db.models.functions import Concat
  5. from tickets.models import Ticket
  6. @login_required
  7. def search(request):
  8. found_tickets = None
  9. query_string = ""
  10. if request.GET:
  11. if ("search" in request.GET) and request.GET["search"].strip():
  12. query_string = request.GET["search"]
  13. found_tickets = Ticket.objects.all()
  14. if not request.user.is_superuser:
  15. found_tickets = found_tickets.filter(
  16. Q(ticket_list__group__in=request.user.groups.all())
  17. | Q(assigned_to=request.user)
  18. )
  19. found_tickets = found_tickets.annotate(created_by_username=F("created_by__username"))
  20. found_tickets = found_tickets.annotate(assigned_to_username=F("assigned_to__username"))
  21. found_tickets = found_tickets.filter(
  22. Q(title__icontains=query_string)
  23. | Q(ticket_list__name__icontains=query_string)
  24. | Q(note__icontains=query_string)
  25. | Q(created_by_username__icontains=query_string)
  26. | Q(assigned_to_username__icontains=query_string)
  27. | Q(status__icontains=query_string)
  28. )
  29. found_tickets = found_tickets.select_related("ticket_list")
  30. found_tickets = found_tickets.order_by("-priority")
  31. context = {
  32. "query_string": query_string,
  33. "found_tickets": found_tickets
  34. }
  35. return render(request, "tickets/search.html", context)