Skip to content

Commit 2df46b4

Browse files
authored
Removed django-prettyjson in favour of plain formatter/error highlighter (#2835)
1 parent 0a4e6a2 commit 2df46b4

File tree

10 files changed

+54
-45
lines changed

10 files changed

+54
-45
lines changed

mypy.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ ignore_missing_imports = on
7272
[mypy-mixer.*]
7373
ignore_missing_imports = on
7474

75-
[mypy-prettyjson.*]
76-
ignore_missing_imports = on
77-
7875
[mypy-shortuuid.*]
7976
ignore_missing_imports = on
8077

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ dependencies = [
2121
"django-filter>=25.2,<25.3",
2222
"django-healthchecks>=1.5.0,<2.0.0",
2323
"django-ipware>=7.0.0,<8.0.0",
24-
"django-prettyjson>=0.4.1,<1.0.0",
2524
"django-silk>=5.3.2,<6.0.0",
2625
"django-split-settings>=1.2.0,<2.0.0",
2726
"django-storages>=1.14,<2.0",

src/core/admin/mixin.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
from django.template.defaultfilters import capfirst, time
1212
from django.utils import timezone
1313
from django.utils.html import format_html
14-
from prettyjson import PrettyJSONWidget
1514

16-
from core.admin.widgets import AppNumberInput
15+
from core.admin.widgets import AppJSONEditor, AppNumberInput
1716
from core.pricing import format_price
1817

1918
if TYPE_CHECKING:
@@ -38,7 +37,7 @@ class AppAdminMixin:
3837
formfield_overrides: Mapping[Type[Field], Mapping[str, Any]] = {
3938
models.DecimalField: {"widget": AppNumberInput},
4039
models.IntegerField: {"widget": AppNumberInput},
41-
models.JSONField: {"widget": PrettyJSONWidget(attrs={"initial": "parsed"})},
40+
models.JSONField: {"widget": AppJSONEditor},
4241
}
4342
foreignkey_queryset_overrides: Mapping[str, Callable[["Apps"], QuerySet]] = {}
4443
global_exclude = (
@@ -48,7 +47,7 @@ class AppAdminMixin:
4847

4948
class Media:
5049
css = {
51-
"all": ["admin.css", "prettyjson.css"],
50+
"all": ["admin/admin.css"],
5251
}
5352

5453
def formfield_for_foreignkey(self, db_field: "ForeignKey", request: "HttpRequest", **kwargs: Any) -> "ModelChoiceField":

src/core/admin/widgets.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django.forms.widgets import NumberInput
1+
from django.forms.widgets import NumberInput, Textarea
22

33

44
class AppNumberInput(NumberInput):
@@ -13,3 +13,17 @@ def __init__(self, attrs: dict | None = None) -> None:
1313
attrs["class"] = f"{classes} app-number-input"
1414

1515
super().__init__(attrs)
16+
17+
18+
class AppJSONEditor(Textarea):
19+
class Media:
20+
js = [
21+
"admin/json_editor.js",
22+
]
23+
24+
css = {
25+
"all": ["admin/json_editor.css"],
26+
}
27+
28+
def __init__(self, attrs: dict | None = None) -> None:
29+
super().__init__(attrs={"class": "app-json-editor", "cols": "40"})

src/core/conf/installed_apps.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"django_filters",
1616
"drf_spectacular",
1717
"drf_spectacular_sidecar",
18-
"prettyjson",
1918
"rest_framework",
2019
"rest_framework.authtoken",
2120
# "silk",
File renamed without changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.app-json-editor--error {
2+
border-color: var(--error-fg);
3+
color: var(--error-fg);
4+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(function() {
2+
function validateJSON(widget) {
3+
try {
4+
JSON.parse(widget.value);
5+
widget.classList.remove('app-json-editor--error');
6+
return true;
7+
} catch (error) {
8+
widget.classList.add('app-json-editor--error');
9+
return false;
10+
}
11+
}
12+
13+
document.addEventListener('DOMContentLoaded', function() {
14+
const widgets = document.querySelectorAll('.app-json-editor');
15+
widgets.forEach((widget) => {
16+
// Format initial JSON
17+
try {
18+
const parsed = JSON.parse(widget.value);
19+
const formatted = JSON.stringify(parsed, null, 4);
20+
widget.value = formatted;
21+
widget.classList.remove('errornote');
22+
} catch (error) {
23+
widget.classList.add('errornote');
24+
}
25+
26+
// Validate on every keypress
27+
widget.addEventListener('input', function() {
28+
validateJSON(widget);
29+
});
30+
});
31+
});
32+
})();

src/core/static/prettyjson.css

Lines changed: 0 additions & 10 deletions
This file was deleted.

uv.lock

Lines changed: 0 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)