Skip to content

Commit

Permalink
better handling of s3 connections
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisclark committed Jun 20, 2024
1 parent ef2941b commit fe95ebc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
6 changes: 6 additions & 0 deletions explorer/ee/db_connections/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.views import View
from django.http import JsonResponse
Expand All @@ -20,6 +21,9 @@
from explorer.ee.db_connections.utils import create_django_style_connection


logger = logging.getLogger(__name__)


class UploadDbView(PermissionRequiredMixin, View):

permission_required = "connections_permission"
Expand All @@ -39,6 +43,7 @@ def post(self, request):
try:
f_bytes = pandas_to_sqlite(df)
except Exception as e: # noqa
logger.exception(f"Exception while parsing file {f_name}: {e}")
return JsonResponse({"error": "Error while parsing the file."}, status=400)

f_name = f_name.replace("csv", "db")
Expand All @@ -47,6 +52,7 @@ def post(self, request):
s3_path = f"user_dbs/user_{request.user.id}/{f_name}"
upload_sqlite(f_bytes, s3_path)
except Exception as e: # noqa
logger.exception(f"Exception while uploading file {f_name}: {e}")
return JsonResponse({"error": "Error while uploading file."}, status=400)

create_connection_for_uploaded_sqlite(f_name, request.user.id, s3_path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ <h2>Delete Database Connection</h2>
<p>Are you sure you want to delete "{{ object }}"?</p>
<form method="post">
{% csrf_token %}
<button type="submit" class="btn btn-danger">Delete</button>
<a href="{% url 'explorer_connections' %}" class="btn btn-secondary">Cancel</a>
<button type="submit" class="btn btn-secondary">Delete</button>
<a href="{% url 'explorer_connections' %}" class="btn btn-info">Cancel</a>
</form>
</div>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ <h2>Connection Details</h2>
</tr>
{% endif %}
</table>
{% if not object.is_upload %}
<a href="{% url 'explorer_connection_update' object.pk %}" class="btn btn-warning">Edit</a>
{% else %}
<span class="text-info-emphasis">The source of this connection is an uploaded file.</span>
{% endif %}
<a href="{% url 'explorer_connection_update' object.pk %}" class="btn btn-info">Edit</a>
</div>
{% endblock %}
3 changes: 3 additions & 0 deletions explorer/templates/connections/database_connection_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
{% block sql_explorer_content %}
<div class="container mt-5">
<h2>{% if object %}Edit{% else %}Create New{% endif %} Connection</h2>
{% if object.is_upload %}
<span class="text-danger">The source of this connection is an uploaded file. In all likelihood you should not be editing it.</span>
{% endif %}
<form method="post" id="db-connection-form">
{% csrf_token %}
{{ form.as_p }}
Expand Down
12 changes: 7 additions & 5 deletions explorer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,13 @@ def get_s3_bucket():
region_name=app_settings.S3_REGION
)

kwargs = {
"aws_access_key_id": app_settings.S3_ACCESS_KEY,
"aws_secret_access_key": app_settings.S3_SECRET_KEY,
"config": config
}
kwargs = {"config": config}

# If these are set, use them. Otherwise, boto will use its built-in mechanisms
# to provide authentication.
if app_settings.S3_ACCESS_KEY and app_settings.S3_SECRET_KEY:
kwargs["aws_access_key_id"] = app_settings.S3_ACCESS_KEY
kwargs["aws_secret_access_key"] = app_settings.S3_SECRET_KEY

if app_settings.S3_ENDPOINT_URL:
kwargs["endpoint_url"] = app_settings.S3_ENDPOINT_URL
Expand Down

0 comments on commit fe95ebc

Please sign in to comment.