Added complex_to_csv
This commit is contained in:
parent
63641dafb3
commit
761a279fc8
@ -63,6 +63,38 @@ def query_to_csv(all_items: ModelSelect, exclude=None):
|
||||
return DataFrame(data_list).to_csv(header=False, index=False)
|
||||
|
||||
|
||||
def complex_data_to_csv(complex_data: List):
|
||||
if len(complex_data) == 0:
|
||||
data_list = [['No data found']]
|
||||
else:
|
||||
data_list = [[x for x in complex_data[0].keys()]]
|
||||
for line in complex_data:
|
||||
logging.info(f'line: {line}')
|
||||
this_row = []
|
||||
for key in line:
|
||||
logging.info(f'key: {key}')
|
||||
if line[key] is None:
|
||||
this_row.append('')
|
||||
|
||||
elif isinstance(line[key], dict):
|
||||
if 'name' in line[key]:
|
||||
this_row.append(line[key]['name'])
|
||||
elif 'abbrev' in line[key]:
|
||||
this_row.append(line[key]['abbrev'])
|
||||
else:
|
||||
this_row.append(line[key]['id'])
|
||||
|
||||
elif isinstance(line[key], int) and line[key] > 100000000:
|
||||
this_row.append(str(line[key]))
|
||||
|
||||
else:
|
||||
this_row.append(line[key])
|
||||
|
||||
data_list.append(this_row)
|
||||
|
||||
return DataFrame(data_list).to_csv(header=False, index=False)
|
||||
|
||||
|
||||
def per_season_weeks(season: int, s_type: Literal['regular', 'post', 'total']):
|
||||
if season == 1:
|
||||
if s_type == 'regular':
|
||||
|
||||
@ -87,7 +87,6 @@ async def get_players(
|
||||
elif sort == 'name-desc':
|
||||
all_players = all_players.order_by(-Player.name)
|
||||
|
||||
print(f'csv: {csv}')
|
||||
if csv:
|
||||
player_list = [
|
||||
['name', 'wara', 'image', 'image2', 'team', 'season', 'pitcher_injury', 'pos_1', 'pos_2', 'pos_3',
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
||||
from typing import List, Optional, Literal
|
||||
import copy
|
||||
import logging
|
||||
from pydantic import BaseModel, validator
|
||||
|
||||
from ..db_engine import db, StratPlay, StratGame, Team, Player, Decision, model_to_dict, chunked, fn, SQL
|
||||
from ..db_engine import db, StratPlay, StratGame, Team, Player, Decision, model_to_dict, chunked, fn, SQL, \
|
||||
complex_data_to_csv
|
||||
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
|
||||
|
||||
logging.basicConfig(
|
||||
@ -502,7 +502,8 @@ async def get_pitching_totals(
|
||||
group_by: Literal['team', 'player', 'playerteam', 'playergame', 'teamgame', 'league'] = 'player',
|
||||
min_pa: Optional[int] = 1, team_id: list = Query(default=None), manager_id: list = Query(default=None),
|
||||
obc: list = Query(default=None), risp: Optional[bool] = None, inning: list = Query(default=None),
|
||||
sort: Optional[str] = None, limit: Optional[int] = None, short_output: Optional[bool] = False):
|
||||
sort: Optional[str] = None, limit: Optional[int] = None, short_output: Optional[bool] = False,
|
||||
csv: Optional[bool] = False):
|
||||
season_games = StratGame.select()
|
||||
if season is not None:
|
||||
season_games = season_games.where(StratGame.season << season)
|
||||
@ -696,6 +697,9 @@ async def get_pitching_totals(
|
||||
'rbi%': rbi_rate
|
||||
})
|
||||
db.close()
|
||||
if csv:
|
||||
return Response(content=complex_data_to_csv(return_stats['stats']), media_type='text/csv')
|
||||
|
||||
return return_stats
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user