Added query_to_csv helper

This commit is contained in:
Cal Corum 2023-09-15 00:01:59 -05:00
parent be5bda6652
commit 88b723d9f2
3 changed files with 43 additions and 15 deletions

View File

@ -1,9 +1,12 @@
import math
from datetime import datetime
from typing import List
import logging
import os
from pandas import DataFrame
from peewee import *
from peewee import ModelSelect
from playhouse.shortcuts import model_to_dict
db = SqliteDatabase(
@ -24,6 +27,27 @@ logging.basicConfig(
)
def model_csv_headers(this_obj, exclude=None) -> List:
if this_obj is None:
return ['None']
data = model_to_dict(this_obj, recurse=False, exclude=exclude)
return [x for x in data.keys()]
def model_to_csv(this_obj, exclude=None) -> List:
data = model_to_dict(this_obj, recurse=False, exclude=exclude)
return [x for x in data.values()]
def query_to_csv(all_items: ModelSelect, exclude=None):
data_list = [model_csv_headers(all_items[0], exclude=exclude)]
for x in all_items:
data_list.append(model_to_csv(x, exclude=exclude))
return DataFrame(data_list).to_csv(header=False, index=False)
class BaseModel(Model):
class Meta:
database = db
@ -485,6 +509,7 @@ db.create_tables([
class BattingCard(BaseModel):
player = ForeignKeyField(Player)
variant = IntegerField()
steal_low = IntegerField()
steal_high = IntegerField()
steal_auto = BooleanField()
@ -496,6 +521,9 @@ class BattingCard(BaseModel):
hand = CharField(default='R')
BattingCard.add_index(BattingCard.player, BattingCard.variant)
class BattingCardRatings(BaseModel):
battingcard = ForeignKeyField(BattingCard)
vs_hand = FloatField()

View File

@ -1,7 +1,7 @@
from fastapi import FastAPI
from.routers_v2 import current, teams, rarity, cardsets, players, packtypes, packs, cards, events, results, rewards, \
batstats, pitstats, notifications, paperdex, gamerewards, gauntletrewards, gauntletruns
batstats, pitstats, notifications, paperdex, gamerewards, gauntletrewards, gauntletruns, battingcard
app = FastAPI(
responses={404: {'description': 'Not found'}}
@ -25,3 +25,4 @@ app.include_router(paperdex.router)
app.include_router(gamerewards.router)
app.include_router(gauntletrewards.router)
app.include_router(gauntletruns.router)
app.include_router(battingcard.router)

View File

@ -6,7 +6,7 @@ import pydantic
from pandas import DataFrame
from ..db_engine import db, Team, model_to_dict, fn, Pack, Card, Player, Paperdex, Notification, PackType, \
Rarity, Current
Rarity, Current, query_to_csv
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA, int_timestamp
logging.basicConfig(
@ -121,19 +121,18 @@ async def get_teams(
# raise HTTPException(status_code=404, detail=f'No teams found')
if csv:
data_list = [[
'id', 'abbrev', 'sname', 'lname', 'gmid', 'gmname', 'wallet', 'gsheet', 'team_value',
'collection_value', 'logo', 'color', 'season', 'ranking'
]]
for line in all_teams:
data_list.append(
[
line.id, line.abbrev, line.sname, line.lname, line.gmid, line.gmname, line.wallet, line.gsheet,
line.team_value, line.collection_value, line.logo, f'\'{line.color}', line.season, line.ranking
]
)
return_val = DataFrame(data_list).to_csv(header=False, index=False)
# data_list = [[
# 'id', 'abbrev', 'sname', 'lname', 'gmid', 'gmname', 'wallet', 'gsheet', 'team_value',
# 'collection_value', 'logo', 'color', 'season', 'ranking'
# ]]
# for line in all_teams:
# data_list.append(
# [
# line.id, line.abbrev, line.sname, line.lname, line.gmid, line.gmname, line.wallet, line.gsheet,
# line.team_value, line.collection_value, line.logo, f'\'{line.color}', line.season, line.ranking
# ]
# )
return_val = query_to_csv(all_teams, exclude=[Team.career])
db.close()
return Response(content=return_val, media_type='text/csv')