search.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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.filter(
  14. Q(ticket_list__group__in=request.user.groups.all())
  15. | Q(assigned_to=request.user)
  16. )
  17. found_tickets = found_tickets.annotate(created_by_username=F("created_by__username"))
  18. found_tickets = found_tickets.annotate(assigned_to_username=F("assigned_to__username"))
  19. found_tickets = found_tickets.filter(
  20. Q(title__icontains=query_string)
  21. | Q(ticket_list__name__icontains=query_string)
  22. | Q(note__icontains=query_string)
  23. | Q(created_by_username__icontains=query_string)
  24. | Q(assigned_to_username__icontains=query_string)
  25. | Q(status__icontains=query_string)
  26. )
  27. found_tickets = found_tickets.select_related("ticket_list")
  28. found_tickets = found_tickets.order_by("-priority")
  29. context = {
  30. "query_string": query_string,
  31. "found_tickets": found_tickets
  32. }
  33. return render(request, "tickets/search.html", context)