Skip to content

Commit

Permalink
ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisclark committed Jul 8, 2024
1 parent a0ff36a commit 918684d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions explorer/ee/db_connections/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
from django.core.exceptions import ValidationError


# This is very annoying, but Django renders the literal string 'null' in the form when displaying JSON
# via a TextInput widget. So this custom widget prevents that.
class JSONTextInput(forms.TextInput):
def render(self, name, value, attrs=None, renderer=None):
if value in (None, '', 'null'):
value = ''
if value in (None, "", "null"):
value = ""
elif isinstance(value, dict):
value = json.dumps(value)
return super().render(name, value, attrs, renderer)

def value_from_datadict(self, data, files, name):
value = data.get(name)
if value in (None, '', 'null'):
if value in (None, "", "null"):
return None
try:
return json.loads(value)
except (TypeError, ValueError):
raise ValidationError("Enter a valid JSON")
except (TypeError, ValueError) as ex:
raise ValidationError("Enter a valid JSON") from ex


class DatabaseConnectionForm(forms.ModelForm):
Expand Down
4 changes: 2 additions & 2 deletions explorer/tests/test_db_connection_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ def test_create_django_style_connection_with_extras(self, mock_load_backend):
mock_load_backend.assert_called_once_with("django.db.backends.postgresql")
mock_backend.DatabaseWrapper.assert_called_once()
args, kwargs = mock_backend.DatabaseWrapper.call_args
self.assertEqual(args[0]['sslmode'], "require")
self.assertEqual(args[0]['connect_timeout'], 10)
self.assertEqual(args[0]["sslmode"], "require")
self.assertEqual(args[0]["connect_timeout"], 10)


@skipIf(not EXPLORER_USER_UPLOADS_ENABLED, "User uploads not enabled")
Expand Down

0 comments on commit 918684d

Please sign in to comment.