From 98690d7f515b69d8b9407adfb4b955b4f1013597 Mon Sep 17 00:00:00 2001 From: Andrew Aikman Date: Sun, 28 Jan 2024 18:20:43 +0000 Subject: [PATCH] Endpoints wired into the frontend, needs the frontend improving to draw / redraw based on the users selection --- shedpi_hub_dashboard/serlializers.py | 10 ++- .../static/shedpi_hub_dashboard/js/index.js | 82 +++++++++++++++---- shedpi_hub_dashboard/tests/test_endpoints.py | 34 +++++--- shedpi_hub_dashboard/views.py | 9 +- shedpi_hub_example_project/urls.py | 3 +- 5 files changed, 105 insertions(+), 33 deletions(-) diff --git a/shedpi_hub_dashboard/serlializers.py b/shedpi_hub_dashboard/serlializers.py index 27c69a7..3107c42 100644 --- a/shedpi_hub_dashboard/serlializers.py +++ b/shedpi_hub_dashboard/serlializers.py @@ -1,9 +1,15 @@ from rest_framework import serializers -from .models import DeviceModuleReading +from .models import DeviceModule, DeviceModuleReading -class DeviceModuleReadingSerializer(serializers.HyperlinkedModelSerializer): +class DeviceModuleSerializer(serializers.ModelSerializer): + class Meta: + model = DeviceModule + fields = "__all__" + + +class DeviceModuleReadingSerializer(serializers.ModelSerializer): class Meta: model = DeviceModuleReading fields = "__all__" diff --git a/shedpi_hub_dashboard/static/shedpi_hub_dashboard/js/index.js b/shedpi_hub_dashboard/static/shedpi_hub_dashboard/js/index.js index df7d187..e6c8f0b 100644 --- a/shedpi_hub_dashboard/static/shedpi_hub_dashboard/js/index.js +++ b/shedpi_hub_dashboard/static/shedpi_hub_dashboard/js/index.js @@ -1,9 +1,65 @@ - const contents = document.getElementsByClassName("contents"); let section = contents[0] -const url = section.getAttribute("data-json-feed") -const myRequest = new Request(url); + +/* Drop down selection */ +const urlDeviceModule = "/api/v1/device-module/" +let endpointDeviceModule = new Request(urlDeviceModule); +response = fetch(endpointDeviceModule) + .then((response) => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + + return response.json(); + }) + .then((response) => { + drawDropdown(response) + }); + +let drawDropdown = function (data) { + let dropdown = document.createElement("select"); + + // Table Header + let emptySelector = document.createElement("option"); + emptySelector.textContent = "Please Select" + dropdown.append(emptySelector) + + for (let deviceModuleIndex in data) { + const deviceModule = data[deviceModuleIndex] + + let optionElement = document.createElement("option"); + optionElement.textContent = deviceModule.device + " - " + deviceModule.name + + optionElement.addEventListener('select', function (e) { + console.log("Draw data") + }); + + dropdown.append(optionElement); + } + + // Add the drpdown to the page + section.append(dropdown); +}; + +/* Table visual */ + +let loadTableData = function () { + const urlDeviceModuleReading = section.getAttribute("data-json-feed") + let endpointDeviceModuleReading = new Request(urlDeviceModuleReading); + response = fetch(endpointDeviceModuleReading) + .then((response) => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + + return response.json(); + }) + .then((response) => { + drawTable(response) + }); +} + let drawTable = function (data) { let table = document.createElement("table"); @@ -11,7 +67,7 @@ let drawTable = function (data) { // Table Header let headerRow = document.createElement("tr"); - for(let heading in data.headings) { + for (let heading in data.headings) { let headerItem = document.createElement("th"); headerItem.textContent = data.headings[heading] @@ -21,9 +77,9 @@ let drawTable = function (data) { table.append(headerRow); // Table Contents - for(let row in data.readings) { + for (let row in data.readings) { let contentRow = document.createElement("tr"); - for(let reading in data.readings[row]) { + for (let reading in data.readings[row]) { let contentItem = document.createElement("td"); contentItem.textContent = data.readings[row][reading] contentRow.append(contentItem); @@ -36,14 +92,6 @@ let drawTable = function (data) { } -response = fetch(myRequest) - .then((response) => { - if (!response.ok) { - throw new Error(`HTTP error! Status: ${response.status}`); - } - - return response.json(); - }) - .then((response) => { - drawTable(response) - }); +/* TODO - On select click, load the table with the reading data */ +// Bind dropdown click +// reloadTable: destroy and rebuild diff --git a/shedpi_hub_dashboard/tests/test_endpoints.py b/shedpi_hub_dashboard/tests/test_endpoints.py index 1b458db..08ac298 100644 --- a/shedpi_hub_dashboard/tests/test_endpoints.py +++ b/shedpi_hub_dashboard/tests/test_endpoints.py @@ -1,6 +1,5 @@ -# TODO: -# - Schema with data fields -# - Submit data to the endpoints +import json + import pytest from django.urls import reverse from rest_framework import status @@ -9,22 +8,35 @@ @pytest.mark.django_db -def test_device_reading_submission(client): +def test_device_module_list(client): + DeviceModuleFactory.create_batch(2) + + url = reverse("devicemodule-list") + response = client.get(url) + + assert response.status_code == status.HTTP_200_OK + assert len(response.data) == 2 + + +@pytest.mark.django_db +def test_device_module_reading_submission(client): schema = { "$id": "https://example.com/person.schema.json", "$schema": "https://json-schema.org/draft/2020-12/schema", - "title": "Person", + "title": "Reading", "type": "object", "properties": { "temperature": {"type": "string", "description": "The Temperature"}, }, } device_module = DeviceModuleFactory(schema=schema) - # devicemodulereading-list - # devicemodulereading-detail - url = reverse("devicemodulereading-detail", kwargs={"pk": device_module.id}) - response = client.post(url, data={"device_module_id": device_module.id}) + # url = reverse("devicemodulereading-detail", kwargs={"pk": device_module.id}) + url = reverse("devicemodulereading-list") + data = {"temperature": "20.001"} + response = client.post( + url, data={"device_module": device_module.id, "data": json.dumps(data)} + ) - assert response.status_code == status.HTTP_200_OK - assert set(response.data.keys()) == {} + assert response.status_code == status.HTTP_201_CREATED + assert response.data["data"] == data diff --git a/shedpi_hub_dashboard/views.py b/shedpi_hub_dashboard/views.py index 8bb5c7d..5a2cf4c 100644 --- a/shedpi_hub_dashboard/views.py +++ b/shedpi_hub_dashboard/views.py @@ -1,8 +1,8 @@ from django.template.response import TemplateResponse from rest_framework import viewsets -from .models import DeviceModuleReading -from .serlializers import DeviceModuleReadingSerializer +from .models import DeviceModule, DeviceModuleReading +from .serlializers import DeviceModuleReadingSerializer, DeviceModuleSerializer def index(request): @@ -10,6 +10,11 @@ def index(request): return response +class DeviceModuleViewSet(viewsets.ModelViewSet): + queryset = DeviceModule.objects.all() + serializer_class = DeviceModuleSerializer + + class DeviceModuleReadingViewSet(viewsets.ModelViewSet): queryset = DeviceModuleReading.objects.all() serializer_class = DeviceModuleReadingSerializer diff --git a/shedpi_hub_example_project/urls.py b/shedpi_hub_example_project/urls.py index 31109b4..679974d 100644 --- a/shedpi_hub_example_project/urls.py +++ b/shedpi_hub_example_project/urls.py @@ -4,9 +4,10 @@ from django.urls import include, path from rest_framework import routers -from shedpi_hub_dashboard.views import DeviceModuleReadingViewSet +from shedpi_hub_dashboard.views import DeviceModuleReadingViewSet, DeviceModuleViewSet router = routers.DefaultRouter() +router.register(r"device-module", DeviceModuleViewSet) router.register(r"device-module-readings", DeviceModuleReadingViewSet) urlpatterns = [