-
-
Notifications
You must be signed in to change notification settings - Fork 315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added functionality to import perfect Excel #3059
Changes from 7 commits
8e99e07
4b82e9f
bb303bd
be2740b
9bd6bca
5603324
7b69371
411b764
d8a4149
bcc5325
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,45 @@ | ||||||||||
import pandas | ||||||||||
|
||||||||||
from db.tables.operations.alter import update_pk_sequence_to_latest | ||||||||||
from mathesar.database.base import create_mathesar_engine | ||||||||||
from db.records.operations.insert import insert_records_from_excel | ||||||||||
from db.tables.operations.create import create_string_column_table | ||||||||||
from db.tables.operations.drop import drop_table | ||||||||||
from mathesar.imports.utils import get_alternate_column_names, process_column_names | ||||||||||
from psycopg2.errors import IntegrityError, DataError | ||||||||||
|
||||||||||
from mathesar.state import reset_reflection | ||||||||||
|
||||||||||
|
||||||||||
def insert_data_from_excel_data_file(name, schema, column_names, engine, comment, dataframe): | ||||||||||
table = create_string_column_table( | ||||||||||
name=name, | ||||||||||
schema=schema.name, | ||||||||||
column_names=column_names, | ||||||||||
engine=engine, | ||||||||||
comment=comment, | ||||||||||
) | ||||||||||
|
||||||||||
insert_records_from_excel( | ||||||||||
table, | ||||||||||
engine, | ||||||||||
dataframe, | ||||||||||
) | ||||||||||
return table | ||||||||||
|
||||||||||
|
||||||||||
def create_db_table_from_excel_data_file(data_file, name, schema, comment=None): | ||||||||||
db_name = schema.database.name | ||||||||||
engine = create_mathesar_engine(db_name) | ||||||||||
dataframe = pandas.read_excel(data_file.file.path) | ||||||||||
column_names = process_column_names(dataframe.columns) | ||||||||||
try: | ||||||||||
table = insert_data_from_excel_data_file(name, schema, column_names, engine, comment, dataframe) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be renamed to
Suggested change
because, you're inserting from a dataframe here, it coming from excel is irrelevant (correct me if I'm wrong), or even
Suggested change
because, you refered to data as |
||||||||||
update_pk_sequence_to_latest(engine, table) | ||||||||||
except (IntegrityError, DataError): | ||||||||||
drop_table(name=name, schema=schema.name, engine=engine) | ||||||||||
column_names_alt = get_alternate_column_names(column_names) | ||||||||||
table = insert_data_from_excel_data_file(name, schema, column_names_alt, engine, comment, dataframe) | ||||||||||
|
||||||||||
reset_reflection(db_name=db_name) | ||||||||||
return table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice and clear 👍