Skip to content

Commit

Permalink
update device detail
Browse files Browse the repository at this point in the history
  • Loading branch information
silvioheinze committed Jan 7, 2025
1 parent 220e54d commit 3d061c7
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 74 deletions.
Empty file.
8 changes: 8 additions & 0 deletions app/devices/templatetags/custom_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# devices/templatetags/custom_tags.py
from django import template

register = template.Library()

@register.filter
def get(dictionary, key):
return dictionary.get(key)
62 changes: 57 additions & 5 deletions app/devices/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
from django.views.generic.edit import UpdateView
from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin
from django.urls import reverse_lazy, reverse
from django.utils import timezone
from django.core.serializers.json import DjangoJSONEncoder
from django.core.paginator import Paginator
import json


from .models import Device, DeviceStatus
from .models import Device, DeviceStatus, DeviceLogs
from .forms import DeviceForm, DeviceNotesForm

class DeviceListView(UserPassesTestMixin, ListView):
Expand All @@ -21,18 +24,67 @@ def get_queryset(self):
return Device.objects.all().order_by('id')


class DeviceDetailView(UserPassesTestMixin, DetailView):
class DeviceDetailView(LoginRequiredMixin, UserPassesTestMixin, DetailView):
model = Device
context_object_name = 'device'
template_name = 'devices/detail.html'

def test_func(self):
# Only superusers can access this view
return self.request.user.is_authenticated and self.request.user.is_superuser

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Fetch all DeviceStatus entries related to this Device, ordered by most recent
context['device_statuses'] = DeviceStatus.objects.filter(device=self.object).order_by('-time_received')
device = self.object

# Fetch all DeviceStatus entries related to this Device, ordered by time_received ascendingly
device_status_qs = DeviceStatus.objects.filter(device=device).order_by('time_received')
context['battery_status'] = device_status_qs.exists()

if context['battery_status']:
# Prepare data for Chart.js
battery_times = [
status.time_received.strftime('%Y-%m-%d %H:%M')
for status in device_status_qs
if status.battery_soc is not None and status.battery_voltage is not None
]
battery_charges = [
min(max(status.battery_soc, 0), 100)
for status in device_status_qs
if status.battery_soc is not None and status.battery_voltage is not None
]
battery_voltages = [
round(status.battery_voltage, 2)
for status in device_status_qs
if status.battery_soc is not None and status.battery_voltage is not None
]

# Serialize data to JSON format
context['battery_times'] = json.dumps(battery_times, cls=DjangoJSONEncoder)
context['battery_charges'] = json.dumps(battery_charges, cls=DjangoJSONEncoder)
context['battery_voltages'] = json.dumps(battery_voltages, cls=DjangoJSONEncoder)

# Fetch all DeviceLogs entries related to this Device, ordered by timestamp descendingly
device_logs_qs = DeviceLogs.objects.filter(device=device).order_by('-timestamp')

# Implement pagination (10 logs per page)
paginator = Paginator(device_logs_qs, 10)
page_number = self.request.GET.get('page')
page_obj = paginator.get_page(page_number)

context['device_logs'] = page_obj
context['paginator'] = paginator
context['page_obj'] = page_obj

# Define a level to badge class mapping
context['level_badge_map'] = {
10: 'bg-secondary', # DEBUG
20: 'bg-info', # INFO
30: 'bg-warning', # WARNING
40: 'bg-danger', # ERROR
50: 'bg-dark', # CRITICAL
}

return context


Expand Down
3 changes: 2 additions & 1 deletion app/main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.humanize',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.staticfiles',
'django.contrib.sites',
# Third-party
Expand Down
Loading

0 comments on commit 3d061c7

Please sign in to comment.