-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate upload UI from regular 'add connection' UI Good cache busting and detection of file changes Ability to force a cache refresh and file refresh of data sets
- Loading branch information
1 parent
c2ac5c6
commit 5274f78
Showing
27 changed files
with
589 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,43 @@ | ||
import os | ||
from io import BytesIO | ||
|
||
from explorer.utils import secure_filename | ||
from explorer.ee.db_connections.type_infer import get_parser | ||
from explorer.ee.db_connections.utils import pandas_to_sqlite | ||
from explorer.ee.db_connections.utils import pandas_to_sqlite, uploaded_db_local_path | ||
|
||
|
||
def parse_to_sqlite(file) -> (BytesIO, str): | ||
f_name = file.name | ||
f_bytes = file.read() | ||
def get_names(file, append_conn=None, user_id=None): | ||
s_filename = secure_filename(file.name) | ||
table_name, _ = os.path.splitext(s_filename) | ||
|
||
# f_name represents the filename of both the sqlite DB on S3, and on the local filesystem. | ||
# If we are appending to an existing data source, then we re-use the same name. | ||
# New connections get a new database name. | ||
if append_conn: | ||
f_name = os.path.basename(append_conn.name) | ||
else: | ||
f_name = f"{table_name}_{user_id}.db" | ||
|
||
return table_name, f_name | ||
|
||
|
||
def parse_to_sqlite(file, append_conn=None, user_id=None) -> (BytesIO, str): | ||
|
||
table_name, f_name = get_names(file, append_conn, user_id) | ||
|
||
# When appending, make sure the database exists locally so that we can write to it | ||
if append_conn: | ||
append_conn.download_sqlite_if_needed() | ||
|
||
df_parser = get_parser(file) | ||
if df_parser: | ||
df = df_parser(f_bytes) | ||
try: | ||
f_bytes = pandas_to_sqlite(df, local_path=f"{f_name}_tmp_local.db") | ||
df = df_parser(file.read()) | ||
local_path = uploaded_db_local_path(f_name) | ||
f_bytes = pandas_to_sqlite(df, table_name, local_path) | ||
except Exception as e: # noqa | ||
raise ValueError(f"Error while parsing {f_name}: {e}") from e | ||
# replace the previous extension with .db, as it is now a sqlite file | ||
name, _ = os.path.splitext(f_name) | ||
f_name = f"{name}.db" | ||
else: | ||
return BytesIO(f_bytes), f_name # if it's a SQLite file already, simply cough it up as a BytesIO object | ||
# If it's a SQLite file already, simply cough it up as a BytesIO object | ||
return BytesIO(file.read()), f_name | ||
return f_bytes, f_name | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.