From 7ab88ad290f5e7411b90512ce9b2f5209bab01bc Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 19 Nov 2024 21:18:29 +0100 Subject: [PATCH] add did view --- did/__init__.py | 0 did/admin.py | 3 + did/apps.py | 6 ++ did/migrations/__init__.py | 0 did/models.py | 3 + did/templates/device_web.html | 171 ++++++++++++++++++++++++++++++++++ did/tests.py | 3 + did/urls.py | 8 ++ did/views.py | 60 ++++++++++++ 9 files changed, 254 insertions(+) create mode 100644 did/__init__.py create mode 100644 did/admin.py create mode 100644 did/apps.py create mode 100644 did/migrations/__init__.py create mode 100644 did/models.py create mode 100644 did/templates/device_web.html create mode 100644 did/tests.py create mode 100644 did/urls.py create mode 100644 did/views.py diff --git a/did/__init__.py b/did/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/did/admin.py b/did/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/did/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/did/apps.py b/did/apps.py new file mode 100644 index 0000000..8893167 --- /dev/null +++ b/did/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class DidConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "did" diff --git a/did/migrations/__init__.py b/did/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/did/models.py b/did/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/did/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/did/templates/device_web.html b/did/templates/device_web.html new file mode 100644 index 0000000..8c607d3 --- /dev/null +++ b/did/templates/device_web.html @@ -0,0 +1,171 @@ + + + + + + {{ object.type }} + + + + + +
+

{{ object.manufacturer }} {{ object.type }} {{ object.model }}

+ +
+
+

Details

+
+
Phid
+
+
{{ object.id }}
+
+
+
+
Type
+
{{ object.type }}
+
+ + {% if object.is_websnapshot %} + {% for snapshot_key, snapshot_value in object.last_user_evidence %} +
+
{{ snapshot_key }}
+
{{ snapshot_value|default:'' }}
+
+ {% endfor %} + {% else %} +
+
Manufacturer
+
{{ object.manufacturer|default:'' }}
+
+
+
Model
+
{{ object.model|default:'' }}
+
+ {% if user.is_authenticated %} +
+
Serial Number
+
{{ object.serial_number|default:'' }}
+
+ {% endif %} + {% endif %} +
+ +
+

Identifiers

+ {% for chid in object.hids %} +
+
{{ chid|default:'' }}
+
+ {% endfor %} +
+
+

Components

+
+ {% for component in object.components %} +
+
+
+
{{ component.type }}
+

+ {% for component_key, component_value in component.items %} + {% if component_key not in 'actions,type' %} + {% if component_key != 'serialNumber' or user.is_authenticated %} + {{ component_key }}: {{ component_value }}
+ {% endif %} + {% endif %} + {% endfor %} +

+
+
+
+ {% endfor %} +
+
+ + + + + diff --git a/did/tests.py b/did/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/did/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/did/urls.py b/did/urls.py new file mode 100644 index 0000000..0eda70c --- /dev/null +++ b/did/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from did import views + +app_name = 'did' + +urlpatterns = [ + path("", views.PublicDeviceWebView.as_view(), name="device_web"), +] diff --git a/did/views.py b/did/views.py new file mode 100644 index 0000000..1caa081 --- /dev/null +++ b/did/views.py @@ -0,0 +1,60 @@ +from django.http import JsonResponse, Http404 +from django.views.generic.base import TemplateView +from device.models import Device + + +class PublicDeviceWebView(TemplateView): + template_name = "device_web.html" + + def get(self, request, *args, **kwargs): + self.pk = kwargs['pk'] + self.object = Device(id=self.pk) + + if not self.object.last_evidence: + raise Http404 + + if self.request.headers.get('Accept') == 'application/json': + return self.get_json_response() + return super().get(request, *args, **kwargs) + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + self.object.initial() + context.update({ + 'object': self.object + }) + return context + + @property + def public_fields(self): + return { + 'id': self.object.id, + 'shortid': self.object.shortid, + 'uuids': self.object.uuids, + 'hids': self.object.hids, + 'components': self.remove_serial_number_from(self.object.components), + } + + @property + def authenticated_fields(self): + return { + 'serial_number': self.object.serial_number, + 'components': self.object.components, + } + + def remove_serial_number_from(self, components): + for component in components: + if 'serial_number' in component: + del component['SerialNumber'] + return components + + def get_device_data(self): + data = self.public_fields + if self.request.user.is_authenticated: + data.update(self.authenticated_fields) + return data + + def get_json_response(self): + device_data = self.get_device_data() + return JsonResponse(device_data) +