from django.contrib import messages from django.contrib.auth.decorators import login_required, user_passes_test from django.http import HttpResponse from django.shortcuts import render from django.db.models import Count from django.contrib.auth.models import Group, Permission from django.contrib.contenttypes.models import ContentType from tickets.forms import SearchForm from tickets.models import * from tickets.utils import staff_check from tickets.admin_utils import add_default_tickets_list @login_required @user_passes_test(staff_check) def list_lists(request) -> HttpResponse: searchform = SearchForm(auto_id=False) add_default_tickets_list(request) # Make sure user belongs to at least one group. if not request.user.groups.all().exists(): messages.warning( request, "You do not yet belong to any groups. Ask your administrator to add you to one.", ) # Get info about Lists lists = TaskList.objects.select_related("group").order_by("group__name", "name") if not request.user.is_superuser: lists = lists.filter(group__in=request.user.groups.all()) lists = lists.annotate(ticket_count=Count("task")) # Get info about Tickets tickets = Task.objects.filter(task_list__in=lists) context = { "lists": lists, "ticket_count": len(tickets), "list_count": len(lists), "searchform": searchform } return render(request, "tickets/list_lists.html", context)