From 9b701d9804db9b68a7fbb3061f4c4bb749b152d4 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 14 Feb 2025 09:11:21 +0100 Subject: [PATCH] add all devices view --- dashboard/templates/base.html | 19 ++++++-- dashboard/urls.py | 1 + dashboard/views.py | 11 +++++ device/models.py | 81 +++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 3 deletions(-) diff --git a/dashboard/templates/base.html b/dashboard/templates/base.html index 7b80134..6a26db3 100644 --- a/dashboard/templates/base.html +++ b/dashboard/templates/base.html @@ -114,6 +114,19 @@ {% endif %} + diff --git a/dashboard/urls.py b/dashboard/urls.py index 4e27760..cf19b31 100644 --- a/dashboard/urls.py +++ b/dashboard/urls.py @@ -5,6 +5,7 @@ app_name = 'dashboard' urlpatterns = [ path("", views.UnassignedDevicesView.as_view(), name="unassigned"), + path("all", views.AllDevicesView.as_view(), name="all_device"), path("/", views.LotDashboardView.as_view(), name="lot"), path("search", views.SearchView.as_view(), name="search"), ] diff --git a/dashboard/views.py b/dashboard/views.py index 4917bb8..0d9ebff 100644 --- a/dashboard/views.py +++ b/dashboard/views.py @@ -22,6 +22,17 @@ class UnassignedDevicesView(InventaryMixin): return Device.get_unassigned(self.request.user.institution, offset, limit) +class AllDevicesView(InventaryMixin): + template_name = "unassigned_devices.html" + section = "All" + title = _("All Devices") + breadcrumb = "Devices / All Devices" + + def get_devices(self, user, offset, limit): + import pdb; pdb.set_trace() + return Device.get_all(self.request.user.institution, offset, limit) + + class LotDashboardView(InventaryMixin, DetailsMixin): template_name = "unassigned_devices.html" section = "dashboard_lot" diff --git a/device/models.py b/device/models.py index a385895..78d2e44 100644 --- a/device/models.py +++ b/device/models.py @@ -136,6 +136,87 @@ class Device: self.lots = [ x.lot for x in DeviceLot.objects.filter(device_id=self.id)] + def get_all(cls, institution, offset=0, limit=None): + sql = """ + WITH RankedAnnotations AS ( + SELECT + t1.value, + t1.key, + ROW_NUMBER() OVER ( + PARTITION BY t1.uuid + ORDER BY + CASE + WHEN t1.key = 'CUSTOM_ID' THEN 1 + WHEN t1.key = 'hidalgo1' THEN 2 + ELSE 3 + END, + t1.created DESC + ) AS row_num + FROM evidence_annotation AS t1 + WHERE t1.owner_id = {institution} + AND t1.type = {type} + ) + SELECT DISTINCT + value + FROM + RankedAnnotations + WHERE + row_num = 1 + """.format( + institution=institution.id, + type=Annotation.Type.SYSTEM, + ) + if limit: + sql += " limit {} offset {}".format(int(limit), int(offset)) + + sql += ";" + + annotations = [] + with connection.cursor() as cursor: + cursor.execute(sql) + annotations = cursor.fetchall() + + devices = [cls(id=x[0]) for x in annotations] + count = cls.get_all_count(institution) + return devices, count + + @classmethod + def get_all_count(cls, institution): + + sql = """ + WITH RankedAnnotations AS ( + SELECT + t1.value, + t1.key, + ROW_NUMBER() OVER ( + PARTITION BY t1.uuid + ORDER BY + CASE + WHEN t1.key = 'CUSTOM_ID' THEN 1 + WHEN t1.key = 'hidalgo1' THEN 2 + ELSE 3 + END, + t1.created DESC + ) AS row_num + FROM evidence_annotation AS t1 + WHERE t1.owner_id = {institution} + AND t1.type = {type} + ) + SELECT + COUNT(DISTINCT value) + FROM + RankedAnnotations + WHERE + row_num = 1 + """.format( + institution=institution.id, + type=Annotation.Type.SYSTEM, + ) + with connection.cursor() as cursor: + cursor.execute(sql) + return cursor.fetchall()[0][0] + + @classmethod def get_unassigned(cls, institution, offset=0, limit=None):