1

i have this endpoint where basically will call the function that will create the excel file

@router.post("/users/export",response_model=schemas.Mesage, status_code=201)
async def get_all_users_from_dashboard(
    *,
    db: Session = Depends(deps.get_db),
    s3: BaseClient = Depends(s3_auth),
    background_tasks: BackgroundTasks,
    current_user: models.User = Depends(deps.get_current_active_dashboard)
) -> Any:
    """
    Dashboard (Update user status all users from dashboard)
    """
    background_tasks.add_task(send_report_user, current_user)
    response = message("THE REPORT WILL be delivered", 201, {}, {})
    return response

and also the function send_report_user is the one who will collect the data from the database and after that with that data will create the excel file

def send_report_user(current_user):
    s3: BaseClient = deps.s3_auth()
    if crud.user.is_only_superuser(user= current_user):
        query = (select(User).where(User.role_id !=4))
    elif current_user.department_id == 2:
        query = (select(User)).where(and_(User.role_id != 1, User.department_id !=1 ))
    else:
        query = (select(User)).where(User.department_id == current_user.department_id, User.role_id != 1, User.department_id !=1 )
    
    users = query.order_by(User.last_name)
    users_enable = query.filter(User.is_active == True).order_by(User.last_name)
    users_disable = query.filter(User.is_active == False).order_by(User.last_name)
    
    save_path = 'C:\miProyecto'
    title_columns = ['ID', 'Apellido','Nombre','Departamento','Correo electrónico','Teléfono']
    excel_file_name= 'Nomina_AIG_'+ datetime.datetime.now().date().strftime("%d-%m-%Y")+'.xlsx'   #nombre del archivo nomina 
    workbook = xlsxwriter.Workbook(excel_file_name) #creates a workbook
    worksheet = workbook.add_worksheet(name='Usuarios activos')
    row = 1 #empieza desde la fila 1 columna 0
    col = 0
    for id, name in enumerate(title_columns):
        worksheet.write(0,id, name)
        worksheet.set_column(0,id,15)
    worksheet = workbook.add_worksheet(name='Usuarios inactivos')
    row = 1
    col = 0
    worksheet.write_row(0, 0, title_columns)
    for row_data in users_enable:
        data = [row, row_data.id,row_data.last_name,row_data.first_name,row_data.department_id,row_data.email,
        row_data.phone]
        worksheet.write_row(row, col, data)
        row += 1
    worksheet = workbook.add_worksheet(name='Usuarios totales')
    row = 1
    col = 0
    worksheet.write_row(0, 0, title_columns)
    for row_data in users:
        data = [row, row_data.id,row_data.last_name,row_data.first_name,row_data.department_id,row_data.email,
        row_data.phone]
        worksheet.write_row(row, col, data)
        row += 1

    workbook.close()
    entries = os.listdir('.')
    print(entries)
    #return create_response(data="hola")

the situation is that isnt working and idk what else i could change

anonymuus_gp
  • 111
  • 3
  • Related answer can be found [here](https://stackoverflow.com/a/73240097/17865804), [here](https://stackoverflow.com/a/73414443/17865804), as well as [here](https://stackoverflow.com/a/73843234/17865804) and [here](https://stackoverflow.com/a/71728386/17865804). – Chris Nov 07 '22 at 14:11
  • You can't use a background task if the goal is to let the user get the file directly as part of the response - the background task can't return anything to the user, as the connection has been closed at that time. – MatsLindh Nov 07 '22 at 14:21

0 Answers0