add is_online property to Worker and WorkerStatus models for online status tracking

This commit is contained in:
Eduardo Silva
2026-01-14 09:08:50 -03:00
parent 886a5f5b80
commit 1b09eca24e
2 changed files with 21 additions and 2 deletions

View File

@@ -54,6 +54,14 @@ class Worker(models.Model):
created = models.DateTimeField(auto_now_add=True)
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
@property
def is_online(self):
try:
return self.workerstatus.is_online
except WorkerStatus.DoesNotExist:
return False
class WorkerStatus(models.Model):
worker = models.OneToOneField(Worker, on_delete=models.CASCADE)
@@ -70,3 +78,11 @@ class WorkerStatus(models.Model):
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
@property
def is_online(self):
from django.utils import timezone
from datetime import timedelta
if not self.last_seen:
return False
return self.last_seen >= timezone.now() - timedelta(minutes=10)

View File

@@ -54,11 +54,14 @@
<span class="text-muted">{% trans 'Not set' %}</span>
{% endif %}
</td>
<td style="width: 1%; white-space: nowrap;">
<td style="width: 1%; white-space: nowrap;" class="{% if not worker.is_online %}text-danger blink{% endif %}">
{% if worker.workerstatus %}
{{ worker.workerstatus.last_seen|date:"M d, H:i" }}
{% else %}
<span class="text-muted">{% trans 'Never' %}</span>
<span class="{% if not worker.is_online %}text-danger blink{% endif %}">{% trans 'Never' %}</span>
{% endif %}
{% if not worker.is_online %}
<i class="fas fa-exclamation-triangle text-danger blink" title="{% trans 'Last seen more than 10 minutes ago or never seen.' %}"></i>
{% endif %}
</td>
<td style="width: 1%; white-space: nowrap;">