ticket_list_list.py 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. from django.contrib import messages
  2. from django.contrib.auth.mixins import LoginRequiredMixin
  3. from django.db.models import Count, Sum, Q
  4. from django.views.generic import ListView
  5. from tickets.models import TicketList
  6. from tickets.forms import TicketListForm
  7. class TicketListView(LoginRequiredMixin, ListView):
  8. model = TicketList
  9. template_name = "tickets/ticket_list_list.html"
  10. context_object_name = "ticket_lists"
  11. def get_queryset(self):
  12. user = self.request.user
  13. user_groups_ids = user.groups.all().values_list("pk", flat=True)
  14. ticket_lists = TicketList.objects.select_related("group").order_by("group__name", "name")
  15. if user_groups_ids:
  16. ticket_lists = ticket_lists.filter(group__id__in=user_groups_ids)
  17. else:
  18. messages.warning(self.request, "You do not yet belong to any groups. Ask your administrator to add you to one.")
  19. ticket_lists = TicketList.objects.none()
  20. return ticket_lists
  21. def get_context_data(self, **kwargs):
  22. context = super().get_context_data(**kwargs)
  23. context["form"] = TicketListForm(self.request.user)
  24. return context