def CreateMatchDetails(matchid, json_data=None): """This creates a MatchDetails object by matchid. If a MatchDetails object already exists, it is deleted and recreated. WARNING: This method is volatile, and will always delete a conflicting duplicate match. See ``GetMatchDetails`` for a non-volatile lookup method. Args: matchid (int): Valid MatchID to parse. json_data (dict): Instead of asking WebAPI for json, you can provide your own. Returns: The newly created MatchDetails object. """ bulk_create = [] account_list = [] match_details = None MatchDetails.objects.filter( match_id=matchid).all().delete() # Delete all previous MatchDetails. try: if not json_data: try: json_data = GetMatchDetailsJson(matchid) except urllib2.HTTPError, e: if e.code == 401: # Unauthorized to view lobby. Return None MatchHistoryQueue.objects.filter( match_id=matchid).all().delete() # Remove from queue. transaction.commit( ) # Make sure the deletion goes through before raising error. raise SteamAPIError("This lobby is password protected.") else: raise json_player_data = json_data['players'] match_details = MatchDetails.from_json_response(json_data) match_details.save() json_picks_bans_data = json_data.get('picks_bans', False) if json_picks_bans_data: picks_bans_bulk_create = [] for json_picks_bans in json_picks_bans_data: picks_bans_bulk_create.append( MatchPicksBans.from_json_response(match_details, json_picks_bans)) MatchPicksBans.objects.bulk_create(picks_bans_bulk_create) for json_player in json_player_data: bulk_player = MatchDetailsPlayerEntry.from_json_response( match_details, json_player) if bulk_player: bulk_create.append(bulk_player) account_list.append( convertAccountNumbertoSteam64( json_player.get('account_id', None))) GetPlayerNames( account_list ) # Loads accounts into db for FK constraints. TODO: Re-work me? Disable FK constraints entirely? if match_details != None and len(bulk_create) > 0: match_details.matchdetailsplayerentry_set.bulk_create(bulk_create) MatchHistoryQueue.objects.filter(match_id=matchid).all().delete() transaction.commit()
def CreateMatchDetails(matchid, json_data=None): """This creates a MatchDetails object by matchid. If a MatchDetails object already exists, it is deleted and recreated. WARNING: This method is volatile, and will always delete a conflicting duplicate match. See ``GetMatchDetails`` for a non-volatile lookup method. Args: matchid (int): Valid MatchID to parse. json_data (dict): Instead of asking WebAPI for json, you can provide your own. Returns: The newly created MatchDetails object. """ bulk_create = [] account_list = [] match_details = None MatchDetails.objects.filter(match_id=matchid).all().delete() # Delete all previous MatchDetails. try: if not json_data: try: json_data = GetMatchDetailsJson(matchid) except urllib2.HTTPError, e: if e.code == 401: # Unauthorized to view lobby. Return None MatchHistoryQueue.objects.filter(match_id=matchid).all().delete() # Remove from queue. transaction.commit() # Make sure the deletion goes through before raising error. raise SteamAPIError("This lobby is password protected.") else: raise json_player_data = json_data['players'] match_details = MatchDetails.from_json_response(json_data) match_details.save() json_picks_bans_data = json_data.get('picks_bans', False) if json_picks_bans_data: picks_bans_bulk_create = [] for json_picks_bans in json_picks_bans_data: picks_bans_bulk_create.append(MatchPicksBans.from_json_response(match_details, json_picks_bans)) MatchPicksBans.objects.bulk_create(picks_bans_bulk_create) for json_player in json_player_data: bulk_player = MatchDetailsPlayerEntry.from_json_response(match_details, json_player) if bulk_player: bulk_create.append(bulk_player) account_list.append(convertAccountNumbertoSteam64(json_player.get('account_id', None))) GetPlayerNames(account_list) # Loads accounts into db for FK constraints. TODO: Re-work me? Disable FK constraints entirely? if match_details != None and len(bulk_create) > 0: match_details.matchdetailsplayerentry_set.bulk_create(bulk_create) MatchHistoryQueue.objects.filter(match_id=matchid).all().delete() transaction.commit()