forked from open-webui/open-webui
feat: csv user import frontend
This commit is contained in:
parent
e5703a588f
commit
bd3b5f1edb
4 changed files with 188 additions and 54 deletions
|
@ -1,12 +1,14 @@
|
|||
import logging
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi import Request, UploadFile, File
|
||||
from fastapi import Depends, HTTPException, status
|
||||
|
||||
from fastapi import APIRouter
|
||||
from pydantic import BaseModel
|
||||
import re
|
||||
import uuid
|
||||
import csv
|
||||
|
||||
|
||||
from apps.web.models.auths import (
|
||||
SigninForm,
|
||||
|
@ -212,7 +214,7 @@ async def signup(request: Request, form_data: SignupForm):
|
|||
|
||||
|
||||
@router.post("/add", response_model=SigninResponse)
|
||||
async def signup(form_data: AddUserForm, user=Depends(get_admin_user)):
|
||||
async def add_user(form_data: AddUserForm, user=Depends(get_admin_user)):
|
||||
|
||||
if not validate_email_format(form_data.email.lower()):
|
||||
raise HTTPException(
|
||||
|
@ -251,6 +253,76 @@ async def signup(form_data: AddUserForm, user=Depends(get_admin_user)):
|
|||
raise HTTPException(500, detail=ERROR_MESSAGES.DEFAULT(err))
|
||||
|
||||
|
||||
@router.post("/add/import", response_model=SigninResponse)
|
||||
async def add_user_csv_import(
|
||||
file: UploadFile = File(...), user=Depends(get_admin_user)
|
||||
):
|
||||
try:
|
||||
|
||||
# Check if the file is a CSV file
|
||||
if file.filename.endswith(".csv"):
|
||||
# Read the contents of the CSV file
|
||||
contents = await file.read()
|
||||
|
||||
# Decode the contents from bytes to string
|
||||
decoded_content = contents.decode("utf-8")
|
||||
|
||||
# Split the CSV content into lines
|
||||
csv_lines = decoded_content.split("\n")
|
||||
|
||||
# Parse CSV
|
||||
csv_data = []
|
||||
csv_reader = csv.reader(csv_lines)
|
||||
for row in csv_reader:
|
||||
csv_data.append(row)
|
||||
|
||||
# Print the CSV data
|
||||
for row in csv_data:
|
||||
print(row)
|
||||
|
||||
return {"message": "CSV file uploaded successfully."}
|
||||
else:
|
||||
raise "File must be a CSV."
|
||||
except Exception as err:
|
||||
raise HTTPException(500, detail=ERROR_MESSAGES.DEFAULT(err))
|
||||
|
||||
# if not validate_email_format(form_data.email.lower()):
|
||||
# raise HTTPException(
|
||||
# status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.INVALID_EMAIL_FORMAT
|
||||
# )
|
||||
|
||||
# if Users.get_user_by_email(form_data.email.lower()):
|
||||
# raise HTTPException(400, detail=ERROR_MESSAGES.EMAIL_TAKEN)
|
||||
|
||||
# try:
|
||||
|
||||
# print(form_data)
|
||||
# hashed = get_password_hash(form_data.password)
|
||||
# user = Auths.insert_new_auth(
|
||||
# form_data.email.lower(),
|
||||
# hashed,
|
||||
# form_data.name,
|
||||
# form_data.profile_image_url,
|
||||
# form_data.role,
|
||||
# )
|
||||
|
||||
# if user:
|
||||
# token = create_token(data={"id": user.id})
|
||||
# return {
|
||||
# "token": token,
|
||||
# "token_type": "Bearer",
|
||||
# "id": user.id,
|
||||
# "email": user.email,
|
||||
# "name": user.name,
|
||||
# "role": user.role,
|
||||
# "profile_image_url": user.profile_image_url,
|
||||
# }
|
||||
# else:
|
||||
# raise HTTPException(500, detail=ERROR_MESSAGES.CREATE_USER_ERROR)
|
||||
# except Exception as err:
|
||||
# raise HTTPException(500, detail=ERROR_MESSAGES.DEFAULT(err))
|
||||
|
||||
|
||||
############################
|
||||
# ToggleSignUp
|
||||
############################
|
||||
|
|
1
backend/static/user-import.csv
Normal file
1
backend/static/user-import.csv
Normal file
|
@ -0,0 +1 @@
|
|||
Name,Email,Password,Role
|
|
Loading…
Add table
Add a link
Reference in a new issue