Skip to content

Commit

Permalink
forms working
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisclark committed Aug 13, 2024
1 parent dfb9a1f commit 7b00057
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
10 changes: 5 additions & 5 deletions explorer/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ class QueryForm(ModelForm):

sql = SqlField()
snapshot = BooleanField(widget=CheckboxInput, required=False)
connection = CharField(widget=Select, required=False)
database_connection = CharField(widget=Select, required=False)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["connection"].widget.choices = self.connections
self.fields["database_connection"].widget.choices = self.connections
if not self.instance.database_connection:
self.initial["database_connection"] = EXPLORER_DEFAULT_CONNECTION
self.fields["connection"].widget.attrs["class"] = "form-select"
self.fields["database_connection"].widget.attrs["class"] = "form-select"

def clean(self):
# Don't overwrite created_by_user
Expand All @@ -55,8 +55,8 @@ def created_at_time(self):

@property
def connections(self):
return [(c.alias, c.id) for c in DatabaseConnection.objects.all()]
return [(c.id, c.alias) for c in DatabaseConnection.objects.all()]

class Meta:
model = Query
fields = ["title", "sql", "description", "snapshot", "connection"]
fields = ["title", "sql", "description", "snapshot", "database_connection"]
2 changes: 1 addition & 1 deletion explorer/src/js/codemirror-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function fetchAndShowSchema(view) {
if (schema.hasOwnProperty(tableName)) {
formattedSchema = JSON.stringify(schema[tableName], null, 2);
} else {
formattedSchema = `Table '${tableName}' not found in schema for connection '${conn}'`;
formattedSchema = `Table '${tableName}' not found in schema for connection`;
}
displaySchemaTooltip(view, formattedSchema);
});
Expand Down
2 changes: 1 addition & 1 deletion explorer/src/js/schemaService.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ export const SchemaSvc = {
};

export function getConnElement() {
return document.querySelector('#id_connection');
return document.querySelector('#id_database_connection');
}
4 changes: 2 additions & 2 deletions explorer/templates/explorer/query.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ <h2>
</div>
{% if form.connections|length > 1 and can_change %}
<div class="mb-3 form-floating">
{{ form.connection }}
<label for="id_connection" class="form-label">{% translate "Connection" %}</label>
{{ form.database_connection }}
<label for="id_database_connection" class="form-label">{% translate "Connection" %}</label>
</div>
{% else %}
{# still need to submit the connection, just hide the UI element #}
Expand Down
10 changes: 5 additions & 5 deletions explorer/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from explorer.models import MSG_FAILED_BLACKLIST, Query, QueryFavorite, QueryLog, DatabaseConnection
from explorer.tests.factories import QueryLogFactory, SimpleQueryFactory
from explorer.utils import user_can_see_query
from explorer.ee.db_connections.utils import default_db_connection_id
from explorer.ee.db_connections.utils import default_db_connection


def reload_app_settings():
Expand Down Expand Up @@ -652,28 +652,28 @@ def setUp(self):

def test_returns_schema_contents(self):
resp = self.client.get(
reverse("explorer_schema", kwargs={"connection": default_db_connection_id()})
reverse("explorer_schema", kwargs={"connection": default_db_connection().alias})
)
self.assertContains(resp, "explorer_query")
self.assertTemplateUsed(resp, "explorer/schema.html")

def test_returns_schema_contents_json(self):
resp = self.client.get(
reverse("explorer_schema_json", kwargs={"connection": default_db_connection_id()})
reverse("explorer_schema_json", kwargs={"connection": default_db_connection().alias})
)
self.assertContains(resp, "explorer_query")
self.assertEqual(resp.headers["Content-Type"], "application/json")

def test_returns_404_if_conn_doesnt_exist(self):
resp = self.client.get(
reverse("explorer_schema", kwargs={"connection": 4})
reverse("explorer_schema", kwargs={"connection": "bananas"})
)
self.assertEqual(resp.status_code, 404)

def test_admin_required(self):
self.client.logout()
resp = self.client.get(
reverse("explorer_schema", kwargs={"connection": 1})
reverse("explorer_schema", kwargs={"connection": default_db_connection().alias})
)
self.assertTemplateUsed(resp, "admin/login.html")

Expand Down
4 changes: 2 additions & 2 deletions explorer/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
path("new/", CreateQueryView.as_view(), name="query_create"),
path("play/", PlayQueryView.as_view(), name="explorer_playground"),
path(
"schema/<int:connection>", SchemaView.as_view(),
"schema/<path:connection>", SchemaView.as_view(),
name="explorer_schema"
),
path(
"schema.json/<int:connection>", SchemaJsonView.as_view(),
"schema.json/<path:connection>", SchemaJsonView.as_view(),
name="explorer_schema_json"
),
path("logs/", ListQueryLogView.as_view(), name="explorer_logs"),
Expand Down

0 comments on commit 7b00057

Please sign in to comment.