Skip to content

Commit

Permalink
Added json enforce no other properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiky30 committed Feb 16, 2024
1 parent 9f0e7fe commit eccb07c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 52 deletions.
10 changes: 0 additions & 10 deletions shedpi_hub_dashboard/static/shedpi_hub_dashboard/dummy_data.json

This file was deleted.

86 changes: 62 additions & 24 deletions shedpi_hub_dashboard/static/shedpi_hub_dashboard/js/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const contents = document.getElementsByClassName("contents")[0];
let section = contents

let deviceModuleEndpoint = "/api/v1/device-module-readings/"
let deviceModuleEndpoint = ""

// Global store for the device modules, with schema
let storeDeviceModules = []
Expand Down Expand Up @@ -72,13 +72,14 @@ section.append(tableContainer);

let loadTableData = function (deviceModuleId) {

// TODO: Get data based on deviceModuleId
// Endpoint: http://127.0.0.1:8000/api/v1/device-module-readings/
// device_module = deviceModuleId
// FIXME: Build the query string using the js libraries
const endpoint = deviceModuleEndpoint + "?device_module=" + deviceModuleId
// FIXME: Pass a reversed full url through
// const urlDeviceModuleReading = section.getAttribute("data-json-feed")
// const url = section.getAttribute("data-json-feed")
const url = "http://localhost:8000//api/v1/device-module-readings/"
const endpoint = new URL(url);
endpoint.searchParams.append("device_module", deviceModuleId);

// FIXME: Need data output and need headings from Schema

// const urlDeviceModuleReading =
let endpointDeviceModuleReading = new Request(endpoint);

response = fetch(endpointDeviceModuleReading)
Expand All @@ -90,12 +91,12 @@ let loadTableData = function (deviceModuleId) {
return response.json();
})
.then((response) => {
drawTable(response)
drawTable(response, deviceModuleId)
});
}


let drawTable = function (data) {
let drawTable = function (dataset, deviceModuleId) {
// First empty the table container
tableContainer.textContent = ""

Expand All @@ -104,33 +105,70 @@ let drawTable = function (data) {
// Table Header
let headerRow = document.createElement("tr");

// TODO: Build the header rows from the data, for now use the first row.
// For other projects this was supplied in the endpoint
// TODO: Build the header rows from the schema, or build a full list in th
// Could use the schema, what about historic data that may violate it,
// we only validate this when historic data is updated
// Built as a ist because the pagination would hammer the device modiule
const headingFields = dataset[0];

let deviceModules = storeDeviceModules
// TODO: Build the header rows from the schema, or build a full list in the backend and supply in the response
// Could use the schema, what about historic data that may violate it,
// Built as a ist because the pagination would hammer the device modiule

schema = deviceModuleSchemaMap[deviceModuleId]

console.log("Drawing table with data: ", data)
let dataFields = []
if (schema) {
extra_fields = Object.keys(schema.properties)
dataFields = [...dataFields, ...extra_fields];
dataFields = [...new Set(dataFields)]
}

for (let heading in data[0]) {
// FIXME: Need human readable headings, probably needs to come from the BE to be
for (let heading in headingFields) {

let headerItem = document.createElement("th");
headerItem.textContent = heading
headerRow.append(headerItem);
if (heading == "data") {

for (let headingIndex in dataFields) {
const heading = dataFields[headingIndex]
let headerItem = document.createElement("th");
headerItem.textContent = heading
headerRow.append(headerItem);
}
} else {
let headerItem = document.createElement("th");
headerItem.textContent = heading
headerRow.append(headerItem);
}
}

table.append(headerRow);

// Table Contents
for (let row in data) {
for (let rowIndex in dataset) {
const row = dataset[rowIndex]
let contentRow = document.createElement("tr");
for (let reading in data[row]) {
let contentItem = document.createElement("td");
contentItem.textContent = data[row][reading]
contentRow.append(contentItem);
for (let reading in row) {
const fieldValue = row[reading]
if (typeof fieldValue == "object") {
for (let dataFieldIndex in dataFields) {
let contentItem = document.createElement("td");
const dataField = dataFields[dataFieldIndex]

// FIXME: Need to change the null value in the project to be an empty object
let mydict = {}
if (fieldValue != null) {
if (fieldValue.hasOwnProperty(dataField)) {
contentItem.textContent = fieldValue[dataField]
}
}

contentRow.append(contentItem);
}
} else {
let contentItem = document.createElement("td");
contentItem.textContent = row[reading]
contentRow.append(contentItem);
}
}
table.append(contentRow);
}
Expand Down
35 changes: 17 additions & 18 deletions shedpi_hub_dashboard/templates/shedpi_hub_dashboard/index.html
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@

{% load static %}

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>ShedPi</title>
<link rel="stylesheet" href="{% static 'shedpi_hub_dashboard/css/landing.css' %}">
</head>
<body>
<header>
<h1>Shed Pi data</h1>
</header>
<div class="contents" data-json-feed="{% static 'shedpi_hub_dashboard/dummy_data.json' %}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>ShedPi</title>
<link rel="stylesheet" href="{% static 'shedpi_hub_dashboard/css/landing.css' %}">
</head>
<body>
<header>
<h1>Shed Pi data</h1>
</header>
<div class="contents" data-endpoint-url="">

</div>
<footer>
</footer>
<script src="{% static 'shedpi_hub_dashboard/js/index.js' %}"></script>
</body>
</div>
<footer>
</footer>
<script src="{% static 'shedpi_hub_dashboard/js/index.js' %}"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions shedpi_hub_dashboard/tests/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ def test_json_schema_update_with_invalid_data():
reading.save()


@pytest.mark.django_db
def test_json_schema_update_with_more_fields_supplied():
schema = {
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"type": "object",
"properties": {
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0,
}
},
"unevaluatedProperties": False,
}
data = {"age": 1, "someOtherField": "Some value here"}
device_module = DeviceModuleFactory(schema=schema)
reading = DeviceModuleReading(device_module=device_module, data=data)

with pytest.raises(ValidationError):
reading.save()


@pytest.mark.django_db
def test_json_schema_invalid_schema():
schema = {"type": 1234}
Expand Down

0 comments on commit eccb07c

Please sign in to comment.