from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.db.models import Count, Sum, Q from django.views.generic import ListView from tickets.models import TicketList from tickets.forms import TicketListForm class TicketListView(LoginRequiredMixin, ListView): model = TicketList template_name = "tickets/ticket_list_list.html" context_object_name = "ticket_lists" def get_queryset(self): user = self.request.user user_groups_ids = user.groups.all().values_list("pk", flat=True) ticket_lists = TicketList.objects.select_related("group").order_by("group__name", "name") if not user.is_superuser: if user_groups_ids: ticket_lists = ticket_lists.filter(group__id__in=user_groups_ids) else: messages.warning(self.request, "You do not yet belong to any groups. Ask your administrator to add you to one.") ticket_lists = TicketList.objects.none() return ticket_lists def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["form"] = TicketListForm(self.request.user) return context