Update csv exports

This commit is contained in:
Cal Corum 2023-09-15 22:38:37 -05:00
parent 761a279fc8
commit 1fa2a207ee
2 changed files with 11 additions and 31 deletions

View File

@ -85,7 +85,10 @@ def complex_data_to_csv(complex_data: List):
this_row.append(line[key]['id'])
elif isinstance(line[key], int) and line[key] > 100000000:
this_row.append(str(line[key]))
this_row.append(f"'{line[key]}")
elif isinstance(line[key], str) and ',' in line[key]:
this_row.append(line[key].replace(",", "-_-"))
else:
this_row.append(line[key])

View File

@ -4,7 +4,7 @@ import logging
import pydantic
from pandas import DataFrame
from ..db_engine import db, Player, model_to_dict, chunked, fn
from ..db_engine import db, Player, model_to_dict, chunked, fn, complex_data_to_csv
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
logging.basicConfig(
@ -87,36 +87,13 @@ async def get_players(
elif sort == 'name-desc':
all_players = all_players.order_by(-Player.name)
if csv:
player_list = [
['name', 'wara', 'image', 'image2', 'team', 'season', 'pitcher_injury', 'pos_1', 'pos_2', 'pos_3',
'pos_4', 'pos_5', 'pos_6', 'pos_7', 'pos_8', 'last_game', 'last_game2', 'il_return', 'demotion_week',
'headshot', 'vanity_card', 'strat_code', 'bbref_id', 'injury_rating', 'player_id']
]
for line in all_players:
player_list.append(
[
line.name, line.wara, line.image, line.image2, line.team.abbrev, line.season, line.pitcher_injury,
line.pos_1, line.pos_2, line.pos_3, line.pos_4, line.pos_5, line.pos_6, line.pos_7, line.pos_8,
line.last_game, line.last_game2, line.il_return, line.demotion_week, line.headshot,
line.vanity_card, line.strat_code.replace(",", "-_-"), line.bbref_id, line.injury_rating, line.id
]
)
return_players = {
'count': all_players.count(),
'players': DataFrame(player_list).to_csv(header=False, index=False),
'csv': True
}
db.close()
return Response(content=return_players['players'], media_type='text/csv')
else:
return_players = {
'count': all_players.count(),
'players': [model_to_dict(x, recurse=not short_output) for x in all_players]
}
return_players = {
'count': all_players.count(),
'players': [model_to_dict(x, recurse=not short_output) for x in all_players]
}
db.close()
if csv:
return Response(content=complex_data_to_csv(return_players['players']), media_type='text/csv')
return return_players