11# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors
22# For license information, please see license.txt
33
4- import os
5-
64import frappe
75from frappe .defaults import get_user_default , set_user_default
86from frappe .handler import is_valid_http_method , is_whitelisted
97from frappe .monitor import add_data_to_monitor
108
119from insights .api .shared import is_public
1210from insights .decorators import insights_whitelist , validate_type
13- from insights .insights .doctype .insights_data_source_v3 .connectors .duckdb import (
14- get_duckdb_connection ,
15- )
11+ from insights .insights .doctype .insights_data_source_v3 .connectors .duckdb import get_duckdb_connection
1612from insights .insights .doctype .insights_data_source_v3 .ibis_utils import (
1713 get_columns_from_schema ,
1814)
@@ -126,30 +122,23 @@ def get_file_data(filename: str):
126122
127123 create_uploads_if_not_exists ()
128124 ds = frappe .get_doc ("Insights Data Source v3" , "uploads" )
129- private_folder = frappe .utils .get_files_path (is_private = 1 )
130- private_folder = os .path .realpath (private_folder )
131- db = get_duckdb_connection (ds , read_only = True , allowed_dir = private_folder )
132- try :
133- if ext in ["xlsx" ]:
134- table = db .read_xlsx (file_path )
135- elif ext in ["json" , "jsonl" ]:
136- table = db .read_json (file_path )
137- else :
138- table = db .read_csv (file_path , table_name = file_name )
125+ db = get_duckdb_connection (ds , read_only = False , allow_private_files = True )
139126
127+ try :
128+ table = _read_uploaded_table (db , file_path , ext )
140129 columns = get_columns_from_schema (table .schema ())
141130 rows = table .head (50 ).execute ().fillna ("" ).to_dict (orient = "records" )
142131 row_count = table .count ().execute ()
132+
133+ return {
134+ "tablename" : file_name ,
135+ "rows" : rows ,
136+ "columns" : columns ,
137+ "total_rows" : int (row_count ),
138+ }
143139 finally :
144140 db .disconnect ()
145141
146- return {
147- "tablename" : file_name ,
148- "rows" : rows ,
149- "columns" : columns ,
150- "total_rows" : int (row_count ),
151- }
152-
153142
154143@insights_whitelist ()
155144@validate_type
@@ -162,33 +151,46 @@ def import_csv_data(filename: str, tablename: str = ""):
162151
163152 create_uploads_if_not_exists ()
164153 ds = frappe .get_doc ("Insights Data Source v3" , "uploads" )
165- private_folder = os . path . realpath ( frappe . utils . get_files_path ( is_private = 1 ) )
154+ db = get_duckdb_connection ( ds , read_only = False , allow_private_files = True )
166155
167- db = get_duckdb_connection (ds , read_only = False , allowed_dir = private_folder )
168156 try :
169- if ext in ["xlsx" ]:
170- table = db .read_xlsx (file_path )
171- elif ext in ["json" , "jsonl" ]:
172- table = db .read_json (file_path )
173- else :
174- table = db .read_csv (file_path , table_name = table_name )
157+ table = _read_uploaded_table (db , file_path , ext )
175158 db .create_table (table_name , table , overwrite = True )
159+ except frappe .ValidationError :
160+ raise
176161 except Exception as e :
177162 frappe .log_error (e )
178- if ext in ["xlsx" ]:
163+ frappe .throw ("Failed to import uploaded file data into Insights uploads table. Please try again." )
164+ finally :
165+ db .disconnect ()
166+
167+ InsightsTablev3 .bulk_create (ds .name , [table_name ])
168+
169+
170+ def _read_uploaded_table (db , file_path : str , ext : str ):
171+ try :
172+ if ext == "xlsx" :
173+ return db .read_xlsx (file_path )
174+
175+ if ext in ["json" , "jsonl" ]:
176+ return db .read_json (file_path )
177+
178+ return db .read_csv (file_path )
179+
180+ except Exception as e :
181+ frappe .log_error (e )
182+
183+ if ext == "xlsx" :
179184 frappe .throw (
180185 "Failed to read Excel data from uploaded file. Please ensure the file is a valid Excel format and try again."
181186 )
182- elif ext in ["json" , "jsonl" ]:
187+
188+ if ext in ["json" , "jsonl" ]:
183189 frappe .throw (
184190 "Failed to read JSON data from uploaded file. Please ensure the file is a valid JSON or JSONL format and try again."
185191 )
186- else :
187- frappe .throw ("Failed to read CSV data from uploaded file. Please try again." )
188- finally :
189- db .disconnect ()
190192
191- InsightsTablev3 . bulk_create ( ds . name , [ table_name ] )
193+ frappe . throw ( "Failed to read CSV data from uploaded file. Please try again." )
192194
193195
194196@frappe .whitelist (allow_guest = True )
0 commit comments