Fixes: 1) Correct `urlsafe_base64_encode` decoding logic in tests; 2) Fix queryset access issues in resolvers; 3) Address missing or incorrect imports across multiple files. Extra: Improve code readability with consistent naming and formatting; Add `# noinspection` annotations to suppress IDE warnings; Update `pyproject.toml` to exclude `drf.py` in MyPy checks.
39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
import json
|
|
|
|
from django import forms
|
|
|
|
|
|
class JSONTableWidget(forms.Widget):
|
|
template_name = "json_table_widget.html"
|
|
|
|
def format_value(self, value):
|
|
if isinstance(value, dict):
|
|
return value
|
|
try:
|
|
if isinstance(value, str):
|
|
value = json.loads(value)
|
|
except json.JSONDecodeError:
|
|
value = {}
|
|
return value
|
|
|
|
def render(self, name, value, attrs=None, renderer=None):
|
|
value = self.format_value(value)
|
|
return super().render(name, value, attrs, renderer)
|
|
|
|
# noinspection PyUnresolvedReferences
|
|
def value_from_datadict(self, data, files, name):
|
|
json_data = {}
|
|
|
|
try:
|
|
keys = data.getlist(f"{name}_key")
|
|
values = data.getlist(f"{name}_value")
|
|
for key, value in zip(keys, values):
|
|
if key.strip():
|
|
try:
|
|
json_data[key] = json.loads(value)
|
|
except (json.JSONDecodeError, ValueError):
|
|
json_data[key] = value
|
|
except TypeError:
|
|
pass
|
|
|
|
return None if not json_data else json.dumps(json_data)
|