from billing.models import * from osinacore.models import * from customercore.models import * from django.db.models import Count, Q def utilities(request): active_subscriptions = None customer_open_tickets = None if request.user.is_authenticated and CustomerProfile.objects.filter(user=request.user): customer = request.user.customerprofile active_subscriptions = OrderItem.objects.filter(active=True, order__customer=customer) customer_open_tickets = Ticket.objects.filter( Q(status__in=['Open', 'Working On']) & Q(customer=request.user.customerprofile) ).order_by('-id') for ticket in customer_open_tickets: unread_updates_count = 0 for ticket_update in ticket.ticketupdate_set.all(): if not TicketRead.objects.filter(ticket_update=ticket_update, user=request.user, read=True).exists(): unread_updates_count += 1 ticket.unread_updates_count = unread_updates_count return {'active_subscriptions': active_subscriptions, 'customer_open_tickets': customer_open_tickets}