ticket_list_list.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  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 not user.is_superuser:
  16. if user_groups_ids:
  17. ticket_lists = ticket_lists.filter(group__id__in=user_groups_ids)
  18. else:
  19. messages.warning(self.request, "You do not yet belong to any groups. Ask your administrator to add you to one.")
  20. ticket_lists = TicketList.objects.none()
  21. return ticket_lists
  22. def get_context_data(self, **kwargs):
  23. context = super().get_context_data(**kwargs)
  24. context["form"] = TicketListForm(self.request.user)
  25. return context