def _convert_dict_to_apex(self): self.raw_data['GENERAL_MANAGER'] = self.raw_data['GENERALMANAGER'] coach_name = self.raw_data['HEADCOACH'] coach, created = Coach.objects.get_or_create( display_first_last=coach_name, defaults={ 'first_name': coach_name.split()[0], 'last_name': coach_name.split()[-1], 'display_first_last': coach_name }) if created: log.debug(("Created a new coach: ", coach)) self.raw_data['HEAD_COACH'] = coach self.raw_data['D_LEAGUE_AFFILIATION'] = self.raw_data[ 'DLEAGUEAFFILIATION'] for fld in [ 'TEAM_CITY', 'TEAM_NAME', 'TEAM_ABBREVIATION', 'TEAM_CONFERENCE', 'TEAM_DIVISION', 'TEAM_CODE' ]: new_key = fld.split("_")[-1] self.raw_data[new_key] = self.raw_data[fld] team_instance = auto_strip_and_convert_fields(Team, self.raw_data, uppercase=True) return team_instance
def lineups(self): lineups_list = [] for row in self.raw_data: final_data = auto_strip_and_convert_fields(model=self.model, data=row, make_instance=False) player_ids = [ int(pid.strip()) for pid in row['group_id'].split("-") ] players = Player.objects.filter( player_id__in=player_ids).order_by('id') filter_dict = make_unique_filter_dict(self.model, final_data) filter_dict['group_quantity'] = final_data['group_quantity'] lineup = self.model.filter_on_players(players, filter_dict).first() if lineup is None: lineup = self.model(**final_data) lineup.save() lineup.players = players lineup.save() log.debug(("Created new lineup: ", filter_dict, players)) else: for fld in final_data: setattr(lineup, fld, final_data[fld]) lineup.save() lineups_list.append(lineup) return lineups_list
def game_logs(self, game_ids=None, team_ids=None, player_ids=None): log.debug("Creating GameLogs") glogs = [] cur_team_id = None team = None cur_player_id = None player = None cur_game_id = None game = None for row in self.raw_data: # While the NBA does give a 'SEASON_ID' in its response, # It seems to be of the form '22016', or '22015', etc. # I don't know what's going on there, and I don't trust it. game_date = convert_datetime_string_to_date_instance( row['GAME_DATE']) row['GAME_DATE'] = game_date row['SEASON_ID'] = determine_season_for_date(game_date) row['WIN_FLAG'] = row['WL'] == "W" new_game_id = int(row['GAME_ID']) if new_game_id != cur_game_id: if game_ids is not None and new_game_id not in game_ids: continue game = Game.objects.get(game_id=new_game_id) cur_game_id = new_game_id row['GAME'] = game new_team_id = row['TEAM_ID'] if new_team_id != cur_team_id: if team_ids is not None and new_team_id not in team_ids: continue team = Team.objects.get(team_id=new_team_id) cur_team_id = new_team_id row['TEAM'] = team if self.entity_type == Player: new_player_id = row['PLAYER_ID'] if player_ids is not None and new_player_id not in player_ids: continue if new_player_id != cur_player_id: player = Player.objects.get(player_id=new_player_id) cur_player_id = new_player_id row['PLAYER'] = player gm_log = auto_strip_and_convert_fields(model=PlayerGameLog, data=row, uppercase=True, make_instance=True) glogs.append(gm_log) return glogs
def _get_objects(self, detail_flag=False): log.debug("in _get objects") log.debug(("len(self.raw_data): ", len(self.raw_data))) objects = [] idx = 1 if detail_flag else 0 rawdata = self.raw_data[idx] parms = rawdata['parameters'] measure_type = parms['MeasureType'] on = rawdata['resultSets'][1] off = rawdata['resultSets'][2] rows = on['rowSet'] + off['rowSet'] headers = on['headers'] prefix = "Player" middle = measure_type.replace(" ", "") if detail_flag else "" suffix = "OnOffDetail" if detail_flag else "OnOffSummary" model = get_model(prefix, middle, suffix) for row in rows: data = dict(zip(headers, row)) data['SEASON_ID'] = make_season_int(parms['Season']) data['MEASURE_TYPE'] = parms['MeasureType'] data['SEASON_TYPE'] = parms['SeasonType'] data['PER_MODE'] = parms['PerMode'] data['TEAM'] = Team.objects.get(team_id=data['TEAM_ID']) data['PLAYER'] = Player.objects.get(player_id=data['VS_PLAYER_ID']) if detail_flag: if measure_type == "Misc": data['PTS_SECOND_CHANCE'] = data['PTS_2ND_CHANCE'] data['OPP_PTS_SECOND_CHANCE'] = data['OPP_PTS_2ND_CHANCE'] elif measure_type == "Scoring": data['PCT_PTS_2PT_MIDRANGE'] = data['PCT_PTS_2PT_MR'] final_data = auto_strip_and_convert_fields(model, data, uppercase=True, make_instance=False) filter_dict = make_unique_filter_dict(model, final_data) obj, created = model.objects.update_or_create(**filter_dict, defaults=final_data) if created: log.debug(("Created new OnOff ", filter_dict)) objects.append(obj) return objects
def fetch_raw_data(self): log.debug(("Before summary request", self.player_nba_id)) plr_sum = PlayerSummary(player_id=self.player_nba_id) log.debug("After summary request") headers = plr_sum.json['resultSets'][0]['headers'] values = plr_sum.json['resultSets'][0]['rowSet'][0] data_dict = dict(zip(headers, values)) data_dict = sanitize_player_data(data_dict) try: data_dict['TEAM'] = Team.objects.get(team_id=data_dict['TEAM_ID']) except Exception as e: log.debug(("DINGO", data_dict)) raise e final_data = auto_strip_and_convert_fields(Player, data=data_dict, uppercase=True, make_instance=False) self.raw_data = final_data
def splits(self): prefix = "Player" if self.player_or_team == PlayerStats else "Team" model = get_model(prefix=prefix, middle=self.measure_type.replace( "Base", "Traditional"), suffix="Split") splits = [] for row in self.raw_data: data = auto_strip_and_convert_fields(model=model, data=row, make_instance=False) filter_dict = make_unique_filter_dict(model, data=data) split, created = model.objects.update_or_create(**filter_dict, defaults=data) if created: log.debug("Created a new split with the following parms:") log.debug(filter_dict) splits.append(split) return splits
def tracking(self): tracks = [] for row in self.raw_data: # Something weird is happening here, but only in specific cases # Which are yet to be determined data = auto_strip_and_convert_fields(model=self.model, data=row, make_instance=False) filter_dict = make_unique_filter_dict(self.model, data=data) track, created = self.model.objects.update_or_create(**filter_dict, defaults=data) if created: log.debug( "Created a new tracking record with the following parms:") log.debug(filter_dict) tracks.append(track) return tracks
def post(self, request, *args, **kwargs): log.debug(("REQUEST", request.POST)) parms = deepcopy(request.POST) user = User.objects.get(id=request.session['user_id']) parms['commissioner'] = user scoring_fields_in_use = {} for key in request.POST.keys(): if "PlayerTraditional__" in key: scoring_fields_in_use[key] = request.POST[key] or 0 parms['scoring_fields_vals'] = scoring_fields_in_use # Workaround for bug in Django that tries to submit Booleans as 'on' or 'off' for key in parms: if 'flag' in key: parms[key] = parms[key].lower() == "on" league = auto_strip_and_convert_fields(FantasyLeague, data=parms) league.save() request.session['league_id'] = league.id return redirect("nba_fantasy:league-homepage", league_id=league.id)