MCPcopy Index your code
hub / github.com/dataease/SQLBot / upload_excel

Function upload_excel

backend/apps/data_training/api/data_training.py:164–271  ·  view source on GitHub ↗
(trans: Trans, current_user: CurrentUser, file: UploadFile = File(...))

Source from the content-addressed store, hash-verified

162@system_log(LogConfig(operation_type=OperationType.IMPORT, module=OperationModules.DATA_TRAINING))
163@require_permissions(permission=SqlbotPermission(role=['ws_admin']))
164async def upload_excel(trans: Trans, current_user: CurrentUser, file: UploadFile = File(...)):
165 ALLOWED_EXTENSIONS = {"xlsx", "xls"}
166 if not file.filename.lower().endswith(tuple(ALLOWED_EXTENSIONS)):
167 raise HTTPException(400, "Only support .xlsx/.xls")
168
169 os.makedirs(path, exist_ok=True)
170 base_filename = f"{file.filename.split('.')[0]}_{hashlib.sha256(uuid.uuid4().bytes).hexdigest()[:10]}"
171 filename = f"{base_filename}.{file.filename.split('.')[1]}"
172 save_path = os.path.join(path, filename)
173 with open(save_path, "wb") as f:
174 f.write(await file.read())
175
176 oid = current_user.oid
177
178 use_cols = [0, 1, 2] # 问题, 描述, 数据源名称
179 # 根据oid确定要读取的列
180 if oid == 1:
181 use_cols = [0, 1, 2, 3] # 问题, 描述, 数据源名称, 高级应用名称
182
183 def inner():
184
185 session = session_maker()
186
187 sheet_names = pd.ExcelFile(save_path).sheet_names
188
189 import_data = []
190
191 for sheet_name in sheet_names:
192
193 if get_excel_column_count(save_path, sheet_name) < len(use_cols):
194 raise Exception(trans("i18n_excel_import.col_num_not_match"))
195
196 df = pd.read_excel(
197 save_path,
198 sheet_name=sheet_name,
199 engine='calamine',
200 header=0,
201 usecols=use_cols,
202 dtype=str
203 ).fillna("")
204
205 for index, row in df.iterrows():
206 # 跳过空行
207 if row.isnull().all():
208 continue
209
210 question = row[0].strip() if pd.notna(row[0]) and row[0].strip() else ''
211 description = row[1].strip() if pd.notna(row[1]) and row[1].strip() else ''
212 datasource_name = row[2].strip() if pd.notna(row[2]) and row[2].strip() else ''
213
214 advanced_application_name = ''
215 if oid == 1 and len(row) > 3:
216 advanced_application_name = row[3].strip() if pd.notna(row[3]) and row[3].strip() else ''
217
218 if oid == 1:
219 import_data.append(
220 DataTrainingInfo(oid=oid, question=question, description=description,
221 datasource_name=datasource_name,

Callers

nothing calls this directly

Calls 2

openFunction · 0.85
readMethod · 0.80

Tested by

no test coverage detected