diff --git a/src/meshapi/tests/test_widgets.py b/src/meshapi/tests/test_widgets.py new file mode 100644 index 00000000..0f65e059 --- /dev/null +++ b/src/meshapi/tests/test_widgets.py @@ -0,0 +1,15 @@ +from django.test import TestCase + +from meshapi.widgets import PanoramaViewer + + +class TestPanoramaViewer(TestCase): + def setUp(self): + pass + + def test_pano_get_context(self): + PanoramaViewer.pano_get_context("test", '["blah", "blah2"]') + + def test_pano_get_context_bad_value(self): + PanoramaViewer.pano_get_context("test", 100) # type: ignore + PanoramaViewer.pano_get_context("test", None) # type: ignore diff --git a/src/meshapi/widgets.py b/src/meshapi/widgets.py index 880a8ac9..4d4bd54e 100644 --- a/src/meshapi/widgets.py +++ b/src/meshapi/widgets.py @@ -1,4 +1,5 @@ import json +import logging from typing import Any, Callable, Dict, Optional from django import forms @@ -15,8 +16,14 @@ class PanoramaViewer(JSONFormWidget): def __init__(self, schema: dict): super().__init__(schema) - def pano_get_context(self, name: str, value: str = "[]") -> dict: - value_as_array = json.loads(value) + @staticmethod + def pano_get_context(name: str, value: str) -> dict: + try: + value_as_array = json.loads(value) if value else "" + except TypeError: + logging.exception("Got bad value when trying to make panorama array.") + value_as_array = "" + return { "widget": { "name": name,