emile 10 months ago
parent 7eb89e5418
commit deccc305c1

Binary file not shown.

@ -5,24 +5,6 @@ from django.template.loader import render_to_string
from asgiref.sync import async_to_sync
import threading
def get_last_seen(user):
connection = Connection.objects.filter(user=user).last()
if not connection.exists():
return "Not seen yet"
if connection.online:
return "Online"
last_seen_time = connection.last_seen
now = timezone.now()
time_diff = now - last_seen_time
if time_diff < timedelta(days=1):
if last_seen_time.date() == now.date():
return f"last seen today at {last_seen_time.strftime('%I:%M %p')}"
else:
return f"last seen yesterday at {last_seen_time.strftime('%I:%M %p')}"
else:
return f"last seen on {last_seen_time.strftime('%b %d at %I:%M %p')}"
class OnlineUserConsumer(WebsocketConsumer):
@ -59,11 +41,15 @@ class OnlineUserConsumer(WebsocketConsumer):
self.modify_online_user()
def modify_online_user(self):
connections = Connection.objects.filter(online=True)
online_users_ids = [connection.user.id for connection in connections]
connections = Connection.objects.all()
online_connections = connections.filter(online=True)
offline_connections = connections.filter(online=False, last_seen__isnull=False).order_by('-last_seen')[:5]
sorted_connections = list(online_connections) + list(offline_connections)
online_users_ids = [connection.user.id for connection in online_connections]
online_users_ids = [connection.user.id for connection in online_connections]
customer_connections = []
staff_connections = []
for connection in connections:
for connection in sorted_connections:
if hasattr(connection.user, 'customerprofile'):
customer_connections.append(connection)
elif hasattr(connection.user, 'staffprofile'):

@ -0,0 +1,29 @@
# Generated by Django 4.2.5 on 2024-07-09 06:06
import django.contrib.auth.models
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
('osinacore', '0093_connection_disconnected'),
]
operations = [
migrations.CreateModel(
name='CustomUser',
fields=[
],
options={
'proxy': True,
'indexes': [],
'constraints': [],
},
bases=('auth.user',),
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]

@ -0,0 +1,16 @@
# Generated by Django 4.2.5 on 2024-07-09 06:07
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('osinacore', '0094_customuser'),
]
operations = [
migrations.DeleteModel(
name='CustomUser',
),
]

@ -10,6 +10,8 @@ from datetime import timedelta
# Create your models here.
class Reference(models.Model):
name = models.CharField(max_length=50)
date = models.DateField()
@ -53,6 +55,25 @@ class CustomerProfile(models.Model):
new_id = str(int(max_id[-4:]) + 1).zfill(4) if max_id else '0001' # If no existing records, start with '0001'
self.customer_id = current_year + new_id # Add 'p' prefix
super(CustomerProfile, self).save(*args, **kwargs)
@property
def get_last_seen(self):
connection = Connection.objects.filter(user=self.user).last()
if not connection:
return "Not seen yet"
if connection.online:
return "Online"
last_seen_time = connection.last_seen
now = timezone.now()
time_diff = now - last_seen_time
if time_diff < timedelta(days=1):
if last_seen_time.date() == now.date():
return f"last seen today at {last_seen_time.strftime('%I:%M %p')}"
else:
return f"last seen yesterday at {last_seen_time.strftime('%I:%M %p')}"
else:
return f"last seen on {last_seen_time.strftime('%b %d at %I:%M %p')}"
@ -124,6 +145,24 @@ class StaffProfile(models.Model):
new_id = str(int(max_id[-4:]) + 1).zfill(4) if max_id else '0001' # If no existing records, start with '0001'
self.staff_id = 'O' + current_year + new_id # Add 'p' prefix
super(StaffProfile, self).save(*args, **kwargs)
@property
def get_last_seen(self):
connection = Connection.objects.filter(user=self.user).last()
if not connection:
return "Not seen yet"
if connection.online:
return "Online"
last_seen_time = connection.last_seen
now = timezone.now()
time_diff = now - last_seen_time
if time_diff < timedelta(days=1):
if last_seen_time.date() == now.date():
return f"last seen today at {last_seen_time.strftime('%I:%M %p')}"
else:
return f"last seen yesterday at {last_seen_time.strftime('%I:%M %p')}"
else:
return f"last seen on {last_seen_time.strftime('%b %d at %I:%M %p')}"
@ -394,3 +433,4 @@ class Connection(models.Model):
last_seen = models.DateTimeField(null=True, blank=True)
disconnected = models.BooleanField(default=False)

@ -26,7 +26,12 @@
<div class="flex flex-col">
<p class="text-secondosiblue text-sm">{{staff_connection.user.first_name}}
{{staff_connection.user.last_name}}</p>
<p class="text-gray-500 text-sm">Online</p>
{% if staff_connection.online %}
<p class="text-green-700 text-sm">Online</p>
{% else %}
<p class="text-gray-500 text-sm">{% if staff_connection.user.staffprofile %} {{staff_connection.user.staffprofile.get_last_seen}} {% else %} {{staff_connection.user.customerprofile.get_last_seen}} {% endif %}</p>
{% endif %}
</div>
</div>
{% endfor %}
@ -51,8 +56,11 @@
<div class="flex flex-col">
<p class="text-secondosiblue text-sm">{{recent_logged_in_customer.first_name}}
{{recent_logged_in_customer.last_name}}</p>
<p class="text-gray-500 text-sm">
{{recent_logged_in_customer.last_login|date:"g:i A"}}</p>
{% if staff_connection.online %}
<p class="text-green-700 text-sm">Online</p>
{% else %}
<p class="text-gray-500 text-sm">{% if staff_connection.user.staffprofile %} {{staff_connection.user.staffprofile.get_last_seen}} {% else %} {{staff_connection.user.customerprofile.get_last_seen}} {% endif %}</p>
{% endif %}
</div>
</div>
{% endfor %}

@ -754,7 +754,7 @@ def get_latest_activities(request):
}
recent_activities = render_to_string('recent-activities.html', response_data)
recent_activities = render_to_string('details_pages/partials/recent-activities.html', response_data)
return HttpResponse(recent_activities)

Loading…
Cancel
Save