You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
7.6 KiB
Python
190 lines
7.6 KiB
Python
from .models import *
|
|
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):
|
|
# Convert status.date to a datetime.date object
|
|
status_date = datetime.strptime(status.date, '%Y-%m-%d').date()
|
|
timestamp = datetime.combine(status_date, datetime.strptime(status.time, '%I:%M %p').time())
|
|
time_difference = datetime.now() - timestamp
|
|
|
|
if time_difference.total_seconds() < 3600:
|
|
# If less than an hour, display in minutes
|
|
return f"{int(time_difference.total_seconds() / 60)}min ago"
|
|
else:
|
|
# Display in hours and remaining minutes
|
|
hours = int(time_difference.total_seconds() // 3600)
|
|
minutes = int((time_difference.total_seconds() % 3600) // 60)
|
|
return f"{hours}hr {minutes}min ago"
|
|
|
|
|
|
def utilities(request):
|
|
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']
|
|
latest_connection = connection['latest_connection']
|
|
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
|
|
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':
|
|
user_offline = True
|
|
else:
|
|
user_offline = False
|
|
|
|
|
|
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()
|
|
|
|
|
|
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)
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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,
|
|
last_login__isnull=False
|
|
).order_by('-last_login')[:8]
|
|
|
|
recent_logged_in_customers = User.objects.filter(
|
|
customerprofile__isnull=False,
|
|
last_login__isnull=False
|
|
).order_by('-last_login')[:8]
|
|
|
|
return {'total_tasks': total_tasks,
|
|
'latest_statuses' : latest_statuses,
|
|
'latest_statuses_time_ago': latest_statuses_time_ago,
|
|
'notes' : notes,
|
|
'recent_note' : recent_note,
|
|
'online_staff_profiles' : online_staff_profiles,
|
|
'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
|
|
}
|
|
|
|
|
|
def last_status(request):
|
|
minutes_ago = 0
|
|
hours_ago = 0
|
|
hours_minutes_ago = ""
|
|
current_date = datetime.now().strftime('%Y-%m-%d')
|
|
if request.user.is_authenticated and StaffProfile.objects.filter(user=request.user):
|
|
last_status = Status.objects.filter(staff=request.user.staffprofile).last()
|
|
if last_status:
|
|
# Convert the 'time' field to a datetime object
|
|
status_time = datetime.strptime(last_status.time, '%I:%M %p')
|
|
|
|
# Get the current time
|
|
current_time = datetime.now().time()
|
|
|
|
# Calculate the time difference
|
|
time_difference = abs(datetime.combine(datetime.today(), current_time) - datetime.combine(datetime.today(), status_time.time()))
|
|
# Get the time difference in minutes
|
|
minutes_ago = time_difference.total_seconds() / 60
|
|
minutes_ago = int(minutes_ago)
|
|
|
|
else:
|
|
# Handle the case when the user is not logged in
|
|
last_status = None
|
|
|
|
if minutes_ago > 60:
|
|
hours_ago = minutes_ago // 60 # Calculate the number of hours
|
|
remaining_minutes = minutes_ago % 60 # Calculate the remaining minutes
|
|
hours_minutes_ago = f"{hours_ago}hr {remaining_minutes}min ago"
|
|
else:
|
|
hours_minutes_ago = f"{minutes_ago}min ago"
|
|
|
|
|
|
|
|
return {'last_status' : last_status, 'current_date' : current_date, 'minutes_ago' : minutes_ago, 'hours_minutes_ago': hours_minutes_ago}
|
|
|
|
|
|
|
|
|