team-3/src/c3nav/control/views/mesh.py

49 lines
1.5 KiB
Python
Raw Normal View History

2023-10-02 22:02:25 +02:00
from django.db.models import Max
2023-10-02 17:31:24 +02:00
from django.views.generic import ListView
2023-10-02 22:02:25 +02:00
from c3nav.control.forms import MeshMessageFilerForm
2023-10-02 17:42:01 +02:00
from c3nav.control.views.base import ControlPanelMixin
2023-10-02 22:02:25 +02:00
from c3nav.mesh.models import MeshNode, NodeMessage
2023-10-02 17:31:24 +02:00
class MeshNodeListView(ControlPanelMixin, ListView):
model = MeshNode
template_name = "control/mesh_nodes.html"
ordering = "address"
context_object_name = "nodes"
2023-10-02 22:02:25 +02:00
def get_queryset(self):
return super().get_queryset().annotate(last_msg=Max('received_messages__datetime')).prefetch_last_messages()
2023-10-02 22:02:25 +02:00
class MeshMessageListView(ControlPanelMixin, ListView):
model = NodeMessage
template_name = "control/mesh_messages.html"
ordering = "-datetime"
paginate_by = 20
context_object_name = "mesh_messages"
def get_queryset(self):
qs = super().get_queryset()
self.form = MeshMessageFilerForm(self.request.GET)
if self.form.is_valid():
if self.form.cleaned_data['message_types']:
qs = qs.filter(message_type__in=self.form.cleaned_data['message_types'])
if self.form.cleaned_data['src_nodes']:
qs = qs.filter(src_node__in=self.form.cleaned_data['src_nodes'])
2023-10-02 22:02:25 +02:00
return qs
def get_context_data(self, *args, **kwargs):
ctx = super().get_context_data(*args, **kwargs)
form_data = self.request.GET.copy()
form_data.pop('page', None)
ctx.update({
'form': self.form,
'form_data': form_data.urlencode(),
})
return ctx