Skip to content

Commit

Permalink
Fixed broken json widget not validating properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiky30 committed Feb 15, 2024
1 parent e66a04c commit 9f0e7fe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
15 changes: 13 additions & 2 deletions shedpi_hub_dashboard/forms/fields.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import json

from django.db.models import JSONField
from django.forms import JSONField as JSONFormField
from django.forms import widgets


class PrettyJSONWidget(widgets.Textarea):
def format_value(self, value):
# Prettify the json
value = json.dumps(json.loads(value), indent=2, sort_keys=True)
try:
# Prettify the json
value = json.dumps(json.loads(value), indent=2, sort_keys=True)
except json.JSONDecodeError:
return super(PrettyJSONWidget, self).format_value(value)

# Calculate the size of the contents
row_lengths = [len(r) for r in value.split("\n")]
Expand All @@ -23,3 +27,10 @@ def format_value(self, value):

class PrettyJsonFormField(JSONFormField):
widget = PrettyJSONWidget


class PrettyJsonField(JSONField):
def formfield(self, **kwargs):
defaults = {"form_class": PrettyJsonFormField}
defaults.update(kwargs)
return super().formfield(**defaults)
4 changes: 2 additions & 2 deletions shedpi_hub_dashboard/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Migration(migrations.Migration):
("location", models.CharField(max_length=50)),
(
"schema",
shedpi_hub_dashboard.models.PrettySONField(blank=True, null=True),
shedpi_hub_dashboard.models.PrettyJsonField(blank=True, null=True),
),
(
"device",
Expand All @@ -70,7 +70,7 @@ class Migration(migrations.Migration):
),
(
"data",
shedpi_hub_dashboard.models.PrettySONField(blank=True, null=True),
shedpi_hub_dashboard.models.PrettyJsonField(blank=True, null=True),
),
("created_at", models.DateTimeField(auto_now_add=True)),
(
Expand Down
14 changes: 3 additions & 11 deletions shedpi_hub_dashboard/models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import uuid

from django.db import models
from django.db.models import JSONField
from jsonschema import validate

from shedpi_hub_dashboard.forms.fields import PrettyJsonFormField


class PrettySONField(JSONField):
def formfield(self, **kwargs):
defaults = {"form_class": PrettyJsonFormField}
defaults.update(kwargs)
return super().formfield(**defaults)
from shedpi_hub_dashboard.forms.fields import PrettyJsonField


class Device(models.Model):
Expand All @@ -32,7 +24,7 @@ class DeviceModule(models.Model):
)
name = models.CharField(max_length=20)
location = models.CharField(max_length=50)
schema = PrettySONField(null=True, blank=True)
schema = PrettyJsonField(null=True, blank=True)

def __str__(self):
return self.name
Expand All @@ -44,7 +36,7 @@ class DeviceModuleReading(models.Model):
on_delete=models.CASCADE,
help_text="A device whose readings were collected.",
)
data = PrettySONField(null=True, blank=True)
data = PrettyJsonField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)

def validate_data(self) -> None:
Expand Down
19 changes: 4 additions & 15 deletions shedpi_hub_dashboard/tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ def test_device_module_list(client):
assert len(response.data) == 2


# TODO: device_module_readings_list


@pytest.mark.django_db
def test_device_module_readings_list(client):
"""
Expand All @@ -39,12 +36,8 @@ def test_device_module_readings_list(client):
},
}
device_module = DeviceModuleFactory(schema=schema)
reading_1 = DeviceModuleReadingFactory(
device_module=device_module, data={"temperature": "20"}
)
reading_2 = DeviceModuleReadingFactory(
device_module=device_module, data={"temperature": "22"}
)
DeviceModuleReadingFactory(device_module=device_module, data={"temperature": "20"})
DeviceModuleReadingFactory(device_module=device_module, data={"temperature": "22"})
# Another modules readings that shouldn't be returned
DeviceModuleReadingFactory(data={"temperature": "10"})

Expand All @@ -69,12 +62,8 @@ def test_device_module_readings_list_no_device_module_supplied(client):
},
}
device_module = DeviceModuleFactory(schema=schema)
reading_1 = DeviceModuleReadingFactory(
device_module=device_module, data={"temperature": "20"}
)
reading_2 = DeviceModuleReadingFactory(
device_module=device_module, data={"temperature": "22"}
)
DeviceModuleReadingFactory(device_module=device_module, data={"temperature": "20"})
DeviceModuleReadingFactory(device_module=device_module, data={"temperature": "22"})
# Another modules readings that shouldn't be returned
DeviceModuleReadingFactory(data={"temperature": "10"})

Expand Down

0 comments on commit 9f0e7fe

Please sign in to comment.