From 918684d5a4af7fdb161d26eb23853d79227afbaf Mon Sep 17 00:00:00 2001 From: Chris Clark Date: Sun, 7 Jul 2024 21:38:17 -0400 Subject: [PATCH] ruff --- explorer/ee/db_connections/forms.py | 12 +++++++----- explorer/tests/test_db_connection_utils.py | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/explorer/ee/db_connections/forms.py b/explorer/ee/db_connections/forms.py index 4c054408..1c455099 100644 --- a/explorer/ee/db_connections/forms.py +++ b/explorer/ee/db_connections/forms.py @@ -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): diff --git a/explorer/tests/test_db_connection_utils.py b/explorer/tests/test_db_connection_utils.py index 2f6bf866..3b81ed95 100644 --- a/explorer/tests/test_db_connection_utils.py +++ b/explorer/tests/test_db_connection_utils.py @@ -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")