Skip to content

Commit

Permalink
Endpoints wired into the frontend, needs the frontend improving to dr…
Browse files Browse the repository at this point in the history
…aw / redraw based on the users selection
  • Loading branch information
Aiky30 committed Jan 28, 2024
1 parent 96cbbd8 commit 98690d7
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 33 deletions.
10 changes: 8 additions & 2 deletions shedpi_hub_dashboard/serlializers.py
Original file line number Diff line number Diff line change
@@ -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__"
82 changes: 65 additions & 17 deletions shedpi_hub_dashboard/static/shedpi_hub_dashboard/js/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,73 @@

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");

// 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]
Expand All @@ -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);
Expand All @@ -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
34 changes: 23 additions & 11 deletions shedpi_hub_dashboard/tests/test_endpoints.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
9 changes: 7 additions & 2 deletions shedpi_hub_dashboard/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
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):
response = TemplateResponse(request, "shedpi_hub_dashboard/index.html", {})
return response


class DeviceModuleViewSet(viewsets.ModelViewSet):
queryset = DeviceModule.objects.all()
serializer_class = DeviceModuleSerializer


class DeviceModuleReadingViewSet(viewsets.ModelViewSet):
queryset = DeviceModuleReading.objects.all()
serializer_class = DeviceModuleReadingSerializer
3 changes: 2 additions & 1 deletion shedpi_hub_example_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 98690d7

Please sign in to comment.