diff --git a/osinaweb/db.sqlite3 b/osinaweb/db.sqlite3 index f316631d..5686ec56 100644 Binary files a/osinaweb/db.sqlite3 and b/osinaweb/db.sqlite3 differ diff --git a/osinaweb/osinacore/__pycache__/consumers.cpython-310.pyc b/osinaweb/osinacore/__pycache__/consumers.cpython-310.pyc index eae2be93..a2c6de55 100644 Binary files a/osinaweb/osinacore/__pycache__/consumers.cpython-310.pyc and b/osinaweb/osinacore/__pycache__/consumers.cpython-310.pyc differ diff --git a/osinaweb/osinacore/__pycache__/models.cpython-310.pyc b/osinaweb/osinacore/__pycache__/models.cpython-310.pyc index 0856a296..011ae3a3 100644 Binary files a/osinaweb/osinacore/__pycache__/models.cpython-310.pyc and b/osinaweb/osinacore/__pycache__/models.cpython-310.pyc differ diff --git a/osinaweb/osinacore/__pycache__/views.cpython-310.pyc b/osinaweb/osinacore/__pycache__/views.cpython-310.pyc index 94457723..7630df43 100644 Binary files a/osinaweb/osinacore/__pycache__/views.cpython-310.pyc and b/osinaweb/osinacore/__pycache__/views.cpython-310.pyc differ diff --git a/osinaweb/osinacore/consumers.py b/osinaweb/osinacore/consumers.py index 2afe06d8..394d4bd0 100644 --- a/osinaweb/osinacore/consumers.py +++ b/osinaweb/osinacore/consumers.py @@ -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'): diff --git a/osinaweb/osinacore/migrations/0094_customuser.py b/osinaweb/osinacore/migrations/0094_customuser.py new file mode 100644 index 00000000..cbfce5d6 --- /dev/null +++ b/osinaweb/osinacore/migrations/0094_customuser.py @@ -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()), + ], + ), + ] diff --git a/osinaweb/osinacore/migrations/0095_delete_customuser.py b/osinaweb/osinacore/migrations/0095_delete_customuser.py new file mode 100644 index 00000000..d6f1cec3 --- /dev/null +++ b/osinaweb/osinacore/migrations/0095_delete_customuser.py @@ -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', + ), + ] diff --git a/osinaweb/osinacore/migrations/__pycache__/0094_customuser.cpython-310.pyc b/osinaweb/osinacore/migrations/__pycache__/0094_customuser.cpython-310.pyc new file mode 100644 index 00000000..c2f28d76 Binary files /dev/null and b/osinaweb/osinacore/migrations/__pycache__/0094_customuser.cpython-310.pyc differ diff --git a/osinaweb/osinacore/migrations/__pycache__/0095_delete_customuser.cpython-310.pyc b/osinaweb/osinacore/migrations/__pycache__/0095_delete_customuser.cpython-310.pyc new file mode 100644 index 00000000..659654b5 Binary files /dev/null and b/osinaweb/osinacore/migrations/__pycache__/0095_delete_customuser.cpython-310.pyc differ diff --git a/osinaweb/osinacore/models.py b/osinaweb/osinacore/models.py index 9cdf8e40..1ffa2079 100644 --- a/osinaweb/osinacore/models.py +++ b/osinaweb/osinacore/models.py @@ -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) + diff --git a/osinaweb/osinacore/templates/details_templates/partials/recently-online.html b/osinaweb/osinacore/templates/details_templates/partials/recently-online.html index 78399cab..f66b9019 100644 --- a/osinaweb/osinacore/templates/details_templates/partials/recently-online.html +++ b/osinaweb/osinacore/templates/details_templates/partials/recently-online.html @@ -26,7 +26,12 @@
{{staff_connection.user.first_name}} {{staff_connection.user.last_name}}
-Online
+ {% if staff_connection.online %} +Online
+ {% else %} +{% if staff_connection.user.staffprofile %} {{staff_connection.user.staffprofile.get_last_seen}} {% else %} {{staff_connection.user.customerprofile.get_last_seen}} {% endif %}
+ {% endif %} +{{recent_logged_in_customer.first_name}} {{recent_logged_in_customer.last_name}}
-- {{recent_logged_in_customer.last_login|date:"g:i A"}}
+ {% if staff_connection.online %} +Online
+ {% else %} +{% if staff_connection.user.staffprofile %} {{staff_connection.user.staffprofile.get_last_seen}} {% else %} {{staff_connection.user.customerprofile.get_last_seen}} {% endif %}
+ {% endif %}