devicehub-django/dashboard/views.py

107 lines
3 KiB
Python
Raw Normal View History

2024-10-09 10:18:09 +00:00
import json
2024-07-01 10:19:21 +00:00
from django.utils.translation import gettext_lazy as _
2024-10-09 10:18:09 +00:00
from django.views.generic.edit import FormView
from django.shortcuts import Http404
2024-10-09 11:05:18 +00:00
from django.db.models import Q
2024-07-10 11:55:57 +00:00
from dashboard.mixins import InventaryMixin, DetailsMixin
2024-10-09 10:18:09 +00:00
from evidence.models import Annotation
from evidence.xapian import search
2024-07-05 13:32:07 +00:00
from device.models import Device
2024-07-30 11:37:08 +00:00
from lot.models import Lot
2024-07-01 10:19:21 +00:00
2024-07-09 11:34:30 +00:00
class UnassignedDevicesView(InventaryMixin):
2024-07-05 13:32:07 +00:00
template_name = "unassigned_devices.html"
section = "Unassigned"
title = _("Unassigned Devices")
breadcrumb = "Devices / Unassigned Devices"
2024-07-01 10:19:21 +00:00
def get_devices(self, user, offset, limit):
2024-10-07 14:56:58 +00:00
return Device.get_unassigned(self.request.user.institution, offset, limit)
2024-07-10 11:55:57 +00:00
class LotDashboardView(InventaryMixin, DetailsMixin):
template_name = "unassigned_devices.html"
2024-07-19 15:40:01 +00:00
section = "dashboard_lot"
2024-07-10 11:55:57 +00:00
title = _("Lot Devices")
2024-07-19 15:40:01 +00:00
breadcrumb = "Lot / Devices"
2024-07-10 11:55:57 +00:00
model = Lot
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2024-07-30 17:38:04 +00:00
lot = context.get('object')
2024-07-01 10:19:21 +00:00
context.update({
2024-07-30 17:38:04 +00:00
'lot': lot,
2024-07-01 10:19:21 +00:00
})
return context
2024-07-19 15:40:01 +00:00
def get_devices(self, user, offset, limit):
2024-10-09 10:18:09 +00:00
chids = self.object.devicelot_set.all().values_list(
"device_id", flat=True
).distinct()
2024-10-09 11:05:18 +00:00
2024-10-02 10:27:20 +00:00
chids_page = chids[offset:offset+limit]
return [Device(id=x) for x in chids_page], chids.count()
2024-10-09 10:18:09 +00:00
class SearchView(InventaryMixin):
template_name = "unassigned_devices.html"
section = "Search"
title = _("Search Devices")
breadcrumb = "Devices / Search Devices"
2024-10-09 11:05:18 +00:00
2024-10-09 10:18:09 +00:00
def get_devices(self, user, offset, limit):
post = dict(self.request.POST)
query = post.get("search")
if not query:
return [], 0
2024-10-09 11:05:18 +00:00
2024-10-09 10:18:09 +00:00
matches = search(
self.request.user.institution,
query[0],
offset,
limit
)
2024-10-09 11:05:18 +00:00
2024-10-25 15:36:13 +00:00
if not matches or not matches.size():
2024-10-09 11:05:18 +00:00
return self.search_hids(query, offset, limit)
2024-10-16 12:17:35 +00:00
devices = []
2024-10-28 17:09:45 +00:00
dev_id = []
2024-10-09 10:18:09 +00:00
for x in matches:
2024-10-28 17:09:45 +00:00
# devices.append(self.get_annotations(x))
dev = self.get_annotations(x)
if dev.id not in dev_id:
devices.append(dev)
dev_id.append(dev.id)
2024-10-09 10:18:09 +00:00
count = matches.size()
2024-10-28 17:09:45 +00:00
# TODO fix of pagination, the count is not correct
2024-10-09 10:18:09 +00:00
return devices, count
def get_annotations(self, xp):
snap = xp.document.get_data()
uuid = json.loads(snap).get('uuid')
2024-10-16 12:17:35 +00:00
return Device.get_annotation_from_uuid(uuid, self.request.user.institution)
2024-10-09 10:18:09 +00:00
2024-10-09 11:05:18 +00:00
def search_hids(self, query, offset, limit):
qry = Q()
for i in query[0].split(" "):
if i:
qry |= Q(value__startswith=i)
chids = Annotation.objects.filter(
type=Annotation.Type.SYSTEM,
owner=self.request.user.institution
).filter(
qry
).values_list("value", flat=True).distinct()
chids_page = chids[offset:offset+limit]
return [Device(id=x) for x in chids_page], chids.count()