Skip to content

Commit

Permalink
fix error on nested json in command. fix custom command type
Browse files Browse the repository at this point in the history
  • Loading branch information
amyasnikov committed Feb 18, 2025
1 parent 968718b commit 2cd9860
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
6 changes: 1 addition & 5 deletions validity/forms/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,8 @@ def __init__(self, *args, **kwargs):
self.initial |= self.subform.data

def save(self, commit=True):
json_field = {}
if self.subform:
for name in self.fields:
if name in self.subform.fields:
json_field[name] = self.cleaned_data[name]
self.instance.subform_json = json_field
self.instance.subform_json = self.subform.data_for_saving
return super().save(commit)

def clean(self):
Expand Down
46 changes: 40 additions & 6 deletions validity/subforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
2. Validate JSON Field
"""

import json
import textwrap
import xml.etree.ElementTree as ET

from django import forms
from django.forms.widgets import Textarea
from django.utils.translation import gettext_lazy as _

from validity.choices import JSONAPIMethodChoices
Expand All @@ -29,7 +31,7 @@ class SensitiveMixin:
def _sensitive_value(cls, field):
if field.name in cls.sensitive_fields:
return cls.placeholder
return field.value()
return json.dumps(field.data) if isinstance(data := field.data, (dict, list)) else data

def rendered_parameters(self):
for field in self:
Expand All @@ -48,6 +50,36 @@ def clean(self):
raise forms.ValidationError(_("Only these keys are allowed: %(fields)s"), params={"fields": allowed_fields})
return self.cleaned_data

@property
def data_for_saving(self):
return self.cleaned_data


class PlainSubform(BaseSubform):
"""
Displays all the params inside one JSONField called "params"
params field MUST be defined in a sublcass
"""

def __init__(self, data=None, *args, **kwargs):
if data is not None:
if data.keys() != {"params"}:
data = type(data)({"params": dict(data)})
super().__init__(data, *args, **kwargs)

def clean_params(self):
params = self.cleaned_data["params"]
if not isinstance(params, dict):
raise forms.ValidationError("Value must be JSON object")
return params

@property
def data_for_saving(self):
return self.cleaned_data["params"]

def rendered_parameters(self):
yield from self.data["params"].items()


# Command Subforms

Expand Down Expand Up @@ -85,11 +117,13 @@ def clean_rpc(self):
return rpc


class CustomCommandForm(BaseSubform):
params = forms.JSONField(label=_("Command Parameters"))

def clean(self):
return self.cleaned_data["params"]
class CustomCommandForm(PlainSubform):
params = forms.JSONField(
label=_("Command Parameters"),
help_text=_("JSON-encoded params"),
widget=Textarea(attrs={"style": "font-family:monospace"}),
initial=dict,
)


# Serializer Subforms
Expand Down

0 comments on commit 2cd9860

Please sign in to comment.