Skip to content

Commit

Permalink
lots of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisclark committed Aug 13, 2024
1 parent 7b00057 commit 9717c38
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 17 deletions.
9 changes: 0 additions & 9 deletions explorer/ee/db_connections/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
create_connection_for_uploaded_sqlite
)
from explorer.ee.db_connections.create_sqlite import parse_to_sqlite
from explorer import app_settings
from explorer.schema import clear_schema_cache
from explorer.app_settings import EXPLORER_MAX_UPLOAD_SIZE
from explorer.ee.db_connections.forms import DatabaseConnectionForm
Expand Down Expand Up @@ -86,14 +85,6 @@ class DatabaseConnectionsListView(PermissionRequiredMixin, ExplorerContextMixin,
template_name = "connections/connections.html"
model = DatabaseConnection

def get_queryset(self):
qs = list(DatabaseConnection.objects.all())
for _, alias in app_settings.EXPLORER_CONNECTIONS.items():
django_conn = DatabaseConnection.objects.get(alias=alias).as_django_connection()
if django_conn:
qs.append(django_conn)
return qs


class DatabaseConnectionDetailView(PermissionRequiredMixin, ExplorerContextMixin, DetailView):
permission_required = "connections_permission"
Expand Down
9 changes: 9 additions & 0 deletions explorer/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def clean(self):
self.instance.created_by_user
return super().clean()

def clean_database_connection(self):
connection_id = self.cleaned_data.get("database_connection")
if connection_id:
try:
return DatabaseConnection.objects.get(id=connection_id)
except DatabaseConnection.DoesNotExist as e:
raise ValidationError("Invalid database connection selected.") from e
return None

@property
def created_at_time(self):
return self.instance.created_at.strftime("%Y-%m-%d")
Expand Down
2 changes: 1 addition & 1 deletion explorer/src/js/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function submitAssistantAsk() {

const data = {
sql: window.editor?.state.doc.toString() ?? null,
connection: document.getElementById("id_connection")?.value ?? null,
connection: document.getElementById("id_database_connection")?.value ?? null,
assistant_request: document.getElementById("id_assistant_input")?.value ?? null,
selected_tables: selectedTables,
db_error: getErrorMessage()
Expand Down
2 changes: 1 addition & 1 deletion explorer/src/js/explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function selectConnection() {
var connectionId = urlParams.get('connection');

if (connectionId) {
var connectionSelect = document.getElementById('id_connection');
var connectionSelect = document.getElementById('id_database_connection');
if (connectionSelect) {
connectionSelect.value = connectionId;
}
Expand Down
2 changes: 1 addition & 1 deletion explorer/templates/connections/connections.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ <h3>Connections</h3>
<td>{{ connection.name }}</td>
<td>{{ connection.get_engine_display }}{% if connection.is_upload %} (uploaded){% endif %}</td>
<td>
<a href="../play/?connection={{ connection.alias }}" class="px-2"><i class="bi-arrow-up-right-square small me-1"></i>Query</a>
<a href="../play/?connection={{ connection.id }}" class="px-2"><i class="bi-arrow-up-right-square small me-1"></i>Query</a>
{% if connection.id %}
<a href="{% url 'explorer_connection_update' connection.pk %}" class="px-2"><i class="bi-pencil-square"></i></a>
<a href="{% url 'explorer_connection_delete' connection.pk %}"><i class="bi-trash"></i></a>
Expand Down
4 changes: 1 addition & 3 deletions explorer/templates/connections/database_connection_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h2>{% if object %}Edit{% else %}Create New{% endif %} Connection</h2>
</div>
<div class="mb-3 form-floating">
{{ form.name }}
<label for="id_name" class="form-label">Name</label>
<label for="id_name" class="form-label">Database Name</label>
<span class="form-text text-muted">Required. The name of the database itself.</span>
</div>
<div class="mb-3 form-floating">
Expand All @@ -33,12 +33,10 @@ <h2>{% if object %}Edit{% else %}Create New{% endif %} Connection</h2>
<div class="mb-3 form-floating">
{{ form.host }}
<label for="id_host" class="form-label">Host</label>
<span class="form-text text-muted">Required.</span>
</div>
<div class="mb-3 form-floating">
{{ form.port }}
<label for="id_port" class="form-label">Port</label>
<span class="form-text text-muted">Required.</span>
</div>
<div class="mb-3 form-floating">
{{ form.extras }}
Expand Down
4 changes: 2 additions & 2 deletions explorer/templates/explorer/play.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ <h2>{% translate "Playground" %}</h2>
{{ form.non_field_errors }}
{% 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
22 changes: 22 additions & 0 deletions explorer/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from explorer.forms import QueryForm
from explorer.tests.factories import SimpleQueryFactory
from explorer.ee.db_connections.utils import default_db_connection_id


class TestFormValidation(TestCase):
Expand All @@ -28,3 +29,24 @@ def test_form_fails_blacklist(self):
q.params = {}
form = QueryForm(model_to_dict(q))
self.assertFalse(form.is_valid())


class QueryFormTestCase(TestCase):

def test_valid_form_submission(self):
form_data = {
"title": "Test Query",
"sql": "SELECT * FROM table",
"description": "A test query description",
"snapshot": False,
"database_connection": str(default_db_connection_id()),
}

form = QueryForm(data=form_data)
self.assertTrue(form.is_valid(), msg=form.errors)
query = form.save()

# Verify that the Query instance was created and is correctly linked to the DatabaseConnection
self.assertEqual(query.database_connection_id, default_db_connection_id())
self.assertEqual(query.title, form_data["title"])
self.assertEqual(query.sql, form_data["sql"])

0 comments on commit 9717c38

Please sign in to comment.