|
|
|
@ -3,6 +3,7 @@ from django.contrib.auth.models import AnonymousUser
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
from django.db.models import Max, F
|
|
|
|
|
from support.models import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def calculate_time_ago(status):
|
|
|
|
@ -22,11 +23,7 @@ def calculate_time_ago(status):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def utilities(request):
|
|
|
|
|
latest_connections = Connection.objects.filter(
|
|
|
|
|
user__staffprofile__isnull=False
|
|
|
|
|
).values('user').annotate(
|
|
|
|
|
latest_connection=Max('date')
|
|
|
|
|
)
|
|
|
|
|
latest_connections = Connection.objects.filter(user__staffprofile__isnull=False).values('user').annotate(latest_connection=Max('date'))
|
|
|
|
|
online_staff_profiles = []
|
|
|
|
|
for connection in latest_connections:
|
|
|
|
|
user_id = connection['user']
|
|
|
|
@ -34,32 +31,87 @@ def utilities(request):
|
|
|
|
|
last_connection = Connection.objects.filter(user_id=user_id, date=latest_connection).first()
|
|
|
|
|
if last_connection.status == 'Online':
|
|
|
|
|
online_staff_profiles.append(last_connection.user.staffprofile)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
notes = None
|
|
|
|
|
recent_note = None
|
|
|
|
|
user_offline=None
|
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
|
open_task_count = 0
|
|
|
|
|
working_on_task_count = 0
|
|
|
|
|
open_tickets = None
|
|
|
|
|
closed_tickets = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if request.user.is_authenticated and StaffProfile.objects.filter(user=request.user):
|
|
|
|
|
notes = Note.objects.filter(user=request.user).order_by('-date')[:6]
|
|
|
|
|
recent_note = Note.objects.filter(user=request.user).last()
|
|
|
|
|
last_user_activity = Connection.objects.filter(user=request.user).last()
|
|
|
|
|
if last_user_activity and last_user_activity.status == 'Offline':
|
|
|
|
|
# Send 'user_offline' object to your context
|
|
|
|
|
user_offline = True
|
|
|
|
|
else:
|
|
|
|
|
user_offline = False
|
|
|
|
|
|
|
|
|
|
if request.user.is_authenticated and request.user.is_superuser:
|
|
|
|
|
|
|
|
|
|
if request.user.is_superuser:
|
|
|
|
|
open_task_count = Task.objects.filter(status='Open').count()
|
|
|
|
|
working_on_task_count = Task.objects.filter(status='Working On').count()
|
|
|
|
|
|
|
|
|
|
elif request.user.is_authenticated and StaffProfile.objects.filter(user=request.user):
|
|
|
|
|
|
|
|
|
|
all_tickets = Ticket.objects.all()
|
|
|
|
|
all_tickets_with_update_date = all_tickets.annotate(latest_update_date=Max('ticketupdate__date_added'))
|
|
|
|
|
all_tickets_ordered = all_tickets_with_update_date.order_by('-latest_update_date')
|
|
|
|
|
open_tickets = []
|
|
|
|
|
closed_tickets = []
|
|
|
|
|
for ticket in all_tickets_ordered:
|
|
|
|
|
last_status = ticket.ticketstatus_set.last()
|
|
|
|
|
if last_status:
|
|
|
|
|
last_status = last_status.status
|
|
|
|
|
if last_status == 'Closed':
|
|
|
|
|
closed_tickets.append(ticket)
|
|
|
|
|
else:
|
|
|
|
|
open_tickets.append(ticket)
|
|
|
|
|
else:
|
|
|
|
|
# If no status is found, assume it's open
|
|
|
|
|
open_tickets.append(ticket)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
open_task_count = Task.objects.filter(assigned_to=request.user.staffprofile, status='Open').count()
|
|
|
|
|
working_on_task_count = Task.objects.filter(assigned_to=request.user.staffprofile, status='Working On').count()
|
|
|
|
|
all_tickets = Ticket.objects.all()
|
|
|
|
|
filtered_tickets = []
|
|
|
|
|
for ticket in all_tickets:
|
|
|
|
|
staff_profiles = ticket.get_all_ticket_staff()
|
|
|
|
|
if any(staff.user == request.user for staff in staff_profiles):
|
|
|
|
|
filtered_tickets.append(ticket)
|
|
|
|
|
|
|
|
|
|
all_tickets = Ticket.objects.filter(id__in=[ticket.id for ticket in filtered_tickets]).annotate(
|
|
|
|
|
latest_update_date=Max('ticketupdate__date_added'))
|
|
|
|
|
all_tickets_ordered = all_tickets_with_update_date.order_by('-latest_update_date')
|
|
|
|
|
|
|
|
|
|
open_tickets = []
|
|
|
|
|
closed_tickets = []
|
|
|
|
|
for ticket in all_tickets_ordered:
|
|
|
|
|
last_status = ticket.ticketstatus_set.last()
|
|
|
|
|
if last_status:
|
|
|
|
|
last_status = last_status.status
|
|
|
|
|
if last_status == 'Closed':
|
|
|
|
|
closed_tickets.append(ticket)
|
|
|
|
|
else:
|
|
|
|
|
open_tickets.append(ticket)
|
|
|
|
|
else:
|
|
|
|
|
# If no status is found, assume it's open
|
|
|
|
|
open_tickets.append(ticket)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
# Handle the case when the user is not logged in
|
|
|
|
|
open_task_count = 0
|
|
|
|
|
working_on_task_count = 0
|
|
|
|
|
for ticket in open_tickets:
|
|
|
|
|
unread_updates_count = 0
|
|
|
|
|
for ticket_update in ticket.ticketupdate_set.exclude(added_by=request.user):
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
total_tasks = open_task_count + working_on_task_count
|
|
|
|
@ -69,19 +121,18 @@ def utilities(request):
|
|
|
|
|
today = datetime.now().date()
|
|
|
|
|
# Fetch the latest statuses from the last 24 hours
|
|
|
|
|
latest_statuses = Status.objects.filter(date=today).order_by('-id')
|
|
|
|
|
|
|
|
|
|
# Calculate time ago for each status and store it in a dictionary
|
|
|
|
|
latest_statuses_time_ago = [{'status': status, 'time_ago': calculate_time_ago(status)} for status in latest_statuses]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
recent_logged_in_staffs = User.objects.filter(
|
|
|
|
|
staffprofile__isnull=False, # Ensure there's a StaffProfile associated
|
|
|
|
|
last_login__isnull=False # Ensure they have logged in at least once
|
|
|
|
|
staffprofile__isnull=False,
|
|
|
|
|
last_login__isnull=False
|
|
|
|
|
).order_by('-last_login')[:8]
|
|
|
|
|
|
|
|
|
|
recent_logged_in_customers = User.objects.filter(
|
|
|
|
|
customerprofile__isnull=False, # Ensure there's a CustomerProfile associated
|
|
|
|
|
last_login__isnull=False # Ensure they have logged in at least once
|
|
|
|
|
customerprofile__isnull=False,
|
|
|
|
|
last_login__isnull=False
|
|
|
|
|
).order_by('-last_login')[:8]
|
|
|
|
|
|
|
|
|
|
return {'total_tasks': total_tasks,
|
|
|
|
@ -93,6 +144,8 @@ def utilities(request):
|
|
|
|
|
'user_offline' : user_offline,
|
|
|
|
|
'recent_logged_in_staffs' : recent_logged_in_staffs,
|
|
|
|
|
'recent_logged_in_customers' : recent_logged_in_customers,
|
|
|
|
|
'open_tickets': open_tickets,
|
|
|
|
|
'closed_tickets': closed_tickets
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|